Constraint Grammar

From Apertium
Revision as of 15:30, 2 April 2021 by Popcorndude (talk | contribs) (subreadings)
Jump to navigation Jump to search

En français

Constraint Grammar is a tool that can be used to POS-tag ambiguous text. There are free constraint grammars developed outside the Apertium project for: Norwegian (the Oslo-Bergen tagger), Sámi languages (from Giellatekno), Faroese (also from Giellatekno), Finnish (by Fred Karlsson).


See also: Apertium stream format
Apertium equivalent: ^words/word<n><pl>/word<vblex><pres><p3><sg>$
  • baseform — the lemma of a word.
  • reading — a single analysis of a word.
Apertium equivalent: ^word<n><pl>$

Basic Rule Format


Sets are defined like this:

LIST VERB = vblex vbser ;     # matches <vblex> or <vbser>
LIST NSG = (n sg) ;           # matches <n><sg>
LIST TO = "to" ;              # matches the lemma "to"
LIST CASE = (n nom) (n acc) ; # matches <n><nom> or <n><acc>

Context Patterns

Context patterns look like this:


PATTERN can be a lemma, set of tags, or the name of a set.

Symbol Meaning
0 The current word
1 The word following the current word
-1 The word preceding the current word
2 The word 2 words after the current word
C Every reading this position must match the pattern (normally only 1 has to)
* In that position or further in that direction
(0 (v))      # the current word must have a verb reading
(1 VERB)     # the following word matches the set "VERB"
(-1 "to")    # the previous word must be "to"
(2C (n))     # every reading of the word after the next one must be a noun
(1* (pr))    # the current word has a preposition after it
(-2* (pron)) # there is a pronoun at least two words before the current word


Rules look like this:


Where FORM is a lemma, set of tags, or the name of a set and CONTEXT is a set of patterns.

SELECT VERB IF (1 (det)) ;    # prefer reading from set "VERB" if following word has <det> tag
REMOVE (n) IF (-1 (adv)) ;    # disprefer <n> reading if preceding word has <adv> tag
SELECT (p1) IF (-1* (prn p1)) # prefer 1st person reading for verbs
               (0 (v)) ;      # if any preceding word is a 1st person pronoun

Note on parenthesis

The use of parentheses to distinguish between tags and lists/sets seems to be the main confusing point for people learning CG. If we have the morphological tags tag1 and tag2, then we can have rules like this:

LIST set1 = tag1 ;
LIST set2 = (tag1 tag2) ; # matches a word with both tag1 and tag2
LIST set3 = tag1 tag2 ;   # matches a word with tag1 or tag2
LIST word = "hello" ;

SELECT:1a (tag1) (1 word) ;
SELECT:1b  set1  (1 word) ;   # equivalent to 1a

SELECT:2a (tag1 tag2) (1 word) ;
SELECT:2b  set2       (1 word) ;   # equivalent to 2a

SELECT:3a tag1 (1 word) ;
SELECT:3b tag2 (1 word) ;
SELECT:3c set3 (1 word) ;   # equivalent to 3a and 3b combined

SELECT:1c  set1  (1 ("hello")) ; # equivalent to 1a (or 1b)


If you're dealing with compounds, the specific parts are called subreadings.

By default the last subreading is treated as the primary one. If you want the first one to be primary, add


If you want to select the value of a subreading, you can use SUB:N (where N is the position of the subreading)

# input ^dog's/dog<n><sg>+'s<gen>/dog<n><sg>+has<vbmod><pres>$ ^eaten/eat<vblex><pp>$
SELECT SUB:1 (vbmod) IF (1 (vblex pp)) ;
# for the first subreading, choose "has" if the next word is a participle

If you want to refer to a subreading in a pattern you can use 1/N

# input ^I've/I<prn>+have<vbmod>$ ^hit/hit<vblex><pres>/hit<vblex><past>/hit<vblex><pp>$
SELECT (vblex pp) IF (-1/1 (vbmod)) ;
# if the first subreading of the preceding word is <vbmod>, then we're probably a participle
# rather than a finite verb

Languages using CG in Apertium

and many others. The following languages currently (2014-06-27) have CG's of over 100 rules:

When is CG needed?

Currently some of the CG rules written in the above language pairs may be written as forbid rules in the TSX format used by apertium-tagger. If the rules for your language pair can be written in the .tsx format, you can go for an easier design without a CG module in that language pair.

Editor support

  • CG-3 IDE – the official vislcg3 CG IDE
  • Gedit syntax highlighting (also for any other editor that uses gtksourceview)
  • Emacs emacs mode for editing and testing CG grammars (highlighting + IDE-like features)

See also

External links