1
|
1 |
|
import json |
2
|
|
|
|
3
|
1 |
|
from ppp_questionparsing_grammatical import computeTree, simplify, DependenciesTree,\ |
4
|
|
|
QuotationHandler, normalFormProduction, GrammaticalError, NamedEntityMerging, PrepositionMerging |
5
|
1 |
|
import data |
6
|
|
|
|
7
|
1 |
|
from unittest import TestCase |
8
|
|
|
|
9
|
1 |
|
class StandardTripleTests(TestCase): |
10
|
|
|
|
11
|
1 |
View Code Duplication |
def testAndnormalFormProduction(self): |
|
|
|
|
12
|
1 |
|
tree = computeTree(data.give_chief()) |
13
|
1 |
|
NamedEntityMerging(tree).merge() |
14
|
1 |
|
PrepositionMerging(tree).merge() |
15
|
1 |
|
qw = simplify(tree) |
16
|
1 |
|
result = normalFormProduction(tree, qw) |
17
|
1 |
|
self.assertEqual(result, { |
18
|
|
|
"list": [ |
19
|
|
|
{ |
20
|
|
|
"type": "triple", |
21
|
|
|
"subject": { |
22
|
|
|
"type": "resource", |
23
|
|
|
"value": "chief" |
24
|
|
|
}, |
25
|
|
|
"object": { |
26
|
|
|
"type": "missing" |
27
|
|
|
}, |
28
|
|
|
"predicate": { |
29
|
|
|
"list": [ |
30
|
|
|
{ |
31
|
|
|
"type": "resource", |
32
|
|
|
"value": "identity" |
33
|
|
|
} |
34
|
|
|
], |
35
|
|
|
"type": "list" |
36
|
|
|
} |
37
|
|
|
}, |
38
|
|
|
{ |
39
|
|
|
"type": "triple", |
40
|
|
|
"subject": { |
41
|
|
|
"type": "resource", |
42
|
|
|
"value": "prime minister" |
43
|
|
|
}, |
44
|
|
|
"object": { |
45
|
|
|
"type": "missing" |
46
|
|
|
}, |
47
|
|
|
"predicate": { |
48
|
|
|
"list": [ |
49
|
|
|
{ |
50
|
|
|
"type": "resource", |
51
|
|
|
"value": "identity" |
52
|
|
|
} |
53
|
|
|
], |
54
|
|
|
"type": "list" |
55
|
|
|
} |
56
|
|
|
} |
57
|
|
|
], |
58
|
|
|
"type": "intersection" |
59
|
|
|
}) |
60
|
|
|
|
61
|
1 |
View Code Duplication |
def testSuperlativenormalFormProduction(self): |
|
|
|
|
62
|
1 |
|
tree = computeTree(data.give_opera()) |
63
|
1 |
|
NamedEntityMerging(tree).merge() |
64
|
1 |
|
PrepositionMerging(tree).merge() |
65
|
1 |
|
qw = simplify(tree) |
66
|
1 |
|
result = normalFormProduction(tree, qw) |
67
|
1 |
|
self.assertEqual(result, { |
68
|
|
|
"list": [ |
69
|
|
|
{ |
70
|
|
|
"list": { |
71
|
|
|
"predicate": { |
72
|
|
|
"value": "default", |
73
|
|
|
"type": "resource" |
74
|
|
|
}, |
75
|
|
|
"list": { |
76
|
|
|
"value": "Gilbert", |
77
|
|
|
"type": "resource" |
78
|
|
|
}, |
79
|
|
|
"type": "sort" |
80
|
|
|
}, |
81
|
|
|
"type": "first" |
82
|
|
|
}, |
83
|
|
|
{ |
84
|
|
|
"list": { |
85
|
|
|
"predicate": { |
86
|
|
|
"value": "default", |
87
|
|
|
"type": "resource" |
88
|
|
|
}, |
89
|
|
|
"list": { |
90
|
|
|
"value": "Sullivan opera", |
91
|
|
|
"type": "resource" |
92
|
|
|
}, |
93
|
|
|
"type": "sort" |
94
|
|
|
}, |
95
|
|
|
"type": "first" |
96
|
|
|
} |
97
|
|
|
], |
98
|
|
|
"type": "intersection" |
99
|
|
|
}) |
100
|
|
|
|
101
|
1 |
|
def testnormalFormProduction1(self): |
102
|
1 |
|
tree = computeTree(data.give_president_of_USA()) |
103
|
1 |
|
NamedEntityMerging(tree).merge() |
104
|
1 |
|
PrepositionMerging(tree).merge() |
105
|
1 |
|
qw = simplify(tree) |
106
|
1 |
|
result = normalFormProduction(tree, qw) |
107
|
1 |
|
self.assertEqual(result, { |
108
|
|
|
"object": { |
109
|
|
|
"type": "missing" |
110
|
|
|
}, |
111
|
|
|
"subject": { |
112
|
|
|
"type": "resource", |
113
|
|
|
"value": "United States" |
114
|
|
|
}, |
115
|
|
|
"predicate": { |
116
|
|
|
"type": "resource", |
117
|
|
|
"value": "president" |
118
|
|
|
}, |
119
|
|
|
"type": "triple" |
120
|
|
|
}) |
121
|
|
|
|
122
|
1 |
|
def testnormalFormProduction2(self): |
123
|
1 |
|
handler = QuotationHandler('foo') |
124
|
1 |
|
sentence = 'Who wrote "Lucy in the Sky with Diamonds" and "Let It Be"?' |
125
|
1 |
|
nonAmbiguousSentence = handler.pull(sentence) |
126
|
1 |
|
result=data.give_LSD_LIB() |
127
|
1 |
|
tree=computeTree(result) |
128
|
1 |
|
handler.push(tree) |
129
|
1 |
|
NamedEntityMerging(tree).merge() |
130
|
1 |
|
PrepositionMerging(tree).merge() |
131
|
1 |
|
qw = simplify(tree) |
132
|
1 |
|
result = normalFormProduction(tree, qw) |
133
|
1 |
|
self.assertEqual(result, { |
134
|
|
|
"list": [ |
135
|
|
|
{ |
136
|
|
|
"inverse-predicate": { |
137
|
|
|
"list": [ |
138
|
|
|
{ |
139
|
|
|
"value": "author", |
140
|
|
|
"type": "resource" |
141
|
|
|
}, |
142
|
|
|
{ |
143
|
|
|
"value": "writer", |
144
|
|
|
"type": "resource" |
145
|
|
|
} |
146
|
|
|
], |
147
|
|
|
"type": "list" |
148
|
|
|
}, |
149
|
|
|
"subject": { |
150
|
|
|
"type": "missing" |
151
|
|
|
}, |
152
|
|
|
"object": { |
153
|
|
|
"value": "Lucy in the Sky with Diamonds", |
154
|
|
|
"type": "resource" |
155
|
|
|
}, |
156
|
|
|
"type": "triple", |
157
|
|
|
"predicate": { |
158
|
|
|
"list": [ |
159
|
|
|
{ |
160
|
|
|
"value": "written", |
161
|
|
|
"type": "resource" |
162
|
|
|
}, |
163
|
|
|
{ |
164
|
|
|
"value": "literary works", |
165
|
|
|
"type": "resource" |
166
|
|
|
}, |
167
|
|
|
{ |
168
|
|
|
"value": "bibliography", |
169
|
|
|
"type": "resource" |
170
|
|
|
}, |
171
|
|
|
{ |
172
|
|
|
"value": "notable work", |
173
|
|
|
"type": "resource" |
174
|
|
|
} |
175
|
|
|
], |
176
|
|
|
"type": "list" |
177
|
|
|
} |
178
|
|
|
}, |
179
|
|
|
{ |
180
|
|
|
"inverse-predicate": { |
181
|
|
|
"list": [ |
182
|
|
|
{ |
183
|
|
|
"value": "author", |
184
|
|
|
"type": "resource" |
185
|
|
|
}, |
186
|
|
|
{ |
187
|
|
|
"value": "writer", |
188
|
|
|
"type": "resource" |
189
|
|
|
} |
190
|
|
|
], |
191
|
|
|
"type": "list" |
192
|
|
|
}, |
193
|
|
|
"subject": { |
194
|
|
|
"type": "missing" |
195
|
|
|
}, |
196
|
|
|
"object": { |
197
|
|
|
"value": "Let It Be", |
198
|
|
|
"type": "resource" |
199
|
|
|
}, |
200
|
|
|
"type": "triple", |
201
|
|
|
"predicate": { |
202
|
|
|
"list": [ |
203
|
|
|
{ |
204
|
|
|
"value": "written", |
205
|
|
|
"type": "resource" |
206
|
|
|
}, |
207
|
|
|
{ |
208
|
|
|
"value": "literary works", |
209
|
|
|
"type": "resource" |
210
|
|
|
}, |
211
|
|
|
{ |
212
|
|
|
"value": "bibliography", |
213
|
|
|
"type": "resource" |
214
|
|
|
}, |
215
|
|
|
{ |
216
|
|
|
"value": "notable work", |
217
|
|
|
"type": "resource" |
218
|
|
|
} |
219
|
|
|
], |
220
|
|
|
"type": "list" |
221
|
|
|
} |
222
|
|
|
} |
223
|
|
|
], |
224
|
|
|
"type": "intersection" |
225
|
|
|
}) |
226
|
|
|
|
227
|
1 |
|
def testnormalFormProduction3(self): |
228
|
1 |
|
tree = computeTree(data.give_obama_president_usa()) |
229
|
1 |
|
NamedEntityMerging(tree).merge() |
230
|
1 |
|
PrepositionMerging(tree).merge() |
231
|
1 |
|
qw = simplify(tree) |
232
|
1 |
|
result = normalFormProduction(tree, qw) |
233
|
1 |
|
self.assertEqual(result, { |
234
|
|
|
"type": "intersection", |
235
|
|
|
"list": [ |
236
|
|
|
{ |
237
|
|
|
"predicate": { |
238
|
|
|
"type": "list", |
239
|
|
|
"list": [ |
240
|
|
|
{ |
241
|
|
|
"type": "resource", |
242
|
|
|
"value": "been" |
243
|
|
|
}, |
244
|
|
|
{ |
245
|
|
|
"type": "resource", |
246
|
|
|
"value": "identity" |
247
|
|
|
} |
248
|
|
|
] |
249
|
|
|
}, |
250
|
|
|
"type": "triple", |
251
|
|
|
"subject": { |
252
|
|
|
"type": "resource", |
253
|
|
|
"value": "Obama" |
254
|
|
|
}, |
255
|
|
|
"inverse-predicate": { |
256
|
|
|
"type": "resource", |
257
|
|
|
"value": "identity" |
258
|
|
|
}, |
259
|
|
|
"object": { |
260
|
|
|
"type": "missing" |
261
|
|
|
} |
262
|
|
|
}, |
263
|
|
|
{ |
264
|
|
|
"predicate": { |
265
|
|
|
"type": "list", |
266
|
|
|
"list": [ |
267
|
|
|
{ |
268
|
|
|
"type": "resource", |
269
|
|
|
"value": "been" |
270
|
|
|
}, |
271
|
|
|
{ |
272
|
|
|
"type": "resource", |
273
|
|
|
"value": "identity" |
274
|
|
|
} |
275
|
|
|
] |
276
|
|
|
}, |
277
|
|
|
"type": "triple", |
278
|
|
|
"subject": { |
279
|
|
|
"type": "resource", |
280
|
|
|
"value": "United States president" |
281
|
|
|
}, |
282
|
|
|
"inverse-predicate": { |
283
|
|
|
"type": "resource", |
284
|
|
|
"value": "identity" |
285
|
|
|
}, |
286
|
|
|
"object": { |
287
|
|
|
"type": "missing" |
288
|
|
|
} |
289
|
|
|
} |
290
|
|
|
] |
291
|
|
|
}) |
292
|
|
|
|
293
|
1 |
View Code Duplication |
def testnormalFormProductionR8(self): |
|
|
|
|
294
|
1 |
|
tree = computeTree(data.mistake()) |
295
|
1 |
|
NamedEntityMerging(tree).merge() |
296
|
1 |
|
PrepositionMerging(tree).merge() |
297
|
1 |
|
qw = simplify(tree) |
298
|
1 |
|
result = normalFormProduction(tree, qw) |
299
|
1 |
|
self.assertEqual(result, { |
300
|
|
|
"type": "triple", |
301
|
|
|
"subject": { |
302
|
|
|
"type": "resource", |
303
|
|
|
"value": "mistake" |
304
|
|
|
}, |
305
|
|
|
"object": { |
306
|
|
|
"type": "missing" |
307
|
|
|
}, |
308
|
|
|
"predicate": { |
309
|
|
|
"type": "list", |
310
|
|
|
"list": [ |
311
|
|
|
{ |
312
|
|
|
"type": "resource", |
313
|
|
|
"value": "place" |
314
|
|
|
}, |
315
|
|
|
{ |
316
|
|
|
"type": "resource", |
317
|
|
|
"value": "location" |
318
|
|
|
}, |
319
|
|
|
{ |
320
|
|
|
"type": "resource", |
321
|
|
|
"value": "residence" |
322
|
|
|
}, |
323
|
|
|
{ |
324
|
|
|
"type": "resource", |
325
|
|
|
"value": "country" |
326
|
|
|
}, |
327
|
|
|
{ |
328
|
|
|
"type": "resource", |
329
|
|
|
"value": "city" |
330
|
|
|
}, |
331
|
|
|
{ |
332
|
|
|
"type": "resource", |
333
|
|
|
"value": "town" |
334
|
|
|
}, |
335
|
|
|
{ |
336
|
|
|
"type": "resource", |
337
|
|
|
"value": "state" |
338
|
|
|
}, |
339
|
|
|
{ |
340
|
|
|
"type": "resource", |
341
|
|
|
"value": "locality" |
342
|
|
|
} |
343
|
|
|
] |
344
|
|
|
} |
345
|
|
|
}) |
346
|
|
|
|
347
|
|
|
|
348
|
1 |
|
def testnormalFormProductionSuperl(self): |
349
|
1 |
|
tree = computeTree(data.tanzania()) |
350
|
1 |
|
NamedEntityMerging(tree).merge() |
351
|
1 |
|
PrepositionMerging(tree).merge() |
352
|
1 |
|
qw = simplify(tree) |
353
|
1 |
|
result = normalFormProduction(tree, qw) |
354
|
1 |
|
self.assertEqual(result, { |
355
|
|
|
"list": { |
356
|
|
|
"list": { |
357
|
|
|
"object": { |
358
|
|
|
"type": "missing" |
359
|
|
|
}, |
360
|
|
|
"predicate": { |
361
|
|
|
"value": "mountain", |
362
|
|
|
"type": "resource" |
363
|
|
|
}, |
364
|
|
|
"subject": { |
365
|
|
|
"value": "Tanzania", |
366
|
|
|
"type": "resource" |
367
|
|
|
}, |
368
|
|
|
"type": "triple" |
369
|
|
|
}, |
370
|
|
|
"predicate": { |
371
|
|
|
"value" : "height", |
372
|
|
|
"type" : "resource" |
373
|
|
|
}, |
374
|
|
|
"type": "sort" |
375
|
|
|
}, |
376
|
|
|
"type": "last" |
377
|
|
|
}) |
378
|
|
|
|
379
|
1 |
|
def testnormalFormProductionSuperl2(self): |
380
|
1 |
|
tree = computeTree(data.car()) |
381
|
1 |
|
NamedEntityMerging(tree).merge() |
382
|
1 |
|
PrepositionMerging(tree).merge() |
383
|
1 |
|
qw = simplify(tree) |
384
|
1 |
|
result = normalFormProduction(tree, qw) |
385
|
1 |
|
self.assertEqual(result, { |
386
|
|
|
"list": { |
387
|
|
|
"list": { |
388
|
|
|
"subject": { |
389
|
|
|
"value": "world", |
390
|
|
|
"type": "resource" |
391
|
|
|
}, |
392
|
|
|
"predicate": { |
393
|
|
|
"value": "car", |
394
|
|
|
"type": "resource" |
395
|
|
|
}, |
396
|
|
|
"object": { |
397
|
|
|
"type": "missing" |
398
|
|
|
}, |
399
|
|
|
"type": "triple" |
400
|
|
|
}, |
401
|
|
|
"predicate": { |
402
|
|
|
"value" : "cost", |
403
|
|
|
"type" : "resource" |
404
|
|
|
}, |
405
|
|
|
"type": "sort" |
406
|
|
|
}, |
407
|
|
|
"type": "last" |
408
|
|
|
}) |
409
|
|
|
|
410
|
1 |
|
def testCop(self): |
411
|
1 |
|
tree = computeTree(data.black()) |
412
|
1 |
|
NamedEntityMerging(tree).merge() |
413
|
1 |
|
PrepositionMerging(tree).merge() |
414
|
1 |
|
self.assertRaises(GrammaticalError, lambda: simplify(tree)) |
415
|
|
|
|
416
|
1 |
|
def testExists(self): |
417
|
1 |
|
tree = computeTree(data.king_england()) |
418
|
1 |
|
NamedEntityMerging(tree).merge() |
419
|
1 |
|
PrepositionMerging(tree).merge() |
420
|
1 |
|
qw = simplify(tree) |
421
|
1 |
|
result = normalFormProduction(tree, qw) |
422
|
1 |
|
self.assertEqual(result, { |
423
|
|
|
"list": { |
424
|
|
|
"predicate": { |
425
|
|
|
"type": "resource", |
426
|
|
|
"value": "king" |
427
|
|
|
}, |
428
|
|
|
"subject": { |
429
|
|
|
"type": "resource", |
430
|
|
|
"value": "England" |
431
|
|
|
}, |
432
|
|
|
"type": "triple", |
433
|
|
|
"object": { |
434
|
|
|
"type": "missing" |
435
|
|
|
} |
436
|
|
|
}, |
437
|
|
|
"type": "exists" |
438
|
|
|
}) |
439
|
|
|
|
440
|
1 |
|
def testSemiQuestionWord1(self): |
441
|
1 |
|
tree = computeTree(data.roald()) |
442
|
1 |
|
NamedEntityMerging(tree).merge() |
443
|
1 |
|
PrepositionMerging(tree).merge() |
444
|
1 |
|
qw = simplify(tree) |
445
|
1 |
|
result = normalFormProduction(tree, qw) |
446
|
1 |
|
self.assertEqual(result, { |
447
|
|
|
"subject": { |
448
|
|
|
"value": "Roald Dahl", |
449
|
|
|
"type": "resource" |
450
|
|
|
}, |
451
|
|
|
"type": "triple", |
452
|
|
|
"predicate": { |
453
|
|
|
"value": "book", |
454
|
|
|
"type": "resource" |
455
|
|
|
}, |
456
|
|
|
"object": { |
457
|
|
|
"type": "missing" |
458
|
|
|
} |
459
|
|
|
}) |
460
|
|
|
|
461
|
1 |
|
def testSemiQuestionWord3(self): |
462
|
1 |
|
tree = computeTree(data.list_president2()) |
463
|
1 |
|
NamedEntityMerging(tree).merge() |
464
|
1 |
|
PrepositionMerging(tree).merge() |
465
|
1 |
|
qw = simplify(tree) |
466
|
1 |
|
result = normalFormProduction(tree, qw) |
467
|
1 |
|
self.assertEqual(result, { |
468
|
|
|
"type": "triple", |
469
|
|
|
"object": { |
470
|
|
|
"type": "missing" |
471
|
|
|
}, |
472
|
|
|
"predicate": { |
473
|
|
|
"type": "resource", |
474
|
|
|
"value": "president" |
475
|
|
|
}, |
476
|
|
|
"subject": { |
477
|
|
|
"type": "resource", |
478
|
|
|
"value": "France" |
479
|
|
|
} |
480
|
|
|
}) |
481
|
|
|
|
482
|
1 |
|
def testSemiQuestionWord4(self): |
483
|
1 |
|
tree = computeTree(data.capital1()) |
484
|
1 |
|
NamedEntityMerging(tree).merge() |
485
|
1 |
|
PrepositionMerging(tree).merge() |
486
|
1 |
|
qw = simplify(tree) |
487
|
1 |
|
result = normalFormProduction(tree, qw) |
488
|
1 |
|
self.assertEqual(result, { |
489
|
|
|
"predicate": { |
490
|
|
|
"type": "resource", |
491
|
|
|
"value": "capital" |
492
|
|
|
}, |
493
|
|
|
"type": "triple", |
494
|
|
|
"subject": { |
495
|
|
|
"type": "resource", |
496
|
|
|
"value": "France" |
497
|
|
|
}, |
498
|
|
|
"object": { |
499
|
|
|
"type": "missing" |
500
|
|
|
} |
501
|
|
|
}) |
502
|
|
|
|
503
|
1 |
|
def testSemiQuestionWord5(self): |
504
|
1 |
|
tree = computeTree(data.capital2()) |
505
|
1 |
|
NamedEntityMerging(tree).merge() |
506
|
1 |
|
PrepositionMerging(tree).merge() |
507
|
1 |
|
qw = simplify(tree) |
508
|
1 |
|
result = normalFormProduction(tree, qw) |
509
|
1 |
|
self.assertEqual(result, { |
510
|
|
|
"predicate": { |
511
|
|
|
"type": "resource", |
512
|
|
|
"value": "capital" |
513
|
|
|
}, |
514
|
|
|
"type": "triple", |
515
|
|
|
"subject": { |
516
|
|
|
"type": "resource", |
517
|
|
|
"value": "France" |
518
|
|
|
}, |
519
|
|
|
"object": { |
520
|
|
|
"type": "missing" |
521
|
|
|
} |
522
|
|
|
}) |
523
|
|
|
|