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