-
Notifications
You must be signed in to change notification settings - Fork 12
/
test_phraselet_production_EN.py
781 lines (651 loc) · 47.6 KB
/
test_phraselet_production_EN.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
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
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
import unittest
import holmes_extractor as holmes
import os
script_directory = os.path.dirname(os.path.realpath(__file__))
ontology = holmes.Ontology(os.sep.join(
(script_directory, 'test_ontology.owl')))
ontology_holmes_manager = holmes.Manager(model='en_core_web_trf',
perform_coreference_resolution=False,
ontology=ontology,
number_of_workers=1)
ontology_holmes_manager_adm_false = holmes.Manager(model='en_core_web_trf',
perform_coreference_resolution=False,
ontology=ontology,
analyze_derivational_morphology=False,
number_of_workers=1)
symmetric_ontology = holmes.Ontology(os.sep.join((script_directory, 'test_ontology.owl')),
symmetric_matching=True)
symmetric_ontology_nocoref_holmes_manager = holmes.Manager(model='en_core_web_trf',
perform_coreference_resolution=False,
ontology=symmetric_ontology,
number_of_workers=1)
no_ontology_coref_holmes_manager = holmes.Manager(model='en_core_web_trf',
perform_coreference_resolution=True,
number_of_workers=1)
class EnglishPhraseletProductionTest(unittest.TestCase):
def _check_equals(self, manager, text_to_match, phraselet_labels,
replace_with_hypernym_ancestors=True, match_all_words=False,
include_reverse_only=False, process_initial_question_words=False):
manager.remove_all_search_phrases()
doc = manager.semantic_analyzer.parse(text_to_match)
phraselet_labels_to_phraselet_infos = {}
manager.linguistic_object_factory.add_phraselets_to_dict(doc,
phraselet_labels_to_phraselet_infos=phraselet_labels_to_phraselet_infos,
replace_with_hypernym_ancestors=replace_with_hypernym_ancestors,
match_all_words=match_all_words,
ignore_relation_phraselets=False,
include_reverse_only=include_reverse_only,
stop_lemmas=manager.semantic_matching_helper.topic_matching_phraselet_stop_lemmas,
stop_tags=manager.semantic_matching_helper.topic_matching_phraselet_stop_tags,
reverse_only_parent_lemmas=manager.semantic_matching_helper.
topic_matching_reverse_only_parent_lemmas,
words_to_corpus_frequencies=None,
maximum_corpus_frequency=None,
process_initial_question_words=process_initial_question_words)
self.assertEqual(
set(phraselet_labels_to_phraselet_infos.keys()),
set(phraselet_labels))
self.assertEqual(len(phraselet_labels_to_phraselet_infos.keys()),
len(phraselet_labels))
def _get_phraselet_dict(self, manager, text_to_match, words_to_corpus_frequencies=None,
maximum_corpus_frequency=None, match_all_words=True):
manager.remove_all_search_phrases()
doc = manager.semantic_analyzer.parse(text_to_match)
phraselet_labels_to_phraselet_infos = {}
manager.linguistic_object_factory.add_phraselets_to_dict(doc,
phraselet_labels_to_phraselet_infos=phraselet_labels_to_phraselet_infos,
replace_with_hypernym_ancestors=False,
match_all_words=match_all_words,
ignore_relation_phraselets=False,
include_reverse_only=True,
stop_lemmas=manager.semantic_matching_helper.topic_matching_phraselet_stop_lemmas,
stop_tags=manager.semantic_matching_helper.topic_matching_phraselet_stop_tags,
reverse_only_parent_lemmas=manager.semantic_matching_helper.
topic_matching_reverse_only_parent_lemmas,
words_to_corpus_frequencies=words_to_corpus_frequencies,
maximum_corpus_frequency=maximum_corpus_frequency,
process_initial_question_words=False)
return phraselet_labels_to_phraselet_infos
def test_verb_subject_no_entry_in_ontology(self):
self._check_equals(ontology_holmes_manager, "A plant grows",
['predicate-actor: grow-plant', 'word: plant'])
def test_phrasal_verb_subject_no_entry_in_ontology(self):
self._check_equals(ontology_holmes_manager, "A plant grows up quickly",
['governor-adjective: grow up-quick', 'predicate-actor: grow up-plant',
'word: plant'])
def test_phrasal_verb_subject_no_entry_in_ontology_adm_false(self):
self._check_equals(ontology_holmes_manager_adm_false, "A plant grows up quickly",
['governor-adjective: grow up-quickly', 'predicate-actor: grow up-plant',
'word: plant'])
def test_verb_direct_object_no_entry_in_ontology(self):
self._check_equals(ontology_holmes_manager, "A plant is grown",
['predicate-passivesubject: grow-plant', 'word: plant'])
def test_verb_indirect_object_no_entry_in_ontology(self):
self._check_equals(ontology_holmes_manager, "Somebody gives something to a plant",
['predicate-recipient: gift-plant', 'word: plant'])
def test_noun_adjective_no_entry_in_ontology(self):
self._check_equals(ontology_holmes_manager, "A healthy plant",
['governor-adjective: plant-healthy', 'word: plant'])
def test_verb_adverb_no_entry_in_ontology(self):
self._check_equals(ontology_holmes_manager, "They sailed rapidly",
['governor-adjective: sail-rapid'])
def test_verb_adverb_no_entry_in_ontology_adm_false(self):
self._check_equals(ontology_holmes_manager_adm_false, "They sailed rapidly",
['governor-adjective: sail-rapidly'])
def test_noun_noun_no_entry_in_ontology(self):
self._check_equals(ontology_holmes_manager, "A hobby plant",
['noun-noun: plant-hobby', 'word: plant', 'word: hobby'])
def test_possessor_possessed_no_entry_in_ontology(self):
self._check_equals(ontology_holmes_manager, "A gardener's plant",
['word-ofword: plant-gardener', 'word: plant', 'word: gardener'])
def test_combined_no_entry_in_ontology(self):
self._check_equals(ontology_holmes_manager,
"A gardener's healthy hobby plant grows in the sun",
['predicate-actor: grow-plant', 'governor-adjective: plant-healthy',
'noun-noun: plant-hobby', 'word-ofword: plant-gardener',
'prepgovernor-noun: grow-sun', 'word: plant', 'word: hobby', 'word: gardener',
'word: sun'])
def test_class_entry_in_ontology(self):
self._check_equals(ontology_holmes_manager, "A dog progresses",
['predicate-actor: progress-animal', 'word: animal'])
def test_multiword_class_entry_in_ontology(self):
self._check_equals(ontology_holmes_manager, "A small cat creature",
['governor-adjective: animal-small', 'word: animal'])
def test_individual_entry_in_ontology(self):
self._check_equals(ontology_holmes_manager, "Fido progresses",
['predicate-actor: progress-animal', 'word: animal'])
def test_multiword_individual_entry_in_ontology(self):
self._check_equals(ontology_holmes_manager, "Mimi Momo progresses",
['predicate-actor: progress-animal', 'word: animal'])
def test_class_entry_in_ontology_no_hypernym_replacement(self):
self._check_equals(ontology_holmes_manager, "A dog progresses",
['predicate-actor: progress-dog', 'word: dog'], False)
def test_multiword_class_entry_in_ontology_no_hypernym_replacement(self):
self._check_equals(ontology_holmes_manager, "A small cat creature",
['governor-adjective: cat creature-small', 'word: cat creature'], False)
def test_individual_entry_in_ontology_no_hypernym_replacement(self):
self._check_equals(ontology_holmes_manager, "Fido progresses",
['predicate-actor: progress-fido', 'word: fido'], False)
def test_multiword_individual_entry_in_ontology_no_hypernym_replacement(self):
self._check_equals(ontology_holmes_manager, "Mimi Momo progresses",
['predicate-actor: progress-mimi momo', 'word: mimi momo'], False)
def test_multiword_in_ontology_no_hypernym(self):
self._check_equals(ontology_holmes_manager, "School gear progresses",
['predicate-actor: progress-school gear', 'word: school gear'])
def test_multiword_not_in_ontology(self):
self._check_equals(ontology_holmes_manager,
"Information extraction progresses with information",
['predicate-actor: progress-extract', 'noun-noun: extract-inform',
'prepgovernor-noun: progress-inform', 'word: inform', 'word: extract'])
def test_multiword_not_in_ontology_analyze_derivational_morphology_false(self):
self._check_equals(ontology_holmes_manager_adm_false,
"Information extraction progresses with information",
['predicate-actor: progress-extraction', 'noun-noun: extraction-information',
'prepgovernor-noun: progress-information', 'word: information', 'word: extraction'])
def test_text_in_ontology_lemma_not_in_ontology(self):
self._check_equals(ontology_holmes_manager,
"He saw rainbows",
['predicate-patient: see-arc', 'word: arc'])
def test_text_in_ontology_lemma_not_in_ontology_no_hypernym_replacement(self):
self._check_equals(ontology_holmes_manager,
"He saw rainbows",
['predicate-patient: see-rainbows', 'word: rainbows'], False)
def test_class_entry_in_ontology_symmetric_ontology(self):
self._check_equals(symmetric_ontology_nocoref_holmes_manager, "A dog progresses",
['predicate-actor: progress-animal', 'word: animal'])
def test_multiword_class_entry_in_ontology_symmetric_ontology(self):
self._check_equals(symmetric_ontology_nocoref_holmes_manager, "A small cat creature",
['governor-adjective: animal-small', 'word: animal'])
def test_individual_entry_in_ontology_symmetric_ontology(self):
self._check_equals(symmetric_ontology_nocoref_holmes_manager, "Fido progresses",
['predicate-actor: progress-animal', 'word: animal'])
def test_multiword_individual_entry_in_ontology_symmetric_ontology(self):
self._check_equals(symmetric_ontology_nocoref_holmes_manager, "Mimi Momo progresses",
['predicate-actor: progress-animal', 'word: animal'])
def test_class_entry_in_ontology_no_hypernym_replacement_symmetric_ontology(self):
self._check_equals(symmetric_ontology_nocoref_holmes_manager, "A dog progresses",
['predicate-actor: progress-dog', 'word: dog'], False)
def test_multiword_class_entry_in_ontology_no_hypernym_replacement_symmetric_ontology(self):
self._check_equals(symmetric_ontology_nocoref_holmes_manager, "A small cat creature",
['governor-adjective: cat creature-small', 'word: cat creature'], False)
def test_individual_entry_in_ontology_no_hypernym_replacement_symmetric_ontology(self):
self._check_equals(symmetric_ontology_nocoref_holmes_manager, "Fido progresses",
['predicate-actor: progress-fido', 'word: fido'], False)
def test_multiword_individual_entry_in_ontology_no_hypernym_replacement_symm_ontology(self):
self._check_equals(symmetric_ontology_nocoref_holmes_manager, "Mimi Momo progresses",
['predicate-actor: progress-mimi momo', 'word: mimi momo'], False)
def test_multiword_not_in_ontology_symmetric_ontology(self):
self._check_equals(symmetric_ontology_nocoref_holmes_manager, "Information extraction progresses",
['predicate-actor: progress-extract', 'noun-noun: extract-inform',
'word: inform', 'word: extract'])
def test_text_in_ontology_lemma_not_in_ontology_symmetric_ontology(self):
self._check_equals(symmetric_ontology_nocoref_holmes_manager,
"He saw rainbows",
['predicate-patient: see-arc', 'word: arc'])
def test_text_in_ontology_lemma_not_in_ontology_no_hypernym_replacement_symm_ontology(self):
self._check_equals(symmetric_ontology_nocoref_holmes_manager,
"He saw rainbows",
['predicate-patient: see-rainbows', 'word: rainbows'], False)
def test_prepposs(self):
self._check_equals(symmetric_ontology_nocoref_holmes_manager,
"He needs insurance for five years",
['predicate-patient: need-insurance', 'number-noun: year-five',
'prepgovernor-noun: need-year', 'prepgovernor-noun: insurance-year',
'word: insurance', 'word: year'], False)
def test_reverse_only(self):
self._check_equals(symmetric_ontology_nocoref_holmes_manager,
"He needs insurance for five years",
['predicate-patient: need-insurance', 'number-noun: year-five',
'prepgovernor-noun: need-year', 'prepgovernor-noun: insurance-year',
'word: insurance', 'word: year', 'prep-noun: for-year'], False,
include_reverse_only=True)
def test_coref(self):
self._check_equals(no_ontology_coref_holmes_manager,
"I saw a dog. He was chasing a cat and a cat",
['predicate-patient: see-dog', 'predicate-actor: chase-dog',
'predicate-patient: chase-cat', 'word: dog', 'word: cat'])
def test_reverse_only_parent_lemma(self):
self._check_equals(no_ontology_coref_holmes_manager,
"Always he had it", ['governor-adjective: have-always'], include_reverse_only=True)
def test_reverse_only_parent_lemma_suppressed(self):
self._check_equals(no_ontology_coref_holmes_manager,
"Always he had it", ['word: have', 'word: always'], include_reverse_only=False)
def test_phraselet_stop_words_governed(self):
self._check_equals(no_ontology_coref_holmes_manager,
"So he did it at home", ['word: home', 'prepgovernor-noun: do-home',
'prep-noun: at-home'],
include_reverse_only=True)
def test_phraselet_stop_words_governed_suppressed(self):
self._check_equals(no_ontology_coref_holmes_manager,
"So he did it at home", ['word: home'],
include_reverse_only=False)
def test_question_word(self):
self._check_equals(no_ontology_coref_holmes_manager,
"Who opened the door?",
['head-WHsubj: open-who', 'predicate-patient: open-door', 'word: door'],
process_initial_question_words=True)
def test_question_word_control(self):
self._check_equals(no_ontology_coref_holmes_manager,
"Who opened the door?", ['predicate-patient: open-door', 'word: door'],
process_initial_question_words=False)
def test_coref_and_phraselet_labels(self):
no_ontology_coref_holmes_manager.remove_all_search_phrases()
doc = no_ontology_coref_holmes_manager.semantic_analyzer.parse(
"I saw a dog. He was chasing a cat and a cat")
phraselet_labels_to_phraselet_infos = {}
no_ontology_coref_holmes_manager.linguistic_object_factory.add_phraselets_to_dict(
doc,
phraselet_labels_to_phraselet_infos=phraselet_labels_to_phraselet_infos,
replace_with_hypernym_ancestors=False,
match_all_words=False,
include_reverse_only=False,
ignore_relation_phraselets=False,
stop_lemmas=no_ontology_coref_holmes_manager.
semantic_matching_helper.topic_matching_phraselet_stop_lemmas,
stop_tags=no_ontology_coref_holmes_manager.
semantic_matching_helper.topic_matching_phraselet_stop_tags,
reverse_only_parent_lemmas=no_ontology_coref_holmes_manager.semantic_matching_helper.
topic_matching_reverse_only_parent_lemmas,
words_to_corpus_frequencies=None,
maximum_corpus_frequency=None,
process_initial_question_words=False)
self.assertEqual(set(
phraselet_labels_to_phraselet_infos.keys()),
set(['predicate-patient: see-dog', 'predicate-actor: chase-dog',
'predicate-patient: chase-cat', 'word: dog', 'word: cat']))
def test_only_verb(self):
self._check_equals(ontology_holmes_manager, "jump",
['word: jump'])
def test_only_preposition(self):
self._check_equals(ontology_holmes_manager, "in",
['word: in'])
def test_match_all_words(self):
self._check_equals(no_ontology_coref_holmes_manager,
"I saw a dog. He was chasing a cat and a cat",
['predicate-actor: chase-dog', 'predicate-patient: chase-cat',
'predicate-patient: see-dog', 'word: dog', 'word: cat',
'word: see', 'word: chase'], False, True)
def test_entity_defined_multiword_not_match_all_words(self):
self._check_equals(no_ontology_coref_holmes_manager,
"Richard Paul Hudson came",
['predicate-actor: come-richard paul hudson',
'word: richard paul hudson'], False, False)
def test_entity_defined_multiword_not_match_all_words_with_adjective(self):
self._check_equals(no_ontology_coref_holmes_manager,
"The big Richard Paul Hudson",
['governor-adjective: richard paul hudson-big',
'word: richard paul hudson'], False, False)
def test_ontology_defined_multiword_not_match_all_words_with_adjective(self):
self._check_equals(ontology_holmes_manager,
"The big Mimi Momo",
['governor-adjective: mimi momo-big',
'word: mimi momo'], False, False)
def test_entity_defined_multiword_match_all_words(self):
self._check_equals(no_ontology_coref_holmes_manager,
"Richard Paul Hudson came",
['predicate-actor: come-richard paul hudson',
'word: richard', 'word: paul', 'word: hudson', 'word: come'], False, True)
def test_entity_defined_multiword_match_all_words_with_adjective(self):
self._check_equals(no_ontology_coref_holmes_manager,
"The big Richard Paul Hudson",
['governor-adjective: richard paul hudson-big',
'word: richard', 'word: paul', 'word: hudson', 'word: big'], False, True)
def test_ontology_defined_multiword_match_all_words_with_adjective(self):
self._check_equals(ontology_holmes_manager,
"The big Mimi Momo",
['governor-adjective: mimi momo-big',
'word: mimi', 'word: momo', 'word: big'], False, True)
def test_ontology_and_entity_defined_multiword_match_all_words_with_adjective(self):
self._check_equals(ontology_holmes_manager,
"The big Richard Pranaya Jones",
['governor-adjective: pranaya jones-big', 'noun-noun: pranaya jones-richard',
'word: pranaya', 'word: jones', 'word: richard', 'word: big'], False, True)
def test_ontology_and_entity_defined_multiword_not_match_all_words_with_adjective(self):
self._check_equals(ontology_holmes_manager,
"The big Richard Pranaya Jones",
['governor-adjective: pranaya jones-big', 'noun-noun: pranaya jones-richard',
'word: pranaya jones', 'word: richard'], False, False)
def test_matching_reprs(self):
dict = self._get_phraselet_dict(no_ontology_coref_holmes_manager,
"The sun shone. They had an anonymous discussion.")
word_phraselet_1 = dict['word: sun']
self.assertEqual(word_phraselet_1.parent_direct_matching_reprs, ['sun'])
self.assertEqual(word_phraselet_1.parent_derivation_matching_reprs, None)
self.assertEqual(word_phraselet_1.child_direct_matching_reprs, None)
self.assertEqual(word_phraselet_1.child_derivation_matching_reprs, None)
word_phraselet_2 = dict['word: discuss']
self.assertEqual(word_phraselet_2.parent_direct_matching_reprs, ['discussion'])
self.assertEqual(word_phraselet_2.parent_derivation_matching_reprs, ['discuss'])
self.assertEqual(word_phraselet_2.child_direct_matching_reprs, None)
self.assertEqual(word_phraselet_2.child_derivation_matching_reprs, None)
relation_phraselet_1 = dict['predicate-actor: shine-sun']
self.assertEqual(relation_phraselet_1.parent_direct_matching_reprs, ['shine'])
self.assertEqual(relation_phraselet_1.parent_derivation_matching_reprs, None)
self.assertEqual(relation_phraselet_1.child_direct_matching_reprs, ['sun'])
self.assertEqual(relation_phraselet_1.child_derivation_matching_reprs, None)
relation_phraselet_2 = dict['governor-adjective: discuss-anonymity']
self.assertEqual(relation_phraselet_2.parent_direct_matching_reprs, ['discussion'])
self.assertEqual(relation_phraselet_2.parent_derivation_matching_reprs, ['discuss'])
self.assertEqual(relation_phraselet_2.child_direct_matching_reprs, ['anonymous'])
self.assertEqual(relation_phraselet_2.child_derivation_matching_reprs, ['anonymity'])
def test_noun_lemmas_preferred_noun_lemma_first(self):
dict = self._get_phraselet_dict(no_ontology_coref_holmes_manager,
"They wanted allowances. They wanted to allow it.")
self.assertFalse('word: allowance' in dict)
self.assertFalse('predicate-patient: want-allowance' in dict)
word_phraselet = dict['word: allow']
self.assertEqual(word_phraselet.parent_lemma, 'allowance')
self.assertEqual(word_phraselet.parent_derived_lemma, 'allow')
relation_phraselet = dict['predicate-patient: want-allow']
self.assertEqual(relation_phraselet.child_lemma, 'allowance')
self.assertEqual(relation_phraselet.child_derived_lemma, 'allow')
def test_noun_lemmas_preferred_noun_lemma_second(self):
dict = self._get_phraselet_dict(no_ontology_coref_holmes_manager,
"They wanted to allow it. They wanted allowances.")
self.assertFalse('word: allowance' in dict)
self.assertFalse('predicate-patient: want-allowance' in dict)
word_phraselet = dict['word: allow']
self.assertEqual(word_phraselet.parent_lemma, 'allowance')
self.assertEqual(word_phraselet.parent_derived_lemma, 'allow')
relation_phraselet = dict['predicate-patient: want-allow']
self.assertEqual(relation_phraselet.child_lemma, 'allowance')
self.assertEqual(relation_phraselet.child_derived_lemma, 'allow')
def test_noun_lemmas_preferred_control_1(self):
dict = self._get_phraselet_dict(no_ontology_coref_holmes_manager,
"They wanted to allow it.")
self.assertFalse('word: allowance' in dict)
self.assertFalse('predicate-patient: want-allowance' in dict)
word_phraselet = dict['word: allow']
self.assertEqual(word_phraselet.parent_lemma, 'allow')
self.assertEqual(word_phraselet.parent_derived_lemma, 'allow')
relation_phraselet = dict['predicate-patient: want-allow']
self.assertEqual(relation_phraselet.child_lemma, 'allow')
self.assertEqual(relation_phraselet.child_derived_lemma, 'allow')
def test_noun_lemmas_preferred_control_2(self):
dict = self._get_phraselet_dict(no_ontology_coref_holmes_manager,
"They wanted allowances.")
self.assertFalse('word: allowance' in dict)
self.assertFalse('predicate-patient: want-allowance' in dict)
word_phraselet = dict['word: allow']
self.assertEqual(word_phraselet.parent_lemma, 'allowance')
self.assertEqual(word_phraselet.parent_derived_lemma, 'allow')
relation_phraselet = dict['predicate-patient: want-allow']
self.assertEqual(relation_phraselet.child_lemma, 'allowance')
self.assertEqual(relation_phraselet.child_derived_lemma, 'allow')
def test_shorter_lemmas_preferred_shorter_lemma_first(self):
dict = self._get_phraselet_dict(no_ontology_coref_holmes_manager,
"They discussed behavior. They discussed behaviour.")
self.assertFalse('word: behaviour' in dict)
self.assertFalse('word: behavior' in dict)
self.assertFalse('predicate-patient: discuss-behaviour' in dict)
self.assertFalse('predicate-patient: discuss-behavior' in dict)
word_phraselet = dict['word: behave']
self.assertEqual(word_phraselet.parent_lemma, 'behavior')
self.assertEqual(word_phraselet.parent_derived_lemma, 'behave')
relation_phraselet = dict['predicate-patient: discuss-behave']
self.assertEqual(relation_phraselet.child_lemma, 'behavior')
self.assertEqual(relation_phraselet.child_derived_lemma, 'behave')
def test_shorter_lemmas_preferred_adm_false_control(self):
dict = self._get_phraselet_dict(ontology_holmes_manager_adm_false,
"They discussed behavior. They discussed behaviour.")
self.assertTrue('word: behaviour' in dict)
self.assertTrue('word: behavior' in dict)
self.assertFalse('word: behave' in dict)
self.assertTrue('predicate-patient: discuss-behaviour' in dict)
self.assertTrue('predicate-patient: discuss-behavior' in dict)
self.assertFalse('predicate-patient: discuss-behave' in dict)
word_phraselet = dict['word: behavior']
self.assertEqual(word_phraselet.parent_lemma, 'behavior')
self.assertEqual(word_phraselet.parent_derived_lemma, 'behavior')
relation_phraselet = dict['predicate-patient: discuss-behavior']
self.assertEqual(relation_phraselet.child_lemma, 'behavior')
self.assertEqual(relation_phraselet.child_derived_lemma, 'behavior')
def test_shorter_lemmas_preferred_shorter_lemma_second(self):
dict = self._get_phraselet_dict(no_ontology_coref_holmes_manager,
"They discussed behaviour. They discussed behavior.")
self.assertFalse('word: behaviour' in dict)
self.assertFalse('word: behavior' in dict)
self.assertFalse('predicate-patient: discuss-behaviour' in dict)
self.assertFalse('predicate-patient: discuss-behavior' in dict)
word_phraselet = dict['word: behave']
self.assertEqual(word_phraselet.parent_lemma, 'behavior')
self.assertEqual(word_phraselet.parent_derived_lemma, 'behave')
relation_phraselet = dict['predicate-patient: discuss-behave']
self.assertEqual(relation_phraselet.child_lemma, 'behavior')
self.assertEqual(relation_phraselet.child_derived_lemma, 'behave')
def test_shorter_lemmas_preferred_control(self):
dict = self._get_phraselet_dict(no_ontology_coref_holmes_manager,
"They discussed behaviour. They behaved")
self.assertFalse('word: behaviour' in dict)
self.assertFalse('word: behavior' in dict)
self.assertFalse('predicate-patient: discuss-behaviour' in dict)
self.assertFalse('predicate-patient: discuss-behavior' in dict)
word_phraselet = dict['word: behave']
self.assertEqual(word_phraselet.parent_lemma, 'behaviour')
self.assertEqual(word_phraselet.parent_derived_lemma, 'behave')
relation_phraselet = dict['predicate-patient: discuss-behave']
self.assertEqual(relation_phraselet.child_lemma, 'behaviour')
self.assertEqual(relation_phraselet.child_derived_lemma, 'behave')
def test_reverse_derived_lemmas_in_ontology_one_lemma_1(self):
dict = self._get_phraselet_dict(ontology_holmes_manager,
"He ate moodily")
self.assertFalse('word: moody' in dict)
self.assertFalse('governor-adjective: eat-moody' in dict)
word_phraselet = dict['word: moodiness']
self.assertEqual(word_phraselet.parent_lemma, 'moodily')
self.assertEqual(word_phraselet.parent_derived_lemma, 'moodiness')
relation_phraselet = dict['governor-adjective: eat-moodiness']
self.assertEqual(relation_phraselet.child_lemma, 'moodily')
self.assertEqual(relation_phraselet.child_derived_lemma, 'moodiness')
def test_reverse_derived_lemmas_in_ontology_one_lemma_2(self):
dict = self._get_phraselet_dict(ontology_holmes_manager,
"He offended the cat")
self.assertFalse('word: offend' in dict)
self.assertFalse('predicate-patient: offend-cat' in dict)
word_phraselet = dict['word: offence']
self.assertEqual(word_phraselet.parent_lemma, 'offend')
self.assertEqual(word_phraselet.parent_derived_lemma, 'offence')
relation_phraselet = dict['predicate-patient: offence-cat']
self.assertEqual(relation_phraselet.parent_lemma, 'offend')
self.assertEqual(relation_phraselet.parent_derived_lemma, 'offence')
doc = ontology_holmes_manager.semantic_analyzer.parse(
'He took offense')
ontology_holmes_manager.linguistic_object_factory.add_phraselets_to_dict(doc,
phraselet_labels_to_phraselet_infos=dict,
replace_with_hypernym_ancestors=False,
match_all_words=True,
ignore_relation_phraselets=False,
include_reverse_only=True,
stop_lemmas=ontology_holmes_manager.semantic_matching_helper.topic_matching_phraselet_stop_lemmas,
stop_tags=ontology_holmes_manager.semantic_matching_helper.topic_matching_phraselet_stop_tags,
reverse_only_parent_lemmas=ontology_holmes_manager.semantic_matching_helper.
topic_matching_reverse_only_parent_lemmas,
words_to_corpus_frequencies=None,
maximum_corpus_frequency=None,
process_initial_question_words=False)
word_phraselet = dict['word: offence']
self.assertEqual(word_phraselet.parent_lemma, 'offense')
self.assertEqual(word_phraselet.parent_derived_lemma, 'offence')
doc = ontology_holmes_manager.semantic_analyzer.parse(
'He took offence')
ontology_holmes_manager.linguistic_object_factory.add_phraselets_to_dict(doc,
phraselet_labels_to_phraselet_infos=dict,
replace_with_hypernym_ancestors=False,
match_all_words=True,
ignore_relation_phraselets=False,
include_reverse_only=True,
stop_lemmas=ontology_holmes_manager.semantic_matching_helper.topic_matching_phraselet_stop_lemmas,
stop_tags=ontology_holmes_manager.semantic_matching_helper.topic_matching_phraselet_stop_tags,
reverse_only_parent_lemmas=ontology_holmes_manager.semantic_matching_helper.
topic_matching_reverse_only_parent_lemmas,
words_to_corpus_frequencies=None,
maximum_corpus_frequency=None,
process_initial_question_words=False)
word_phraselet = dict['word: offence']
self.assertEqual(word_phraselet.parent_lemma, 'offense')
self.assertEqual(word_phraselet.parent_derived_lemma, 'offence')
def test_reverse_derived_lemmas_in_ontology_multiword_match_all_words(self):
dict = self._get_phraselet_dict(ontology_holmes_manager,
"He used a waste horse")
self.assertTrue('word: waste' in dict)
self.assertTrue('word: horse' in dict)
relation_phraselet = dict['predicate-patient: use-wastage horse']
self.assertEqual(relation_phraselet.child_lemma, 'wastage horse')
self.assertEqual(
relation_phraselet.child_derived_lemma, 'wastage horse')
def test_reverse_derived_lemmas_in_ontology_multiword_not_match_all_words(self):
dict = self._get_phraselet_dict(ontology_holmes_manager,
"He used a waste horse", match_all_words=False)
self.assertFalse('word: waste horse' in dict)
self.assertFalse('predicate-patient: use-waste horse' in dict)
word_phraselet = dict['word: wastage horse']
self.assertEqual(word_phraselet.parent_lemma, 'wastage horse')
self.assertEqual(word_phraselet.parent_derived_lemma, 'wastage horse')
relation_phraselet = dict['predicate-patient: use-wastage horse']
self.assertEqual(relation_phraselet.child_lemma, 'wastage horse')
self.assertEqual(
relation_phraselet.child_derived_lemma, 'wastage horse')
def test_frequency_factors_small(self):
dict = self._get_phraselet_dict(ontology_holmes_manager,
"The dog chased the cat",
words_to_corpus_frequencies={'dog': 1, 'chasing': 1, 'cat': 2}, maximum_corpus_frequency=5)
dog_phraselet = dict['word: dog']
self.assertEqual(str(dog_phraselet.frequency_factor), '1.0')
cat_phraselet = dict['word: cat']
self.assertEqual(str(cat_phraselet.frequency_factor), '1.0')
chase_phraselet = dict['word: chasing']
self.assertEqual(str(chase_phraselet.frequency_factor), '1.0')
chase_dog_phraselet = dict['predicate-actor: chasing-dog']
self.assertEqual(str(chase_dog_phraselet.frequency_factor), '1.0')
chase_cat_phraselet = dict['predicate-patient: chasing-cat']
self.assertEqual(str(chase_cat_phraselet.frequency_factor), '1.0')
def test_frequency_factors_small_with_small_mcf(self):
dict = self._get_phraselet_dict(ontology_holmes_manager,
"The dog chased the cat",
words_to_corpus_frequencies={'dog': 1, 'chasing': 1, 'cat': 2}, maximum_corpus_frequency=2)
dog_phraselet = dict['word: dog']
self.assertEqual(str(dog_phraselet.frequency_factor), '1.0')
cat_phraselet = dict['word: cat']
self.assertEqual(str(cat_phraselet.frequency_factor), '1.0')
chase_phraselet = dict['word: chasing']
self.assertEqual(str(chase_phraselet.frequency_factor), '1.0')
chase_dog_phraselet = dict['predicate-actor: chasing-dog']
self.assertEqual(str(chase_dog_phraselet.frequency_factor), '1.0')
chase_cat_phraselet = dict['predicate-patient: chasing-cat']
self.assertEqual(str(chase_cat_phraselet.frequency_factor), '1.0')
def test_frequency_factors_large(self):
dict = self._get_phraselet_dict(ontology_holmes_manager,
"The dog chased the cat",
words_to_corpus_frequencies={'dog': 3, 'chasing': 4, 'cat': 5}, maximum_corpus_frequency=5)
dog_phraselet = dict['word: dog']
self.assertEqual(str(dog_phraselet.frequency_factor), '0.5693234419266069')
cat_phraselet = dict['word: cat']
self.assertEqual(str(cat_phraselet.frequency_factor), '0.1386468838532139')
chase_phraselet = dict['word: chasing']
self.assertEqual(str(chase_phraselet.frequency_factor), '0.31739380551401464')
chase_dog_phraselet = dict['predicate-actor: chasing-dog']
self.assertEqual(str(chase_dog_phraselet.frequency_factor), '0.18069973380142287')
chase_cat_phraselet = dict['predicate-patient: chasing-cat']
self.assertEqual(str(chase_cat_phraselet.frequency_factor), '0.044005662088831145')
def test_frequency_factors_large_with_ontology_match(self):
dict = self._get_phraselet_dict(ontology_holmes_manager,
"The dog chased the cat",
words_to_corpus_frequencies={'dog': 2, 'puppy': 4, 'chasing': 4, 'cat': 5}, maximum_corpus_frequency=5)
dog_phraselet = dict['word: dog']
self.assertEqual(str(dog_phraselet.frequency_factor), '0.31739380551401464')
cat_phraselet = dict['word: cat']
self.assertEqual(str(cat_phraselet.frequency_factor), '0.1386468838532139')
chase_phraselet = dict['word: chasing']
self.assertEqual(str(chase_phraselet.frequency_factor), '0.31739380551401464')
chase_dog_phraselet = dict['predicate-actor: chasing-dog']
self.assertEqual(str(chase_dog_phraselet.frequency_factor), '0.10073882777866815')
chase_cat_phraselet = dict['predicate-patient: chasing-cat']
self.assertEqual(str(chase_cat_phraselet.frequency_factor), '0.044005662088831145')
def test_frequency_factors_very_large(self):
dict = self._get_phraselet_dict(ontology_holmes_manager,
"The dog chased the cat",
words_to_corpus_frequencies={'dog': 97, 'chasing': 98, 'cat': 99}, maximum_corpus_frequency=100)
dog_phraselet = dict['word: dog']
self.assertEqual(str(dog_phraselet.frequency_factor), '0.008864383480215898')
cat_phraselet = dict['word: cat']
self.assertEqual(str(cat_phraselet.frequency_factor), '0.0043869621537525605')
chase_phraselet = dict['word: chasing']
self.assertEqual(str(chase_phraselet.frequency_factor), '0.00661413286687762')
chase_dog_phraselet = dict['predicate-actor: chasing-dog']
self.assertEqual(str(chase_dog_phraselet.frequency_factor), '5.863021012110299e-05')
chase_cat_phraselet = dict['predicate-patient: chasing-cat']
self.assertEqual(str(chase_cat_phraselet.frequency_factor), '2.9015950566883042e-05')
def test_ent_types(self):
dict = self._get_phraselet_dict(ontology_holmes_manager,
"The big Richard came home.")
self.assertEqual(dict['word: richard'].parent_ent_type, 'PERSON')
self.assertEqual(dict['predicate-actor: come-richard'].parent_ent_type, '')
self.assertEqual(dict['predicate-actor: come-richard'].child_ent_type, 'PERSON')
self.assertEqual(dict['governor-adjective: richard-big'].parent_ent_type, 'PERSON')
self.assertEqual(dict['governor-adjective: richard-big'].child_ent_type, '')
def test_parent_lemma_replacement(self):
ontology_holmes_manager.remove_all_search_phrases()
ontology_holmes_manager.parse_and_register_document("They discussed loudly", '1')
doc = ontology_holmes_manager.get_document('1')
phraselet_labels_to_phraselet_infos = {}
ontology_holmes_manager.linguistic_object_factory.add_phraselets_to_dict(doc,
phraselet_labels_to_phraselet_infos=phraselet_labels_to_phraselet_infos,
replace_with_hypernym_ancestors=False,
match_all_words=False,
ignore_relation_phraselets=False,
include_reverse_only=False,
stop_lemmas=[],
stop_tags=[],
reverse_only_parent_lemmas=[],
words_to_corpus_frequencies=None,
maximum_corpus_frequency=None,
process_initial_question_words=False)
self.assertEqual(phraselet_labels_to_phraselet_infos['governor-adjective: discuss-loud'].parent_lemma, 'discuss')
ontology_holmes_manager.parse_and_register_document("A loud discussion", '2')
doc = ontology_holmes_manager.get_document('2')
ontology_holmes_manager.linguistic_object_factory.add_phraselets_to_dict(doc,
phraselet_labels_to_phraselet_infos=phraselet_labels_to_phraselet_infos,
replace_with_hypernym_ancestors=False,
match_all_words=False,
ignore_relation_phraselets=False,
include_reverse_only=False,
stop_lemmas=[],
stop_tags=[],
reverse_only_parent_lemmas=[],
words_to_corpus_frequencies=None,
maximum_corpus_frequency=None,
process_initial_question_words=False)
self.assertEqual(phraselet_labels_to_phraselet_infos['governor-adjective: discuss-loud'].parent_lemma, 'discussion')
def test_child_lemma_replacement(self):
ontology_holmes_manager.remove_all_search_phrases()
ontology_holmes_manager.parse_and_register_document("They started to discuss", '3')
doc = ontology_holmes_manager.get_document('3')
phraselet_labels_to_phraselet_infos = {}
ontology_holmes_manager.linguistic_object_factory.add_phraselets_to_dict(doc,
phraselet_labels_to_phraselet_infos=phraselet_labels_to_phraselet_infos,
replace_with_hypernym_ancestors=False,
match_all_words=False,
ignore_relation_phraselets=False,
include_reverse_only=False,
stop_lemmas=[],
stop_tags=[],
reverse_only_parent_lemmas=[],
words_to_corpus_frequencies=None,
maximum_corpus_frequency=None,
process_initial_question_words=False)
self.assertEqual(phraselet_labels_to_phraselet_infos['predicate-patient: start-discuss'].child_lemma, 'discuss')
ontology_holmes_manager.parse_and_register_document("They started a discussion", '4')
doc = ontology_holmes_manager.get_document('4')
ontology_holmes_manager.linguistic_object_factory.add_phraselets_to_dict(doc,
phraselet_labels_to_phraselet_infos=phraselet_labels_to_phraselet_infos,
replace_with_hypernym_ancestors=False,
match_all_words=False,
ignore_relation_phraselets=False,
include_reverse_only=False,
stop_lemmas=[],
stop_tags=[],
reverse_only_parent_lemmas=[],
words_to_corpus_frequencies=None,
maximum_corpus_frequency=None,
process_initial_question_words=False)
self.assertEqual(phraselet_labels_to_phraselet_infos['predicate-patient: start-discuss'].child_lemma, 'discussion')