-
Notifications
You must be signed in to change notification settings - Fork 0
/
dcg.pl
205 lines (156 loc) · 6.85 KB
/
dcg.pl
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
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
% Num - number agreement (singular or plural)
% Gp - grammatical person (1, 2 ,or 3)
% Gr - grammatical role (subject or object)
% Gvc - grammatical verb category (tv or iv)
% Ay - animacy of subject (animate or inanimate)
s(s(NP, VP)) --> np(NP, Num, subject, Gp, Ay), vp(VP, Num, Gp, Ay).
np(np(PRO), Num, Gr, Gp, _) --> pro(PRO, Num, Gr, Gp).
np(np(DET, NBAR), Num, _, _, Ay) --> det(DET, Num), nbar(NBAR, Num, Ay).
np(np(DET, NBAR, PP), Num, _, _, Ay) --> det(DET, Num), nbar(NBAR, Num, Ay), pp(PP, _).
vp(vp(V, NP), Num, Gp, Ay) --> v(V, Num, Gp, tv, Ay), np(NP, _, object, Gp, _).
vp(vp(V), Num, Gp, Ay) --> v(V, Num, Gp, iv, Ay).
nbar(nbar(N), Num, Ay) --> n(N, Num, Ay).
nbar(nbar(JP), Num, Ay) --> jp(JP, Num, Ay).
jp(jp(ADJ, N), Num, Ay) --> adj(ADJ), n(N, Num, Ay).
jp(jp(ADJ, JP), Num, Ay) --> adj(ADJ), jp(JP, Num, Ay).
pp(pp(PREP, NP), Num) --> prep(PREP), np(NP, Num, _, _, _).
pro(pro(Word), Num, Gr, Gp) --> [Word], {lex(Word, pro, Num, Gp, Gr)}.
v(v(Word), Num, Gp, Gvc, Ay) --> [Word], {lex(Word, Gvc, Num, Gp, Ay)}.
n(n(Word), Num, Ay) --> [Word], {lex(Word, n, Num, Ay)}.
det(det(Word), Num) --> [Word], {lex(Word, det, Num)}.
prep(prep(Word)) --> [Word], {lex(Word, prep)}.
adj(adj(Word)) --> [Word], {lex(Word, adj)}.
% word, grammatical category (pronoun), number (singular/plural), grammatical person (1st, 2nd, or 3rd), and grammatical role (subject or object)
lex(i, pro, singular, 1, subject).
lex(you, pro, singular, 2, subject).
lex(he, pro, singular, 3, subject).
lex(she, pro, singular, 3, subject).
lex(it, pro, singular, 3, subject).
lex(we, pro, plural, 1, subject).
lex(you, pro, plural, 2, subject).
lex(they, pro, plural, 3, subject).
lex(me, pro, singular, 1, object).
lex(you, pro, singular, 2, object).
lex(him, pro, singular, 3, object).
lex(her, pro, singular, 3, object).
lex(it, pro, singular, 3, object).
lex(us, pro, plural, 1, object).
lex(you, pro, plural, 2, object).
lex(them, pro, plural, 3, object).
% word, grammatical category (verb), number (singular/plural),
% grammatical person (1st, 2nd, 3rd), animacy of subject (animate/inanimate)
lex(know, tv, singular, 1, animate).
lex(know, tv, singular, 2, animate).
lex(knows, tv, singular, 3, animate).
lex(know, tv, plural, _, animate).
lex(see, tv, singular, 1, animate).
lex(see, tv, singular, 2, animate).
lex(sees, tv, singular, 3, animate).
lex(see, tv, plural, _, animate).
lex(hire, tv, singular, 1, _).
lex(hire, tv, singular, 2, _).
lex(hires, tv, singular, 3, _).
lex(hire, tv, plural, _, _).
lex(fall, iv, singular, 1, _).
lex(fall, iv, singular, 2, _).
lex(falls, iv, singular, 3, _).
lex(fall, iv, plural, _, _).
lex(sleep, iv, singular, 1, animate).
lex(sleep, iv, singular, 2, animate).
lex(sleeps, iv, singular, 3, animate).
lex(sleep, iv, plural, _, animate).
% word, grammatical category (determiner), number
lex(a, det, singular).
lex(the, det, _).
lex(two, det, plural).
% word, grammatical category (noun), number
lex(man, n, singular, animate).
lex(woman, n, singular, animate).
lex(apple, n, singular, inanimate).
lex(chair, n, singular, inanimate).
lex(room, n, singular, inanimate).
lex(men, n, plural, animate).
lex(women, n, plural, animate).
lex(apples, n, plural, inanimate).
lex(chairs, n, plural, inanimate).
lex(rooms, n, plural, inanimate).
% word, grammatical category (preposition)
lex(on, prep).
lex(in, prep).
lex(under, prep).
% word, grammatical category (adjective)
lex(old, adj).
lex(young, adj).
lex(red, adj).
lex(short, adj).
lex(tall, adj).
% Test sentences
% 1. ?- s(Tree,[the, woman, sees, the, apples],[]).
% Tree = s(np(det(the), nbar(n(woman))), vp(v(sees), np(det(the), nbar(n(apples)))))
% 2. ?- s(Tree,[a, woman, knows, him],[]).
% Tree = s(np(det(a), nbar(n(woman))), vp(v(knows), np(pro(him))))
% 3. ?- s(Tree,[two, woman, hires,a,man],[]).
% false.
% 4. ?- s(Tree,[two, women, hire,a,man],[]).
% Tree = s(np(det(two), nbar(n(women))), vp(v(hire), np(det(a), nbar(n(man))))) .
% 5. ?- s(Tree,[she, knows, her],[]).
% Tree = s(np(pro(she)), vp(v(knows), np(pro(her))))
% 6. ?- s(Tree,[she, know, the, man ],[]).
% false.
% 7. ?- s(Tree,[us, see, the, apple],[]).
% false.
% 8. ?- s(Tree,[we, see,the, apple],[]).
% Tree = s(np(pro(we)), vp(v(see), np(det(the), nbar(n(apple)))))
% 9. ?- s(Tree,[i, know, a, short, man],[]).
% Tree = s(np(pro(i)), vp(v(know), np(det(a), nbar(jp(adj(short), n(man))))))
% 10. ?- s(Tree,[he, hires, they],[]).
% false.
% 11. ?- s(Tree, [two, apples, fall], []).
% Tree = s(np(det(two), nbar(n(apples))), vp(v(fall)))
% 12. ?- s(Tree, [the, apple, falls], []).
% Tree = s(np(det(the), nbar(n(apple))), vp(v(falls)))
% 13. ?- s(Tree, [the, apples, fall], []).
% Tree = s(np(det(the), nbar(n(apples))), vp(v(fall)))
% 14. ?- s(Tree, [i, sleep], []).
% Tree = s(np(pro(i)), vp(v(sleep)))
% 15. ?- s(Tree, [you, sleep], []).
% Tree = s(np(pro(you)), vp(v(sleep)))
% 16. ?- s(Tree, [she, sleeps], []).
% Tree = s(np(pro(she)), vp(v(sleeps)))
% 17. ?- s(Tree,[he, sleep],[]).
% false.
% 18. ?- s(Tree,[them, sleep],[]).
% false.
% 19. ?- s(Tree,[a, men, sleep],[]).
% false.
% 20. ?- s(Tree,[the, tall, woman, sees, the, red],[]).
% false.
% 21. ?- s(Tree,[the, young, tall, man, knows, the, old, short, woman],[]).
% Tree = s(np(det(the), nbar(jp(adj(young), jp(adj(tall), n(man))))), vp(v(knows), np(det(the), nbar(jp(adj(old), jp(adj(short), n(woman)))))))
% 22. ?- s(Tree,[a, man, tall, knows, the, short, woman],[]).
% false.
% 23. ?- s(Tree,[a, man, on, a, chair, sees, a, woman, in, a, room],[]).
% Tree = s(np(det(a), nbar(n(man)), pp(prep(on), np(det(a), nbar(n(chair))))), vp(v(sees), np(det(a), nbar(n(woman)), pp(prep(in), np(det(a), nbar(n(room)))))))
% 24. ?- s(Tree,[a, man, on, a, chair, sees, a, woman, a, room, in],[]).
% false.
% 25. ?- s(Tree,[the, tall, young, woman, in, a, room, on, the, chair, in, a, room, in, the, room, sees, the, red, apples, under, the ,chair],[]).
% Tree = s(np(det(the), nbar(jp(adj(tall), jp(adj(young), n(woman)))), pp(prep(in), np(det(a), nbar(n(room)),
% pp(prep(on), np(det(the), nbar(n(chair)), pp(prep(in), np(det(a), nbar(n(...)), pp(prep(...), np(..., ...))))))))),
% vp(v(sees), np(det(the), nbar(jp(adj(red), n(apples))), pp(prep(under), np(det(the), nbar(n(chair)))))))
% Extra Credit - Test sentences on animacy
% 1. ?- s(Tree,[the, woman, sees, the, apples],[]).
% Tree = s(np(det(the), nbar(n(woman))), vp(v(sees), np(det(the), nbar(n(apples))))) .
% 2. ?- s(Tree,[a, woman, knows, him],[]).
% Tree = s(np(det(a), nbar(n(woman))), vp(v(knows), np(pro(him)))) .
% 3. ?- s(Tree,[the, man, sleeps],[]).
% Tree = s(np(det(the), nbar(n(man))), vp(v(sleeps))) .
% 4. ?- s(Tree,[the, room, sleeps],[]).
% false.
% 5. ?- s(Tree,[the, apple, sees, the, chair],[]).
% false.
% 6. ?- s(Tree,[the, rooms, know, the, man],[]).
% false.
% 7. ?- s(Tree,[the, apple, falls],[]).
% Tree = s(np(det(the), nbar(n(apple))), vp(v(falls))) .
% 8. ?- s(Tree,[the, man, falls],[]).
% Tree = s(np(det(the), nbar(n(man))), vp(v(falls))) .