-
Notifications
You must be signed in to change notification settings - Fork 0
/
UnificationTest.py
88 lines (85 loc) · 2.81 KB
/
UnificationTest.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
from nltk.grammar import FeatureGrammar
from nltk import FeatureChartParser
ugrammar = FeatureGrammar.fromstring("""\
# ###################
# Grammar Productions
# ###################
# Sentence expansion productions
S -> NP[NUM=?n] VP[FORM=?f, NUM=?n, SUBCAT=nil] | AdvC S | WH Aux[NUM=?n] NP[NUM=?n] VP[FORM=?f, NUM=pl, SUBCAT=nil]
# Noun Phrase expansion productions
NP[NUM=?n] -> ProperNoun[NUM=?n] | ProNoun[NUM=?n] | Nominal | DET[NUM=?n] Nominal
NP[NUM=pl] -> NP[NUM=?n] CP
# Verb Phrase expansion productions
VP[FORM=?f, NUM=?n, SUBCAT=?rest] -> VP[FORM=?f, NUM=?n, SUBCAT=[HEAD=?arg, TAIL=?rest]] ARG[CAT=?arg]
VP[FORM=?f, NUM=?n, SUBCAT=?args] -> V[FORM=?f, NUM=?n, SUBCAT=?args] | ADV V[FORM=?f, NUM=?n, SUBCAT=?args]
# Argument expansion productions
ARG[CAT=np] -> NP[NUM=?n]
ARG[CAT=pp] -> PP
ARG[CAT=subc] -> S
# Conjunction Phrase expansion productions
CP -> CONJ NP[NUM=?n]
# Prepositional Phrase expansion productions
PP -> Prep NP[NUM=?n]
AdvC -> CONJ S
# Nominal expansion productions
Nominal -> NOUN[NUM=?n] | Nominal PP | ADJ Nominal | Nominal NOUN[NUM=?n]
# ###################
# Lexical Productions
# ###################
# Determiners and Nouns
DET[NUM=sg] -> 'a'
DET[NUM=?n] -> 'the'
ProperNoun[NUM=sg] -> 'Bart' | 'Homer' | 'Lisa'
ProNoun[NUM=sg] -> 'he'
NOUN[NUM=sg] -> 'milk' | 'salad' | 'kitchen' | 'midnight' | 'table' | 'bread'
NOUN[NUM=pl] -> 'shoes'
# Conjunctions and Adverbs
CONJ -> 'and' | 'when'
ADV -> 'always' | 'never'
# Auxiliaries
Aux[NUM=pl] -> 'do'
Aux[NUM=sg] -> 'does'
# ###################
# Verbs
# VI
V[FORM=base, NUM=pl, SUBCAT=nil] -> 'laugh'
V[FORM=vbz, NUM=sg, SUBCAT=nil] -> 'laughs'
V[FORM=pret, NUM=?n, SUBCAT=nil] -> 'laughed'
# VT + NP
V[FORM=base, NUM=pl, SUBCAT=[HEAD=np, TAIL=nil]] -> 'drink' | 'wear'
V[FORM=vbz, NUM=sg, SUBCAT=[HEAD=np, TAIL=nil]] -> 'wears' | 'serves' | 'drinks'
V[FORM=vbz, NUM=sg, SUBCAT=[HEAD=subc, TAIL=nil]] -> 'thinks'
# VT + NP + PP
V[FORM=vbz, NUM=sg, SUBCAT=[HEAD=np, TAIL=[HEAD=pp, TAIL=nil]]] -> 'serves' | 'drinks' | 'puts'
# VT + NP + NP
V[FORM=vbz, NUM=sg, SUBCAT=[HEAD=np, TAIL=[HEAD=np, TAIL=nil]]] -> 'serves'
# Adjectives, Prepositions and WHs
ADJ -> 'blue' | 'healthy' | 'green'
Prep -> 'in' | 'before' | 'on'
WH -> 'when'
""")
uparser = FeatureChartParser(ugrammar)
text = """\
Bart laughs
Homer laughed
Bart and Lisa drink milk
Bart wears blue shoes
Lisa serves Bart a healthy green salad
Homer serves Lisa
Bart always drinks milk
Lisa thinks Homer thinks Bart drinks milk
Homer never drinks milk in the kitchen before midnight
when Homer drinks milk Bart laughs
when does Lisa drink the milk on the table
when do Lisa and Bart wear shoes
Bart laugh
when do Homer drinks milk
Bart laughs the kitchen
he puts bread on the table
"""
sents = text.splitlines()
for sent in sents:
parses = uparser.parse(sent.split())
print(sent)
for tree in parses:
print(tree)