mirror of
https://github.com/GrammaticalFramework/gf-core.git
synced 2026-04-12 06:19:33 -06:00
moved tutorial examples to GF/examples
This commit is contained in:
69
examples/tutorial/syntax/MorphoEng.gf
Normal file
69
examples/tutorial/syntax/MorphoEng.gf
Normal file
@@ -0,0 +1,69 @@
|
||||
--# -path=.:prelude
|
||||
|
||||
resource MorphoEng = open Prelude in {
|
||||
|
||||
-- the lexicon construction API
|
||||
|
||||
oper
|
||||
mkN : overload {
|
||||
mkN : (bus : Str) -> Noun ;
|
||||
mkN : (man,men : Str) -> Noun ;
|
||||
} ;
|
||||
|
||||
mkA : (warm : Str) -> Adjective ;
|
||||
|
||||
mkV : overload {
|
||||
mkV : (kiss : Str) -> Verb ;
|
||||
mkV : (do,does : Str) -> Verb ;
|
||||
} ;
|
||||
|
||||
mkV2 : overload {
|
||||
mkV2 : (love : Verb) -> Verb2 ;
|
||||
mkV2 : (talk : Verb) -> (about : Str) -> Verb2 ;
|
||||
} ;
|
||||
|
||||
-- grammar-internal definitions
|
||||
|
||||
param
|
||||
Number = Sg | Pl ;
|
||||
|
||||
oper
|
||||
Noun, Verb : Type = {s : Number => Str} ;
|
||||
Adjective : Type = {s : Str} ;
|
||||
Verb2 : Type = Verb ** {c : Str} ;
|
||||
|
||||
mkN = overload {
|
||||
mkN : (bus : Str) -> Noun = \s -> mkNoun s (add_s s) ;
|
||||
mkN : (man,men : Str) -> Noun = mkNoun ;
|
||||
} ;
|
||||
|
||||
mkA : (warm : Str) -> Adjective = ss ;
|
||||
|
||||
mkV = overload {
|
||||
mkV : (kiss : Str) -> Verb = \s -> mkVerb s (add_s s) ;
|
||||
mkV : (do,does : Str) -> Verb = mkVerb ;
|
||||
} ;
|
||||
|
||||
mkV2 = overload {
|
||||
mkV2 : (love : Verb) -> Verb2 = \love -> love ** {c = []} ;
|
||||
mkV2 : (talk : Verb) -> (about : Str) -> Verb2 =
|
||||
\talk,about -> talk ** {c = about} ;
|
||||
} ;
|
||||
|
||||
add_s : Str -> Str = \w -> case w of {
|
||||
_ + "oo" => w + "s" ; -- bamboo
|
||||
_ + ("s" | "z" | "x" | "sh" | "o") => w + "es" ; -- bus, hero
|
||||
_ + ("a" | "o" | "u" | "e") + "y" => w + "s" ; -- boy
|
||||
x + "y" => x + "ies" ; -- fly
|
||||
_ => w + "s" -- car
|
||||
} ;
|
||||
|
||||
mkNoun : Str -> Str -> Noun = \x,y -> {
|
||||
s = table {
|
||||
Sg => x ;
|
||||
Pl => y
|
||||
}
|
||||
} ;
|
||||
|
||||
mkVerb : Str -> Str -> Verb = \x,y -> mkNoun y x ;
|
||||
}
|
||||
100
examples/tutorial/syntax/MorphoIta.gf
Normal file
100
examples/tutorial/syntax/MorphoIta.gf
Normal file
@@ -0,0 +1,100 @@
|
||||
--# -path=.:prelude
|
||||
|
||||
-- This is a simple Italian resource morphology for the GF tutorial.
|
||||
|
||||
resource MorphoIta = open Prelude in {
|
||||
|
||||
-- the lexicographer's API
|
||||
|
||||
oper
|
||||
masculine, feminine : Gender ;
|
||||
|
||||
|
||||
|
||||
param
|
||||
Number = Sg | Pl ;
|
||||
Gender = Masc | Fem ;
|
||||
|
||||
oper
|
||||
Noun : Type = {s : Number => Str ; g : Gender} ;
|
||||
Adjective : Type = {s : Gender => Number => Str} ;
|
||||
|
||||
-- we will only use present indicative third person verb forms
|
||||
|
||||
Verb : Type = {s : Number => Str} ;
|
||||
|
||||
-- two-place verbs have a preposition
|
||||
|
||||
Verb2 : Type = Verb ** {c : Str} ;
|
||||
|
||||
-- this function takes the gender and both singular and plural forms
|
||||
|
||||
mkNoun : Gender -> Str -> Str -> Noun = \g,vino,vini -> {
|
||||
s = table {
|
||||
Sg => vino ;
|
||||
Pl => vini
|
||||
} ;
|
||||
g = g
|
||||
} ;
|
||||
|
||||
-- this function takes the singular form
|
||||
|
||||
regNoun : Str -> Noun = \vino ->
|
||||
case vino of {
|
||||
vin + c@("c" | "g") + "a"
|
||||
=> mkNoun Fem vino (vin + c + "he") ; -- banche
|
||||
vin + "a"
|
||||
=> mkNoun Fem vino (vin + "e") ; -- pizza
|
||||
vin + c@("c" | "g") + "o"
|
||||
=> mkNoun Masc vino (vin + c + "hi") ; -- boschi
|
||||
vin + ("o" | "e")
|
||||
=> mkNoun Masc vino (vin + "i") ; -- vino, pane
|
||||
_ => mkNoun Masc vino vino -- tram
|
||||
} ;
|
||||
|
||||
-- to make nouns such as "carne", "università" feminine
|
||||
|
||||
femNoun : Noun -> Noun = \mano -> {
|
||||
s = mano.s ;
|
||||
g = Fem
|
||||
} ;
|
||||
|
||||
-- this takes both genders and numbers
|
||||
|
||||
mkAdjective : (x1,_,_,x4 : Str) -> Adjective = \nero,nera,neri,nere -> {
|
||||
s = table {
|
||||
Masc => (mkNoun Masc nero neri).s ;
|
||||
Fem => (mkNoun Fem nera nere).s
|
||||
}
|
||||
} ;
|
||||
|
||||
-- this takes the masculine singular form
|
||||
|
||||
regAdjective : Str -> Adjective = \nero ->
|
||||
let ner = init nero in
|
||||
case last nero of {
|
||||
"o" => mkAdjective (ner + "o") (ner + "a") (ner + "i") (ner + "e") ;
|
||||
"e" => mkAdjective (ner + "e") (ner + "e") (ner + "i") (ner + "i") ;
|
||||
_ => mkAdjective nero nero nero nero
|
||||
} ;
|
||||
|
||||
-- this function takes the singular and plural forms
|
||||
|
||||
mkVerb : Str -> Str -> Verb = \ama,amano -> {
|
||||
s = table {
|
||||
Sg => ama ;
|
||||
Pl => amano
|
||||
}
|
||||
} ;
|
||||
|
||||
-- this function takes the infinitive form
|
||||
|
||||
regVerb : Str -> Verb = \amare ->
|
||||
let am = Predef.tk 3 amare in
|
||||
case Predef.dp 3 amare of {
|
||||
"ere" => mkVerb (am + "e") (am + "ono") ; -- premere
|
||||
"ire" => mkVerb (am + "isce") (am + "iscono") ; -- finire
|
||||
_ => mkVerb (am + "a") (am + "ano") -- amare
|
||||
} ;
|
||||
|
||||
}
|
||||
60
examples/tutorial/syntax/Syntax.gf
Normal file
60
examples/tutorial/syntax/Syntax.gf
Normal file
@@ -0,0 +1,60 @@
|
||||
abstract Syntax = {
|
||||
|
||||
flags startcat=Phr ;
|
||||
|
||||
cat
|
||||
Phr ; -- any complete sentence e.g. "Is this pizza good?"
|
||||
S ; -- declarative sentence e.g. "this pizza is good"
|
||||
QS ; -- question sentence e.g. "is this pizza good"
|
||||
NP ; -- noun phrase e.g. "this pizza"
|
||||
IP ; -- interrogative phrase e.g "which pizza"
|
||||
CN ; -- common noun phrase e.g. "very good pizza"
|
||||
Det ; -- determiner e.g. "this"
|
||||
AP ; -- adjectival phrase e.g. "very good"
|
||||
AdA ; -- adadjective e.g. "very"
|
||||
VP ; -- verb phrase e.g. "is good"
|
||||
N ; -- noun e.g. "pizza"
|
||||
A ; -- adjective e.g. "good"
|
||||
V ; -- intransitive verb e.g. "boil"
|
||||
V2 ; -- two-place verb e.g. "eat"
|
||||
|
||||
fun
|
||||
PhrS : S -> Phr ;
|
||||
PhrQS : QS -> Phr ;
|
||||
|
||||
PosVP, NegVP : NP -> VP -> S ;
|
||||
QPosVP, QNegVP : NP -> VP -> QS ;
|
||||
|
||||
IPPosVP, IPNegVP : IP -> VP -> QS ;
|
||||
IPPosV2, IPNegV2 : IP -> NP -> V2 -> QS ;
|
||||
|
||||
ComplV2 : V2 -> NP -> VP ;
|
||||
ComplAP : AP -> VP ;
|
||||
|
||||
DetCN : Det -> CN -> NP ;
|
||||
|
||||
ModCN : AP -> CN -> CN ;
|
||||
|
||||
AdAP : AdA -> AP -> AP ;
|
||||
|
||||
WhichCN : CN -> IP ;
|
||||
|
||||
UseN : N -> CN ;
|
||||
UseA : A -> AP ;
|
||||
UseV : V -> VP ;
|
||||
|
||||
-- entries of the closed lexicon
|
||||
|
||||
this_Det : Det ;
|
||||
that_Det : Det ;
|
||||
these_Det : Det ;
|
||||
those_Det : Det ;
|
||||
every_Det : Det ;
|
||||
theSg_Det : Det ;
|
||||
thePl_Det : Det ;
|
||||
indef_Det : Det ;
|
||||
plur_Det : Det ;
|
||||
two_Det : Det ;
|
||||
|
||||
very_AdA : AdA ;
|
||||
}
|
||||
118
examples/tutorial/syntax/SyntaxEng.gf
Normal file
118
examples/tutorial/syntax/SyntaxEng.gf
Normal file
@@ -0,0 +1,118 @@
|
||||
--# -path=.:prelude
|
||||
|
||||
concrete SyntaxEng of Syntax = open Prelude, MorphoEng in {
|
||||
|
||||
lincat
|
||||
Phr = {s : Str} ;
|
||||
S = {s : Str} ;
|
||||
QS = {s : Str} ;
|
||||
NP = NounPhrase ;
|
||||
IP = NounPhrase ;
|
||||
CN = Noun ;
|
||||
Det = {s : Str ; n : Number} ;
|
||||
AP = {s : Str} ;
|
||||
AdA = {s : Str} ;
|
||||
VP = VerbPhrase ;
|
||||
N = Noun ;
|
||||
A = {s : Str} ;
|
||||
V = Verb ;
|
||||
V2 = Verb2 ;
|
||||
|
||||
lin
|
||||
PhrS = postfixSS "." ;
|
||||
PhrQS = postfixSS "?" ;
|
||||
|
||||
PosVP = predVP True True ;
|
||||
NegVP = predVP True False ;
|
||||
QPosVP = predVP False True ;
|
||||
QNegVP = predVP False False ;
|
||||
IPPosVP = predVP True True ;
|
||||
IPNegVP = predVP True False ;
|
||||
|
||||
IPPosV2 ip np v2 = {
|
||||
s = let
|
||||
vp : VerbPhrase = {s = \\q,b,n => predVerb v2 q b n} ;
|
||||
in
|
||||
bothWays (ip.s ++ (predVP False True np vp).s) v2.c
|
||||
} ;
|
||||
IPNegV2 ip np v2 = {
|
||||
s = let
|
||||
vp : VerbPhrase = {s = \\q,b,n => predVerb v2 q b n} ;
|
||||
in
|
||||
bothWays (ip.s ++ (predVP False False np vp).s) v2.c
|
||||
} ;
|
||||
|
||||
|
||||
ComplV2 v2 np = {
|
||||
s = \\q,b,n =>
|
||||
let vp = predVerb v2 q b n in
|
||||
<vp.p1, vp.p2 ++ v2.c ++ np.s>
|
||||
} ;
|
||||
|
||||
ComplAP ap = {s = \\_,b,n => <copula b n, ap.s>} ;
|
||||
|
||||
DetCN det cn = {s = det.s ++ cn.s ! det.n ; n = det.n} ;
|
||||
|
||||
ModCN ap cn = {s = \\n => ap.s ++ cn.s ! n} ;
|
||||
|
||||
AdAP ada ap = {s = ada.s ++ ap.s} ;
|
||||
|
||||
WhichCN cn = {s = "which" ++ cn.s ! Sg ; n = Sg} ;
|
||||
|
||||
UseN n = n ;
|
||||
UseA a = a ;
|
||||
UseV v = {s = \\q,b,n => predVerb v q b n} ;
|
||||
|
||||
this_Det = {s = "this" ; n = Sg} ;
|
||||
that_Det = {s = "that" ; n = Sg} ;
|
||||
these_Det = {s = "these" ; n = Pl} ;
|
||||
those_Det = {s = "those" ; n = Pl} ;
|
||||
every_Det = {s = "every" ; n = Sg} ;
|
||||
theSg_Det = {s = "the" ; n = Sg} ;
|
||||
thePl_Det = {s = "the" ; n = Pl} ;
|
||||
indef_Det = {s = artIndef ; n = Sg} ;
|
||||
plur_Det = {s = [] ; n = Pl} ;
|
||||
two_Det = {s = "two" ; n = Pl} ;
|
||||
|
||||
very_AdA = {s = "very"} ;
|
||||
|
||||
oper
|
||||
NounPhrase = {s : Str ; n : Number} ;
|
||||
VerbPhrase = {s : Bool => Bool => Number => Str * Str} ; -- decl, pol
|
||||
|
||||
predVP : Bool -> Bool -> NounPhrase -> VerbPhrase -> SS =
|
||||
\q,b,np,vp -> {
|
||||
s = let vps = vp.s ! q ! b ! np.n
|
||||
in case q of {
|
||||
True => np.s ++ vps.p1 ++ vps.p2 ;
|
||||
False => vps.p1 ++ np.s ++ vps.p2
|
||||
}
|
||||
} ;
|
||||
|
||||
copula : Bool -> Number -> Str = \b,n -> case n of {
|
||||
Sg => posneg b "is" ;
|
||||
Pl => posneg b "are"
|
||||
} ;
|
||||
|
||||
do : Bool -> Number -> Str = \b,n ->
|
||||
posneg b ((mkV "do").s ! n) ;
|
||||
|
||||
predVerb : Verb -> Bool -> Bool -> Number -> Str * Str = \verb,q,b,n ->
|
||||
let
|
||||
inf = verb.s ! Pl ;
|
||||
fin = verb.s ! n ;
|
||||
aux = do b n
|
||||
in
|
||||
case <q,b> of {
|
||||
<True,True> => <[],fin> ;
|
||||
_ => <aux,inf>
|
||||
} ;
|
||||
|
||||
posneg : Bool -> Str -> Str = \b,do -> case b of {
|
||||
True => do ;
|
||||
False => do + "n't"
|
||||
} ;
|
||||
|
||||
artIndef : Str =
|
||||
pre {"a" ; "an" / strs {"a" ; "e" ; "i" ; "o"}} ;
|
||||
}
|
||||
102
examples/tutorial/syntax/SyntaxIta.gf
Normal file
102
examples/tutorial/syntax/SyntaxIta.gf
Normal file
@@ -0,0 +1,102 @@
|
||||
--# -path=.:prelude
|
||||
|
||||
concrete SyntaxIta of Syntax = open Prelude, MorphoIta in {
|
||||
|
||||
lincat
|
||||
Phr = {s : Str} ;
|
||||
S = {s : Str} ;
|
||||
QS = {s : Str} ;
|
||||
NP = {s : Str ; g : Gender ; n : Number} ;
|
||||
IP = {s : Str ; g : Gender ; n : Number} ;
|
||||
CN = Noun ;
|
||||
Det = {s : Gender => Str ; n : Number} ;
|
||||
AP = {s : Gender => Number => Str} ;
|
||||
AdA = {s : Str} ;
|
||||
VP = {s : Bool => Gender => Number => Str} ;
|
||||
|
||||
N = Noun ;
|
||||
A = Adjective ;
|
||||
V = Verb ;
|
||||
V2 = Verb2 ;
|
||||
|
||||
lin
|
||||
PhrS = postfixSS "." ;
|
||||
PhrQS = postfixSS "?" ;
|
||||
|
||||
PosVP np vp = {s = np.s ++ vp.s ! True ! np.g ! np.n} ;
|
||||
NegVP np vp = {s = np.s ++ vp.s ! False ! np.g ! np.n} ;
|
||||
QPosVP np vp = {s = np.s ++ vp.s ! True ! np.g ! np.n} ;
|
||||
QNegVP np vp = {s = np.s ++ vp.s ! False ! np.g ! np.n} ;
|
||||
IPPosVP np vp = {s = np.s ++ vp.s ! True ! np.g ! np.n} ;
|
||||
IPNegVP np vp = {s = np.s ++ vp.s ! False ! np.g ! np.n} ;
|
||||
|
||||
IPPosV2 ip np v2 = {s = v2.c ++ ip.s ++ v2.s ! np.n ++ np.s} ;
|
||||
IPNegV2 ip np v2 = {s = v2.c ++ ip.s ++ "non" ++ v2.s ! np.n ++ np.s} ;
|
||||
|
||||
ComplV2 v2 np = {s = \\b,_,n => posneg b ++ v2.s ! n ++ v2.c ++ np.s} ;
|
||||
ComplAP ap = {s = \\b,g,n => posneg b ++ copula n ++ ap.s ! g ! n} ;
|
||||
|
||||
DetCN det cn = {s = det.s ! cn.g ++ cn.s ! det.n ; g = cn.g ; n = det.n} ;
|
||||
|
||||
ModCN ap cn = {s = \\n => cn.s ! n ++ ap.s ! cn.g ! n ; g = cn.g} ;
|
||||
|
||||
AdAP ada ap = {s = \\n,g => ada.s ++ ap.s ! n ! g} ;
|
||||
|
||||
WhichCN cn = {s = "quale" ++ cn.s ! Sg ; g = cn.g ; n = Sg} ;
|
||||
|
||||
UseN n = n ;
|
||||
UseA a = a ;
|
||||
UseV v = {s = \\b,_,n => posneg b ++ v.s ! n} ;
|
||||
|
||||
|
||||
this_Det = mkDet Sg (regAdjective "questo") ;
|
||||
that_Det = mkDet Sg (regAdjective "quello") ;
|
||||
these_Det = mkDet Pl (regAdjective "questo") ;
|
||||
those_Det = mkDet Pl (regAdjective "quello") ;
|
||||
every_Det = {s = \\_ => "ogni" ; n = Sg} ;
|
||||
theSg_Det = {s = artDef Sg ; n = Sg} ;
|
||||
thePl_Det = {s = artDef Pl ; n = Pl} ;
|
||||
indef_Det = {s = artIndef ; n = Sg} ;
|
||||
plur_Det = {s = \\_ => [] ; n = Pl} ;
|
||||
two_Det = {s = \\_ => "due" ; n = Pl} ;
|
||||
|
||||
very_AdA = {s = "molto"} ;
|
||||
|
||||
|
||||
oper
|
||||
copula : Number -> Str = \n -> case n of {
|
||||
Sg => "è" ;
|
||||
Pl => "sono"
|
||||
} ;
|
||||
|
||||
posneg : Bool -> Str = \b -> case b of {
|
||||
True => [] ;
|
||||
False => "non"
|
||||
} ;
|
||||
|
||||
mkDet : Number -> Adjective -> Det = \n,adj -> {
|
||||
s = \\g => adj.s ! g ! n ;
|
||||
n = n ;
|
||||
lock_Det = <>
|
||||
} ;
|
||||
|
||||
artDef : Number -> Gender => Str = \n -> case n of {
|
||||
Sg => table {
|
||||
Masc => pre {"il" ; "lo" / sImpuro} ;
|
||||
Fem => "la"
|
||||
} ;
|
||||
Pl => table {
|
||||
Masc => pre {"i" ; "gli" / sImpuro ; "gli" / vowel} ;
|
||||
Fem => "le"
|
||||
}
|
||||
} ;
|
||||
|
||||
artIndef : Gender => Str = table {
|
||||
Masc => pre {"un" ; "uno" / sImpuro} ;
|
||||
Fem => pre {"una" ; "un'" / vowel}
|
||||
} ;
|
||||
|
||||
sImpuro : Strs = strs {"sb" ; "sp" ; "sy" ; "z"} ;
|
||||
vowel : Strs = strs {"a" ; "e" ; "i" ; "o" ; "u"} ;
|
||||
|
||||
}
|
||||
8
examples/tutorial/syntax/Test.gf
Normal file
8
examples/tutorial/syntax/Test.gf
Normal file
@@ -0,0 +1,8 @@
|
||||
abstract Test = Syntax ** {
|
||||
|
||||
fun
|
||||
Wine, Cheese, Fish, Pizza, Waiter, Customer : N ;
|
||||
Fresh, Warm, Italian, Expensive, Delicious, Boring : A ;
|
||||
Stink : V ;
|
||||
Eat, Love, Talk : V2 ;
|
||||
}
|
||||
23
examples/tutorial/syntax/TestEng.gf
Normal file
23
examples/tutorial/syntax/TestEng.gf
Normal file
@@ -0,0 +1,23 @@
|
||||
--# -path=.:resource:prelude
|
||||
|
||||
concrete TestEng of Test = SyntaxEng ** open Prelude, MorphoEng in {
|
||||
|
||||
lin
|
||||
Wine = mkN "wine" ;
|
||||
Cheese = mkN "cheese" ;
|
||||
Fish = mkN "fish" "fish" ;
|
||||
Pizza = mkN "pizza" ;
|
||||
Waiter = mkN "waiter" ;
|
||||
Customer = mkN "customer" ;
|
||||
Fresh = mkA "fresh" ;
|
||||
Warm = mkA "warm" ;
|
||||
Italian = mkA "Italian" ;
|
||||
Expensive = mkA "expensive" ;
|
||||
Delicious = mkA "delicious" ;
|
||||
Boring = mkA "boring" ;
|
||||
Stink = mkV "stink" ;
|
||||
Eat = mkV2 (mkV "eat") ;
|
||||
Love = mkV2 (mkV "love") ;
|
||||
Talk = mkV2 (mkV "talk") "about" ;
|
||||
}
|
||||
|
||||
23
examples/tutorial/syntax/TestIta.gf
Normal file
23
examples/tutorial/syntax/TestIta.gf
Normal file
@@ -0,0 +1,23 @@
|
||||
--# -path=.:resource:prelude
|
||||
|
||||
concrete TestIta of Test = SyntaxIta ** open Prelude, MorphoIta in {
|
||||
|
||||
lin
|
||||
Wine = regNoun "vino" ;
|
||||
Cheese = regNoun "formaggio" ;
|
||||
Fish = regNoun "pesce" ;
|
||||
Pizza = regNoun "pizza" ;
|
||||
Waiter = regNoun "cameriere" ;
|
||||
Customer = regNoun "cliente" ;
|
||||
Fresh = regAdjective "fresco" ;
|
||||
Warm = regAdjective "caldo" ;
|
||||
Italian = regAdjective "italiano" ;
|
||||
Expensive = regAdjective "caro" ;
|
||||
Delicious = regAdjective "delizioso" ;
|
||||
Boring = regAdjective "noioso" ;
|
||||
Stink = regVerb "puzzare" ;
|
||||
Eat = regVerb "mangiare" ** {c = []} ;
|
||||
Love = regVerb "amare" ** {c = []} ;
|
||||
Talk = regVerb "parlare" ** {c = "di"} ;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user