Grammatical Framework: an Overview Aarne Ranta and Björn Bringert IIT Bombay, January 16, 2008 %!postproc(html): #NEW %!target: html ===GF = Grammatical Framework=== [``gf.digitalgrammars.com`` http://gf.digitalgrammars.com/] Grammar formalism = special-purpose programming language for writing grammars Uses of grammars: - translation - random generation - multilingual syntax editing Interpreter: grammars can be used in GF system Compiler: grammars can be translated to programs in C, Java, JavaScript... #NEW ===Demos=== Here are some things that one can build with GF. [Multilingual and multimodal dialogue system http://www.cs.chalmers.se/~bringert/demos/gotgodis/] [WebALT Multilingual editor for mathematics exercises http://webalt.math.helsinki.fi/PublicFiles/CD/Screencast/TextMathEditor%20Demo.swf] [Translator applet http://www.cs.chalmers.se/~bringert/gf/translate/] [Multilingual authoring system http://www.cs.chalmers.se/~markus/gramlets/letter-applet.html] [Multilingual Wiki http://csmisc14.cs.chalmers.se/~meza/wiki/wiki.cgi] [Language Trainer http://csmisc14.cs.chalmers.se/~bjorn/langtrain.cgi] [Web-based Multimodal Dialogue System http://www.cs.chalmers.se/~bringert/xv/pizza/pizza-movie-large.html] #NEW ===Grammar formats=== **Context-free grammars** (= **BNF**) can be used in GF ``` -- file food.cf Is. S ::= Item "is" Quality ; That. Item ::= "that" Kind ; This. Item ::= "this" Kind ; QKind. Kind ::= Quality Kind ; Cheese. Kind ::= "cheese" ; Fish. Kind ::= "fish" ; Pizza. Kind ::= "pizza" ; Wine. Kind ::= "wine" ; Italian. Quality ::= "Italian" ; Boring. Quality ::= "boring" ; Delicious. Quality ::= "delicious" ; Expensive. Quality ::= "expensive" ; Fresh. Quality ::= "fresh" ; Very. Quality ::= "very" Quality ; Warm. Quality ::= "warm" ; ``` #NEW ===Using the grammar in GF interpreter=== Start GF ``` $ gf ``` Import a grammar ``` > import food.cf ``` **Parse** a **string** to a **tree** ``` > parse "this pizza is delicious" Is (This Pizza) Delicious ``` **Linearize** a tree to a string ``` > linearize Is (That Wine) (Very (Very Italian)) that wine is very very Italian ``` #NEW ===Generation and pipes=== **Random-generate** a tree ``` > generate_random Is (This (QKind Fresh Fish)) Fresh ``` Random-generate with a **pipe** to linearization ``` > generate_random | linearize this pizza is warm ``` Random-generate with a pipe to linearization, preserving a **trace** ``` > generate_random -tr | linearize Is (That Fish) Boring that fish is boring ``` #NEW ===Abstract and concrete syntax=== Full GF format is more expressive than BNF: - **Abstract syntax**: semantic structure, "what to say" - **Concrete syntax**: linguistic expression, "how to say it" BNF format mixes these two in one grammar GF takes them apart, which enables **multilingual grammars** - one abstract syntax + several concrete syntaxes #NEW ===Abstract syntax of the food example=== ``` -- file Food.gf abstract Food = { cat Phrase ; Item ; Kind ; Quality ; flags startcat = Phrase ; fun Is : Item -> Quality -> Phrase ; This, That : Kind -> Item ; QKind : Quality -> Kind -> Kind ; Wine, Pizza, Cheese, Fish : Kind ; Very : Quality -> Quality ; Fresh, Warm, Italian, Expensive, Delicious, Boring : Quality ; } ``` #NEW ===English concrete syntax of the food example=== ``` -- file FoodEng.gf concrete FoodEng of Food = { lincat Phrase, Item, Kind, Quality = {s : Str} ; lin Is item quality = {s = item.s ++ "is" ++ quality.s} ; This kind = {s = "this" ++ kind.s} ; That kind = {s = "that" ++ kind.s} ; QKind quality kind = {s = quality.s ++ kind.s} ; Wine = {s = "wine"} ; Pizza = {s = "pizza"} ; Cheese = {s = "cheese"} ; Fish = {s = "fish"} ; Very quality = {s = "very" ++ quality.s} ; Fresh = {s = "fresh"} ; Warm = {s = "warm"} ; Italian = {s = "Italian"} ; Expensive = {s = "expensive"} ; Delicious = {s = "delicious"} ; Boring = {s = "boring"} ; } ``` #NEW ===Italian concrete syntax of the food example=== ``` -- file FoodIta.gf concrete FoodIta of Food = { lincat Phrase, Item, Kind, Quality = {s : Str} ; lin Is item quality = {s = item.s ++ "è" ++ quality.s} ; This kind = {s = "questo" ++ kind.s} ; That kind = {s = "quello" ++ kind.s} ; QKind quality kind = {s = kind.s ++ quality.s} ; Wine = {s = "vino"} ; Pizza = {s = "pizza"} ; Cheese = {s = "formaggio"} ; Fish = {s = "pesce"} ; Very quality = {s = "molto" ++ quality.s} ; Fresh = {s = "fresco"} ; Warm = {s = "caldo"} ; Italian = {s = "italiano"} ; Expensive = {s = "caro"} ; Delicious = {s = "delizioso"} ; Boring = {s = "noioso"} ; } ``` #NEW ===Translation and multilingual generation=== Import the grammars ``` > import FoodEng.gf > import FoodIta.gf ``` **Translate**: parse in English, linearize in Italian ``` > parse -lang=FoodEng "this Italian wine is very boring" | linearize -lang=FoodIta questo vino italiano è molto noioso ``` Random-generate and linearize to multiple languages ``` > generate_random | linearize -multi quello pesce è noioso that fish is boring ``` #NEW ===Translation quiz=== ``` > translation_quiz FoodEng FoodIta Welcome to GF Translation Quiz. The quiz is over when you have done at least 10 examples with at least 75 % success. You can interrupt the quiz by entering a line consisting of a dot ('.'). this cheese is boring questo formaggio è noioso > Yes. Score 1/1 that delicious cheese is Italian quello vino delicioso è italiano > No, not quello vino delicioso è italiano, but quello formaggio delizioso è italiano Score 1/2 ``` #NEW ===Swedish concrete syntax of the food example?=== Let's try it out: ``` -- file FoodSwe.gf concrete FoodSwe of Food = { ... } ``` Just replace English words with Swedish words... ? #NEW ===Problems with string-based grammars=== Errors in **inflection** and **agreement** Solution: records and parameters Large-scale solution: resource grammar libraries #NEW ===Parameters=== See the section "Grammars with parameters" in the [GF Tutorial http://www.cs.chalmers.se/Cs/Research/Language-technology/GF/doc/gf-tutorial.html].