Basic Grew rules

A rewrite rule in Grew is defined by:

Example

rule accuser {
  pattern {
    V [cat=V, lemma="accuser"];
    O [];
    D [cat=D, lemma="de"];
    DO [cat=V, m = inf | part];

    V -[obj]-> O;
    V -[de_obj]-> D;
    D -[obj]-> DO
    }
  without {
    DO -[suj]-> O
  }
  commands {
    add_edge DO -[suj]-> O
  }
}

Using lexicons in Grew rules

⚠️ The syntax described below was introduced in version 1.0. See “About new lexical rules syntax” for more details. ⚠️

Grew rules can be parametrized by one or several lexicons.

Lexicon

A lexicon is defined by:

For instance, the table below describes a tiny lexicon for French nouns where each noun is associated with its gender.

noun Gender
garçon Masc
maison Fem

A lexicon is written as text where:

The lexicon above can be then written in the file nouns.lex

noun	Gender
%-------------
garçon	Masc
maison	Fem

Lexical rule

A rule can be parametrized by a lexicon. The rule below adds a new feat Gender with the relevant value when the noun is found in the lexicon. Note that the lexicon is named in the rule (lex in the example), this will allow us to use several lexicons in the same rule.

rule set_gender (lex from "nouns.lex") {
  pattern { N [upos=NOUN, !Gender, lemma=lex.noun] }
  commands { N.Gender = lex.Gender }
}

Once the lexicon lex is declared, the syntax lex.ident can be used to refer to lexical items in any place where a feature value can be used in the rule definition.

When a lexicon is short and specific to one rule, it may be painful to put it in a new file. In this case, an alternative syntax is proposed: the lexicon is defined directly at the end of the rule definition. The rule above can be written:

rule set_gender {
  pattern { N [upos=NOUN, !Gender, lemma=lex.noun] }
  commands { N.Gender = lex.Gender }
}
#BEGIN lex
noun	Gender
%-------------
garçon	Masc
maison	Fem
#END

Try it!

The file set_gender.py below presents a self-contained example of rewriting with the lexical rule above. It supposes that the grew Python library is installed (see Installation page).

⚠️ The tabulation is not well interpreted when using copy/paste into interactive Python session. That’s why tabulations are replaced by explicit \t in the Python code below.

import grew
grew.init()

g = grew.graph('''graph {
  W1 [form="le", lemma="le", cat=DET];
  W2 [form="garçon", lemma="garçon", cat=NOUN];
  W3 [form="voit", lemma="voir", cat=VERB];
  W4 [form="la", lemma="le", cat=DET];
  W5 [form="maison", lemma="maison", cat=NOUN];
  }''')

r = grew.grs('''
rule set_gender {
  pattern { N [upos=NOUN, !Gender, lemma=lex.noun] }
  commands { N.Gender = lex.Gender }
}
#BEGIN lex
noun\tGender
%-------------
garçon\tMasc
maison\tFem
maison\tMasc
#END
''')

output = grew.run(r, g, 'Iter(set_gender)')

print (output)

Using several lexicons

The file obl_loc.grs below defines a rule which changes the relation obl into obl:loc when both the verb and the preposition are controled by lexicons.

rule obl_loc {
  pattern {
    e: VERB -[obl]-> OBL; OBL -[case]-> ADP;
    VERB [lemma = loc_verb.lemma];
    ADP [lemma = loc_prep.lemma];
  }
  commands { del_edge e; add_edge VERB -[obl:loc]-> OBL; }
}
#BEGIN loc_verb
lemma
%--------------
aller
venir
#END
#BEGIN loc_prep
lemma
%--------------
à
dans
sur
vers
#END

The file max.conll contains the following sentence:

Max

With the command grew transform -grs obl_loc.grs -strat "Onf(obl_loc)" -i max.conll -o max_loc.conll, the rule above is applied twice and produces the next graph:

Max_loc

Using twice the same lexicons

If the file transitive_verbs.lex contains a list of transitive verbs, the following rule distributes the obj relation when two transitive verbs are coordinated.

rule transitive_coord (lex_1 from "transitiv_verbs.lex", lex_2 from "transitive_verbs.lex") {
  pattern {
    VERB1 [lemma=lex_1.lemma]; VERB1 -[conj]-> VERB2; VERB2 [lemma=lex_2.lemma];
    VERB2 -[obj]-> OBJ; VERB2 << OBJ;
  }
  without { VERB1 -[E:obj]-> OBJ; }
  commands { add_edge VERB1 -[E:obj]-> OBJ; }
}

This rule can be used to turn the left part below into the right part:

pomme pomme_enhanced