Grammar

Glossia uses a two-layer grammar system: a context-free grammar (CFG) layer that generates POS tag sequences, and a type-driven layer based on Montague Grammar that constrains POS slots via lambda calculus.

Grammar Files

Grammars are defined in YAML files located in languages/{language}/grammar.yaml. Each grammar file specifies:

  1. Types: Montague Grammar semantic types for each POS tag
  2. Rules: CFG production rules with weights and lambda terms

Example Grammar (English)

grammar:
  name: "English Grammar"

  types:
    - name: "N"
      lambda_type: "e -> t"
    - name: "V"
      lambda_type: "e -> (e -> t)"
    - name: "Adj"
      lambda_type: "e -> e"
    - name: "Det"
      lambda_type: "(e -> t) -> (e -> t)"
    - name: "Cop"
      lambda_type: "(e -> t) -> (e -> t)"
    - name: "Dot"
      lambda_type: "t"

  rules:
    sentence:
      lambda: "N"
      cfg_productions:
        - production: "NP VP Dot"
          weight: 0.85
          lambda: "N"
        - production: "NP Adv V NP Dot"
          weight: 0.10
          lambda: "N"
        - production: "NP V NP Dot"
          weight: 0.05
          lambda: "N"

    noun_phrase:
      lambda: "N"
      cfg_productions:
        - production: "N"
          weight: 1.0
          lambda: "N"
        - production: "Det[def] N"
          weight: 0.50
          lambda: "Det"
        - production: "Det[indef] N"
          weight: 0.25
          lambda: "Det"

    verb_phrase:
      lambda: "N"
      cfg_productions:
        - production: "VP_BASE"
          weight: 0.70
          lambda: "N"
        - production: "VP_BASE VP_PP_TAIL"
          weight: 0.30
          lambda: "N"

Key Syntax

  • Terminal symbols are POS tags: Det, Adj, N, V, Modal, Aux, Cop, To, Prep, Adv, Conj, Dot, Prefix
  • Refinement tags in brackets constrain cover word selection: Det[def] selects definite determiners ("the"), Det[indef] selects indefinite ("a", "an"), Det[quant] selects quantifiers ("each", "some")
  • Weights determine probabilistic selection between alternative productions
  • Rule names map to standard abbreviations: noun_phrase -> NP, verb_phrase -> VP, sentence -> S

POS Tags

Glossia uses 13 parts of speech:

POSDescriptionExample Words
DetDeterminerthe, a, each, some
AdjAdjectivebig, red, fast
NNouncat, house, time
VVerbrun, see, get
ModalModal verbmay, can, might
AuxAuxiliary verbhas, will, does
CopCopulais, are, was
ToInfinitive markerto
PrepPrepositionin, on, for, to
AdvAdverbvery, already, out
ConjConjunctionand, but, or
DotSentence terminator.
PrefixSentence prefixRe:, Fwd:

POS Slot Constraints

  • Payload-eligible POS: N, V, Adj, Adv, Prep, Det, Conj - these slots can be filled with either payload or cover words
  • Cover-only POS: Aux, Cop, To, Prefix, Dot, Modal - these slots are always filled with cover (function) words

Montague Grammar Type System

Each POS tag maps to a semantic type from Montague Grammar:

POSTypeInterpretation
Ne -> tPredicate over entities
Ve -> (e -> t)Relation between entities
Adje -> eEntity modifier
Adv(e -> t) -> (e -> t)Predicate modifier
Det(e -> t) -> (e -> t)Quantifier
Prepe -> (e -> t)Prepositional relation
Conjt -> (t -> t)Proposition connective
Cop(e -> t) -> (e -> t)Subject-predicate linker
Modal/Aux/To(e -> t) -> (e -> t)Predicate modifier
DottSentence truth value
Prefixt -> tSentence wrapper

Where:

  • e = Entity type
  • t = Truth value type
  • -> = Function type

These types ensure that constituents combine in semantically valid ways, beyond what the CFG alone can enforce.

Dialect Support

Grammar files can define dialect overlays that modify the base rules for different generation modes. Dialects are defined in a separate dialect.yaml file:

dialects:
  subject:
    rules:
      sentence:
        cfg_productions:
          - production: "Prefix NP Cop Adj Dot"
            weight: 0.30
          - production: "NP Cop Adj Dot"
            weight: 0.70

  poetry:
    rules:
      sentence:
        cfg_productions:
          - production: "NP VP Dot"
            weight: 0.50
          - production: "Adj N V Adv Dot"
            weight: 0.50

The --dialect subject and --dialect body flags select which dialect overlay to apply on top of the base grammar rules.

Sentence Length Strategy

The grammar interacts with the length mode to control output:

  • Compact mode: Tries sentence lengths from k_min to k_max, stopping at the first length where the payload fits. Within that length, picks the highest-probability POS sequence.
  • Natural mode: Samples sentence length from the grammar's probability distribution across all valid lengths. This produces more varied, natural-sounding text.

The scoring function for balancing compactness vs naturalness:

score = log P(sequence) - λ * length

Higher λ yields shorter text; lower λ yields more natural text.