-
Notifications
You must be signed in to change notification settings - Fork 12
/
Copy pathtest_topic_matching_EN.py
1277 lines (1066 loc) · 82.5 KB
/
test_topic_matching_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
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
import unittest
import holmes_extractor as holmes
from holmes_extractor.topic_matching import TopicMatcher
import os
script_directory = os.path.dirname(os.path.realpath(__file__))
ontology = holmes.Ontology(os.sep.join((script_directory, 'test_ontology.owl')),
symmetric_matching=True)
holmes_manager_coref = holmes.Manager(model='en_core_web_trf',
perform_coreference_resolution=True,
ontology=ontology,
number_of_workers=2)
class EnglishTopicMatchingTest(unittest.TestCase):
def _check_equals(self, text_to_match, document_text, highest_score, manager,
word_embedding_match_threshold=0.42, use_frequency_factor=True):
manager.remove_all_documents()
manager.parse_and_register_document(document_text)
topic_matches = manager.topic_match_documents_against(text_to_match,
word_embedding_match_threshold=
word_embedding_match_threshold,
relation_score=20,
reverse_only_relation_score=15, single_word_score=10, single_word_any_tag_score=5,
different_match_cutoff_score=10,
relation_matching_frequency_threshold=0.0,
embedding_matching_frequency_threshold=0.0,
use_frequency_factor=use_frequency_factor)
if type(highest_score) == list:
self.assertIn(int(topic_matches[0]['score']), highest_score)
else:
self.assertEqual(int(topic_matches[0]['score']), highest_score)
def test_no_match(self):
holmes_manager_coref.remove_all_documents()
holmes_manager_coref.parse_and_register_document("A plant grows")
topic_matches = holmes_manager_coref.topic_match_documents_against("fewfew",
relation_score=20, reverse_only_relation_score=15, single_word_score=10,
single_word_any_tag_score=5)
self.assertEqual(topic_matches, [])
def test_no_match_stopwords(self):
holmes_manager_coref.remove_all_documents()
holmes_manager_coref.parse_and_register_document("then")
topic_matches = holmes_manager_coref.topic_match_documents_against("then",
relation_score=20, reverse_only_relation_score=15, single_word_score=10,
single_word_any_tag_score=5)
self.assertEqual(topic_matches, [])
def test_direct_matching(self):
self._check_equals("A plant grows", "A plant grows",
34, holmes_manager_coref)
def test_direct_matching_frequency_factor_2_word(self):
self._check_equals("A plant grows", "A plant grows. A plant",
34, holmes_manager_coref)
def test_direct_matching_frequency_factor_3_word(self):
self._check_equals("A plant grows", "A plant grows. A plant and a plant",
16, holmes_manager_coref)
def test_direct_matching_frequency_factor_3_word_control(self):
self._check_equals("A plant grows", "A plant grows. A plant and a plant",
34, holmes_manager_coref, use_frequency_factor=False)
def test_direct_matching_frequency_factor_3_word_with_higher_maximum(self):
self._check_equals("A plant grows", "A plant grows. A plant and a plant. Word word word word word.",
22, holmes_manager_coref)
def test_direct_matching_frequency_factor_2_relation(self):
self._check_equals("A plant grows", "A plant grows. A plant grows.",
34, holmes_manager_coref)
def test_direct_matching_frequency_factor_3_relation(self):
self._check_equals("A plant grows", "A plant grows. A plant grows. A plant grows.",
8, holmes_manager_coref)
def test_direct_matching_frequency_factor_3_relation_with_higher_maximum(self):
self._check_equals("A plant grows", "A plant grows. A plant grows. A plant grows. Word word word word word.",
14, holmes_manager_coref)
def test_direct_matching_nonsense_word(self):
self._check_equals("A hurricane visited gegwghg", "Peter visited gegwghg", 34,
holmes_manager_coref)
def test_dative_matching(self):
self._check_equals("I gave Peter a dog",
"I gave Peter a present", 34, holmes_manager_coref)
def test_coref_matching(self):
self._check_equals("A plant grows", "I saw a plant. It was growing", 34,
holmes_manager_coref)
def test_entity_matching(self):
self._check_equals("My house visited ENTITYGPE", "Peter visited London", 34,
holmes_manager_coref)
def test_entity_matching_frequency_factor(self):
self._check_equals("My house visited ENTITYGPE", "Peter visited Paris. Somebody lived in London. Somebody lived in Berlin.", 15,
holmes_manager_coref)
def test_entity_embedding_matching(self):
self._check_equals("My friend visited ENTITYGPE", "Peter visited London", [56, 57],
holmes_manager_coref)
def test_entity_embedding_matching_frequency_factor(self):
self._check_equals("My friend visited ENTITYGPE", "Peter visited Paris. Somebody lived in London. Somebody lived in Berlin.", [31, 32],
holmes_manager_coref)
def test_entitynoun_matching(self):
self._check_equals("My hurricane visited ENTITYNOUN", "Peter visited a city", 25,
holmes_manager_coref)
def test_entitynoun_matching_control(self):
self._check_equals("My hurricane visited ENTITYNOUN", "Peter visited a city. Word. word.", 25,
holmes_manager_coref)
def test_ontology_matching_synonym(self):
self._check_equals("I saw an pussy", "Somebody saw a cat", 31,
holmes_manager_coref)
def test_ontology_matching_synonym_frequency_factor(self):
self._check_equals("I saw an pussy", "Somebody saw a cat. A cat. A cat.", 14,
holmes_manager_coref)
def test_ontology_matching_synonym_frequency_factor_different_ontology_words(self):
self._check_equals("I saw an pussy", "Somebody saw a cat. A kitten. A cat.", 31,
holmes_manager_coref)
def test_ontology_matching_hyponym_depth_1(self):
self._check_equals("I saw an animal", "Somebody saw a cat", 28,
holmes_manager_coref)
def test_ontology_matching_hyponym_depth_1_frequency_factor(self):
self._check_equals("I saw an animal", "Somebody saw a cat. An cat. A cat.", 13,
holmes_manager_coref)
def test_ontology_matching_hyponym_depth_1_frequency_factor_different_ontology_words(self):
self._check_equals("I saw an animal", "Somebody saw a cat. An kitten. A cat.", 28,
holmes_manager_coref)
def test_ontology_matching_hyponym_depth_2(self):
self._check_equals("I saw an animal", "Somebody saw a kitten", 26,
holmes_manager_coref)
def test_ontology_matching_hypernym_depth_1(self):
self._check_equals("I saw an cat", "Somebody saw an animal", 28,
holmes_manager_coref)
def test_ontology_matching_hypernym_depth_2(self):
self._check_equals("I saw a kitten", "Somebody saw an animal", 26,
holmes_manager_coref)
def test_ontology_matching_both_poles(self):
self._check_equals("A cat opens something", "An animal takes something out", 27,
holmes_manager_coref)
def test_ontology_matching_multiword_in_document(self):
self._check_equals("I saw an animal", "Somebody saw Mimi Momo", 26,
holmes_manager_coref)
def test_ontology_matching_multiword_in_document_frequency_factor(self):
self._check_equals("I saw an animal", "Somebody saw Mimi Momo. Mimi Momo. Mimi Momo.", 12,
holmes_manager_coref)
@unittest.skipIf(holmes_manager_coref.nlp.meta['version'] == '3.4.1', 'Version fluke')
def test_ontology_matching_multiword_in_document_frequency_factor_control(self):
self._check_equals("I saw an animal", "Somebody saw Mimi Momo. Momo. Momo.", 26,
holmes_manager_coref)
def test_ontology_matching_multiword_in_search_text(self):
self._check_equals("I saw Mimi Momo", "Somebody saw an animal", 26,
holmes_manager_coref)
def test_ontology_matching_word_only(self):
self._check_equals("I saw an animal", "Somebody chased a cat", 8,
holmes_manager_coref)
def test_ontology_matching_word_only_multiword_in_document(self):
self._check_equals("I saw an animal", "Somebody chased Mimi Momo", 7,
holmes_manager_coref)
def test_ontology_matching_word_only_multiword_in_search_text(self):
self._check_equals("I saw Mimi Momo", "Somebody chased an animal", 7,
holmes_manager_coref)
def test_embedding_matching_not_root(self):
self._check_equals("I saw a king", "Somebody saw a queen", [14, 15],
holmes_manager_coref)
def test_embedding_matching_root_overall_similarity_too_low(self):
holmes_manager_coref.remove_all_documents()
holmes_manager_coref.parse_and_register_document(
"I saw a king.")
topic_matches = holmes_manager_coref.topic_match_documents_against("Somebody viewed a queen",
relation_score=20, reverse_only_relation_score=15, single_word_score=10,
single_word_any_tag_score=5,
relation_matching_frequency_threshold=0.0,
embedding_matching_frequency_threshold=1.0,
use_frequency_factor=False)
self.assertEqual(len(topic_matches), 0)
def test_embedding_matching_root_word_only(self):
holmes_manager_coref.remove_all_documents()
holmes_manager_coref.parse_and_register_document(
"king")
topic_matches = holmes_manager_coref.topic_match_documents_against("queen",
relation_score=20, reverse_only_relation_score=15, single_word_score=10,
single_word_any_tag_score=5,
relation_matching_frequency_threshold=0.0,
embedding_matching_frequency_threshold=1.0,
use_frequency_factor=False)
self.assertEqual(len(topic_matches), 0)
def test_matching_only_adjective(self):
self._check_equals("nice", "nice", 5, holmes_manager_coref)
def test_matching_only_adjective_where_noun(self):
self._check_equals("nice place", "nice", 5, holmes_manager_coref)
def test_reverse_only_parent_lemma_threeway(self):
self._check_equals("The donkey has a roof", "The donkey has a roof", 68,
holmes_manager_coref)
def test_reverse_only_parent_lemma_threeway_coreference(self):
self._check_equals("A friend has a roof", "I saw a friend and I saw a roof. He had it.", 68,
holmes_manager_coref)
def test_reverse_only_parent_lemma_twoway(self):
self._check_equals("The donkey has a roof", "The donkey has a house", [46, 47],
holmes_manager_coref)
def test_reverse_only_parent_lemma_threeway_control(self):
self._check_equals("The donkey paints a roof", "The donkey paints a roof", 82,
holmes_manager_coref)
def test_reverse_only_parent_lemma_twoway_control(self):
self._check_equals("The donkey paints a roof", "The donkey paints a house", [56, 58],
holmes_manager_coref)
def test_reverse_only_parent_lemma_twoway_control_no_embedding_based_match(self):
self._check_equals("The donkey paints a roof", "The donkey paints a mouse", 34,
holmes_manager_coref)
def test_reverse_only_parent_lemma_be(self):
self._check_equals("A president is a politician", "A president is a politician", 68,
holmes_manager_coref)
def test_reverse_only_parent_lemma_be_reversed(self):
self._check_equals("A president is a politician", "A politician is a president", 24,
holmes_manager_coref)
def test_reverse_only_parent_lemma_aux_in_document(self):
self._check_equals("A donkey has a roof", "A donkey has painted a roof", 24,
holmes_manager_coref)
def test_reverse_matching_noun_no_coreference(self):
self._check_equals("A car with an engine", "An automobile with an engine", [50, 51],
holmes_manager_coref)
def test_reverse_matching_noun_no_coreference_frequency_factor(self):
self._check_equals("A car with an engine", "An automobile with an engine. An engine. An engine.", [27, 28],
holmes_manager_coref)
def test_reverse_matching_noun_no_coreference_control_no_embeddings(self):
self._check_equals("A car with an engine", "An automobile with an engine", 29,
holmes_manager_coref, word_embedding_match_threshold=1.0)
def test_reverse_matching_noun_no_coreference_control_same_word(self):
self._check_equals("A car with an engine", "A car with an engine", 75,
holmes_manager_coref, word_embedding_match_threshold=1.0)
def test_forward_matching_noun_entity_governor_match(self):
self._check_equals("An ENTITYPERSON with a car", "Richard Hudson with a vehicle", [23, 24],
holmes_manager_coref)
def test_forward_matching_noun_entity_governor_no_match(self):
self._check_equals("An ENTITYPERSON with a car", "Richard Hudson with a lion", 14,
holmes_manager_coref)
def test_reverse_matching_noun_entity_governed(self):
self._check_equals("A car with an ENTITYPERSON", "A vehicle with Richard Hudson", 50,
holmes_manager_coref)
def test_forward_matching_noun_entitynoun_governor_match(self):
self._check_equals("An ENTITYNOUN with a car", "Richard Hudson with a vehicle", 5,
holmes_manager_coref)
def test_forward_matching_noun_entitynoun_governor_no_match(self):
self._check_equals("An ENTITYNOUN with a car", "Richard Hudson with a lion", 5,
holmes_manager_coref)
def test_reverse_matching_noun_entitynoun_governed(self):
self._check_equals("A car with an ENTITYNOUN", "A vehicle with Richard Hudson", 5,
holmes_manager_coref)
def test_multiword_matching_with_hyphen_normalization(self):
self._check_equals("Richard Hudson speaks", "Richard-Hudson speaks",
34, holmes_manager_coref)
def test_relation_matching_suppressed(self):
holmes_manager_coref.remove_all_documents()
holmes_manager_coref.parse_and_register_document("A dog chases a cat. A dog sees a cat. A dog sees a cat. A person was chasing a person. A person chased a person.")
topic_matches = holmes_manager_coref.topic_match_documents_against("A dog chases a cat.",
relation_score=20, reverse_only_relation_score=15, single_word_score=10,
single_word_any_tag_score=5,
relation_matching_frequency_threshold=1.0,
embedding_matching_frequency_threshold=1.0,
use_frequency_factor=False)
self.assertEqual(int(topic_matches[0]['score']), 24)
def test_suppressed_relation_matching_picked_up_during_reverse_matching(self):
holmes_manager_coref.remove_all_documents()
holmes_manager_coref.parse_and_register_document(
"Chasing. Chasing. A dog chases a cat. A lion chases a tiger.")
topic_matches = holmes_manager_coref.topic_match_documents_against("A dog chases a cat",
relation_score=20, reverse_only_relation_score=15, single_word_score=10,
single_word_any_tag_score=5,
relation_matching_frequency_threshold=0.9,
embedding_matching_frequency_threshold=1.0,
use_frequency_factor=False)
self.assertEqual(int(topic_matches[0]['score']), 82)
def test_suppressed_relation_matching_picked_up_during_reverse_matching_with_coreference(self):
holmes_manager_coref.remove_all_documents()
holmes_manager_coref.parse_and_register_document(
"There was a cat. A dog chased it. A lion chases a tiger. Chasing. Chasing. ")
topic_matches = holmes_manager_coref.topic_match_documents_against("A dog chases a cat",
relation_score=20, reverse_only_relation_score=15, single_word_score=10,
single_word_any_tag_score=5,
relation_matching_frequency_threshold=0.9,
embedding_matching_frequency_threshold=1.0,
use_frequency_factor=False)
self.assertEqual(int(topic_matches[0]['score']), 84)
def test_suppressed_relation_matching_picked_up_during_reverse_matching_with_reverse_dependency(self):
holmes_manager_coref.remove_all_documents()
holmes_manager_coref.parse_and_register_document(
"Someone adopts the child. The child is here. Children. Children. Children.")
topic_matches = holmes_manager_coref.topic_match_documents_against("An adopted child",
relation_score=20, reverse_only_relation_score=15, single_word_score=10,
single_word_any_tag_score=5,
relation_matching_frequency_threshold=0.9,
embedding_matching_frequency_threshold=1.0,
use_frequency_factor=False)
self.assertEqual(int(topic_matches[0]['score']), 34)
def test_reverse_matching_suppressed_with_relation_matching(self):
holmes_manager_coref.remove_all_documents()
holmes_manager_coref.parse_and_register_document(
"I was in Germany. I know Germany. Germany. Germany.")
topic_matches = holmes_manager_coref.topic_match_documents_against("in Germany",
relation_score=20, reverse_only_relation_score=15, single_word_score=10,
single_word_any_tag_score=5,
relation_matching_frequency_threshold=0.1, embedding_matching_frequency_threshold=0.6)
self.assertEqual(int(topic_matches[0]['score']), 10)
def test_reverse_matching_suppressed_with_relation_matching_embedding_value_same(self):
holmes_manager_coref.remove_all_documents()
holmes_manager_coref.parse_and_register_document(
"I was in Germany. I know Germany. Germany. Germany.")
topic_matches = holmes_manager_coref.topic_match_documents_against("in Germany",
relation_score=20, reverse_only_relation_score=15, single_word_score=10,
single_word_any_tag_score=5,
relation_matching_frequency_threshold=0.1, embedding_matching_frequency_threshold=0.1)
self.assertEqual(int(topic_matches[0]['score']), 10)
def test_reverse_matching_suppressed_with_relation_matching_control(self):
holmes_manager_coref.remove_all_documents()
holmes_manager_coref.parse_and_register_document(
"I was in Germany. I know Germany. Germany. Germany.")
topic_matches = holmes_manager_coref.topic_match_documents_against("in Germany",
relation_score=20, reverse_only_relation_score=15, single_word_score=10,
single_word_any_tag_score=5,
relation_matching_frequency_threshold=1.0, embedding_matching_frequency_threshold=1.0)
self.assertEqual(int(topic_matches[0]['score']), 7)
def test_reverse_matching_suppressed_with_embedding_reverse_matching_parent(self):
holmes_manager_coref.remove_all_documents()
holmes_manager_coref.parse_and_register_document(
"An automobile with an engine. An engine. An engine.")
topic_matches = holmes_manager_coref.topic_match_documents_against("A car with an engine",
word_embedding_match_threshold=0.42,
relation_score=20, reverse_only_relation_score=15, single_word_score=10,
single_word_any_tag_score=5,
relation_matching_frequency_threshold=0.0, embedding_matching_frequency_threshold=1.0,
use_frequency_factor=False)
self.assertEqual(int(topic_matches[0]['score']), 29)
def test_reverse_matching_suppressed_with_embedding_reverse_matching_parent_control(self):
holmes_manager_coref.remove_all_documents()
holmes_manager_coref.parse_and_register_document(
"An automobile with an engine. An engine. An engine.")
topic_matches = holmes_manager_coref.topic_match_documents_against("A car with an engine",
word_embedding_match_threshold=0.42,
relation_score=20, reverse_only_relation_score=15, single_word_score=10,
single_word_any_tag_score=5,
relation_matching_frequency_threshold=0.0, embedding_matching_frequency_threshold=0.0,
use_frequency_factor=False)
self.assertIn(int(topic_matches[0]['score']), [50, 51])
def test_reverse_matching_suppressed_with_embedding_reverse_matching_child(self):
holmes_manager_coref.remove_all_documents()
holmes_manager_coref.parse_and_register_document(
"An engine with an automobile. An engine. An engine.")
topic_matches = holmes_manager_coref.topic_match_documents_against("An engine with a car",
word_embedding_match_threshold=0.42,
relation_score=20, reverse_only_relation_score=15, single_word_score=10,
single_word_any_tag_score=5,
relation_matching_frequency_threshold=1.0, embedding_matching_frequency_threshold=1.0,
use_frequency_factor=False)
self.assertEqual(int(topic_matches[0]['score']), 14)
def test_reverse_matching_suppressed_with_embedding_reverse_matching_child_control(self):
holmes_manager_coref.remove_all_documents()
holmes_manager_coref.parse_and_register_document(
"An engine with an automobile. An engine. An engine.")
topic_matches = holmes_manager_coref.topic_match_documents_against("An engine with a car",
word_embedding_match_threshold=0.42,
relation_score=20, reverse_only_relation_score=15, single_word_score=10,
single_word_any_tag_score=5,
relation_matching_frequency_threshold=0.0, embedding_matching_frequency_threshold=0.0,
use_frequency_factor=False)
self.assertIn(int(topic_matches[0]['score']), [24, 25])
def test_entity_matching_suppressed_with_relation_matching_for_governor(self):
holmes_manager_coref.remove_all_documents()
holmes_manager_coref.parse_and_register_document(
"I was tired Richard Paul Hudson. I was a tired Richard Paul Hudson. I spoke to Richard Paul Hudson and he was tired.")
topic_matches = holmes_manager_coref.topic_match_documents_against("tired ENTITYPERSON",
relation_score=20, reverse_only_relation_score=15, single_word_score=10,
single_word_any_tag_score=5,
relation_matching_frequency_threshold=1.0, embedding_matching_frequency_threshold=1.0,
use_frequency_factor=False)
self.assertEqual(int(topic_matches[0]['score']), 14)
def test_entity_matching_suppressed_with_relation_matching_for_governor_control(self):
holmes_manager_coref.remove_all_documents()
holmes_manager_coref.parse_and_register_document(
"I was tired Richard Paul Hudson. I was a tired Richard Paul Hudson. I spoke to Richard Paul Hudson and he was tired.")
topic_matches = holmes_manager_coref.topic_match_documents_against("tired ENTITYPERSON",
relation_score=20, reverse_only_relation_score=15, single_word_score=10,
single_word_any_tag_score=5,
relation_matching_frequency_threshold=0.0, embedding_matching_frequency_threshold=0.0,
use_frequency_factor=False)
self.assertEqual(int(topic_matches[0]['score']), 34)
def test_entity_matching_suppressed_with_relation_matching_for_governed(self):
holmes_manager_coref.remove_all_documents()
holmes_manager_coref.parse_and_register_document(
"I knew Richard Paul Hudson. I knew Richard Paul Hudson. I knew someone and spoke to Richard Paul Hudson.")
topic_matches = holmes_manager_coref.topic_match_documents_against(
"someone knows an ENTITYPERSON",
relation_score=20, reverse_only_relation_score=15, single_word_score=10,
single_word_any_tag_score=5,
relation_matching_frequency_threshold=1.0,
embedding_matching_frequency_threshold=1.0,
use_frequency_factor=False)
self.assertEqual(int(topic_matches[0]['score']), 14)
def test_entity_matching_suppressed_with_relation_matching_for_governed_control(self):
holmes_manager_coref.remove_all_documents()
holmes_manager_coref.parse_and_register_document(
"I knew Richard Paul Hudson. I knew Richard Paul Hudson. I knew someone and spoke to Richard Paul Hudson.")
topic_matches = holmes_manager_coref.topic_match_documents_against(
"someone knows an ENTITYPERSON",
relation_score=20, reverse_only_relation_score=15, single_word_score=10,
single_word_any_tag_score=5,
relation_matching_frequency_threshold=0.0,
embedding_matching_frequency_threshold=0.0,
use_frequency_factor=False)
self.assertEqual(int(topic_matches[0]['score']), 34)
def test_reverse_matching_noun_coreference_on_governor(self):
self._check_equals("A car with an engine", "I saw an automobile. I saw it with an engine",
[50, 51],
holmes_manager_coref)
def test_reverse_matching_noun_coreference_on_governor_control_no_embeddings(self):
self._check_equals("A car with an engine", "I saw an automobile. I saw it with an engine",
29,
holmes_manager_coref, word_embedding_match_threshold=1.0)
@unittest.skipIf(holmes_manager_coref.nlp.meta['version'] == '3.5.0', 'Version fluke')
def test_reverse_matching_noun_coreference_on_governor_control_same_word(self):
self._check_equals("A car with an engine", "I saw a car. I saw it with an engine",
73,
holmes_manager_coref, word_embedding_match_threshold=1.0)
def test_reverse_matching_noun_coreference_on_governed(self):
self._check_equals(
"An engine with a car", "I saw an automobile. I saw the engine with it", [24, 25],
holmes_manager_coref)
def test_reverse_matching_noun_coreference_on_governed_control_no_embeddings(self):
self._check_equals(
"An engine with a car", "I saw an automobile. I saw the engine with it", 14,
holmes_manager_coref, word_embedding_match_threshold=1.0)
def test_reverse_matching_noun_coreference_on_governed_control_same_word(self):
self._check_equals(
"An engine with a car", "I saw a car. I saw the engine with it", 76,
holmes_manager_coref, word_embedding_match_threshold=1.0)
def test_reverse_matching_verb_with_coreference_and_conjunction(self):
self._check_equals("A company is bought", "A company is bought and purchased", 34,
holmes_manager_coref)
def test_two_matches_on_same_document_tokens_because_of_embeddings(self):
self._check_equals("Somebody buys a vehicle",
"Somebody buys a vehicle and a car", 34,
holmes_manager_coref)
def test_reverse_matching_only(self):
self._check_equals("with an idea",
"with an idea", 29,
holmes_manager_coref)
def test_repeated_single_word_label_tags_matched(self):
self._check_equals("dog",
"a dog and a dog", 10,
holmes_manager_coref)
def test_repeated_single_word_label_tags_not_matched(self):
self._check_equals("in",
"in and in", 5,
holmes_manager_coref)
def test_repeated_relation_label_not_reverse_only_no_common_term(self):
self._check_equals("a big dog",
"a big dog and a big dog", 34,
holmes_manager_coref)
def test_repeated_relation_label_not_reverse_only_common_term(self):
self._check_equals("a big dog",
"a big and big dog", 34,
holmes_manager_coref)
def test_repeated_relation_label_reverse_only_no_common_term(self):
self._check_equals("in Germany",
"in Germany and in Germany", 29,
holmes_manager_coref)
def test_repeated_relation_label_reverse_only_common_term(self):
self._check_equals("in Germany",
"in Germany and Germany", 29,
holmes_manager_coref)
def test_multiword_in_text_to_search_and_in_document_not_root(self):
self._check_equals("Richard Paul Hudson came",
"I saw Richard Paul Hudson", 10,
holmes_manager_coref)
def test_multiword_in_text_to_search_single_word_in_document_not_root(self):
self._check_equals("Hudson came",
"I saw Richard Paul Hudson", 10,
holmes_manager_coref)
def test_multiword_in_text_to_search_dependent_words_in_document_not_root(self):
self._check_equals("Richard Paul came",
"I saw Richard Paul Hudson", 9,
holmes_manager_coref)
def test_multiword_in_text_to_search_multiword_in_document_with_coref_not_root(self):
self._check_equals("Richard Paul Hudson came",
"I saw Richard Paul Hudson. He came", 34,
holmes_manager_coref)
def test_multiword_in_text_to_search_multiword_in_document_with_noun_coref_not_root(self):
self._check_equals("Richard Paul Hudson came",
"I saw Richard Paul Hudson. Hudson came", 39,
holmes_manager_coref)
def test_multiword_in_text_to_search_single_in_document_with_coref_not_root(self):
self._check_equals("Hudson came",
"I saw Richard Paul Hudson. He came", 34,
holmes_manager_coref)
def test_multiword_in_text_to_search_and_in_document_root(self):
self._check_equals("the tired Richard Paul Hudson",
"I saw Richard Paul Hudson", 10,
holmes_manager_coref)
def test_multiword_in_text_to_search_single_word_in_document_root(self):
self._check_equals("the tired Hudson",
"I saw Richard Paul Hudson", 10,
holmes_manager_coref)
def test_multiword_in_text_to_search_dependent_words_in_document_root(self):
self._check_equals("the tired Richard Paul",
"I saw Richard Paul Hudson", 9,
holmes_manager_coref)
def test_multiword_in_text_to_search_multiword_in_document_with_coref_root(self):
self._check_equals("the tired Richard Paul Hudson",
"I saw Richard Paul Hudson. He came", 10,
holmes_manager_coref)
def test_multiword_in_text_to_search_single_in_document_with_coref_root(self):
self._check_equals("the tired Hudson came",
"I saw Richard Paul Hudson. He came", 34,
holmes_manager_coref)
def test_multiword_in_text_to_search_and_in_document_not_root_match_on_embeddings(self):
self._check_equals("Richard Paul Hudson came",
"I saw Richard Paul Hudson", 10,
holmes_manager_coref)
def test_multiword_in_text_to_search_and_in_document_root_match_on_embeddings(self):
self._check_equals("the tired Richard Paul Hudson",
"I saw Richard Paul Hudson", 10,
holmes_manager_coref)
def test_multiword_in_text_to_search_and_in_document_not_root_no_embeddings(self):
self._check_equals("Richard Paul Hudson came",
"I saw Richard Paul Hudson", 10,
holmes_manager_coref)
def test_multiword_in_text_to_search_and_in_document_root_no_embeddings(self):
self._check_equals("the tired Richard Paul Hudson",
"I saw Richard Paul Hudson", 10,
holmes_manager_coref)
def test_matches_in_opposite_directions(self):
self._check_equals("the mirror of Erised",
"the mirror of Erised", 39,
holmes_manager_coref)
def test_derived_form_text_to_match_single_word(self):
self._check_equals("information",
"inform", 10,
holmes_manager_coref)
def test_derived_form_text_to_match_single_word_frequency_factor(self):
self._check_equals("information",
"inform. inform. inform.", 3,
holmes_manager_coref)
def test_derived_form_document_text_single_word(self):
self._check_equals("give",
"gift", 5,
holmes_manager_coref)
def test_derived_form_single_word_control(self):
self._check_equals("information",
"information", 10,
holmes_manager_coref)
def test_derived_form_document_text_parent(self):
self._check_equals("inform quickly",
"quick information", 29,
holmes_manager_coref)
def test_derived_form_text_to_match_parent(self):
self._check_equals("quick information",
"inform quickly", 34,
holmes_manager_coref)
def test_derived_form_parent_control(self):
self._check_equals("quick information",
"quick information", 34,
holmes_manager_coref)
def test_derived_form_document_text_child(self):
self._check_equals("He decided to inform",
"He decided information", 29,
holmes_manager_coref)
def test_derived_form_text_to_match_child(self):
self._check_equals("He decided information",
"He decided to inform", 34,
holmes_manager_coref)
def test_derived_form_child_control(self):
self._check_equals("He decided information",
"He decided information", 34,
holmes_manager_coref)
def test_derived_forms_matched_by_ontology_1(self):
self._check_equals("An invitation to a politician",
"He explained to a politician", 35,
holmes_manager_coref)
def test_derived_forms_matched_by_ontology_2(self):
self._check_equals("He explained to a politician",
"An invitation to a politician", 31,
holmes_manager_coref)
def test_derived_multiword_child_also_matched_by_ontology_1(self):
self._check_equals("He used a waste horse",
"He used a wastage horse", 34,
holmes_manager_coref)
def test_derived_multiword_child_also_matched_by_ontology_2(self):
self._check_equals("He used a wastage horse",
"He used a waste horse", 33,
holmes_manager_coref)
def test_derived_multiword_child_also_matched_by_ontology_3(self):
self._check_equals("He used a waste horse",
"He used gymnastics equipment", 26,
holmes_manager_coref)
def test_derived_multiword_child_also_matched_by_ontology_4(self):
self._check_equals("He used gymnastics equipment",
"He used a waste horse", 26,
holmes_manager_coref)
def test_derived_multiword_parent_also_matched_by_ontology_1(self):
self._check_equals("A big waste horse",
"A big wastage horse", 34,
holmes_manager_coref)
def test_derived_multiword_parent_also_matched_by_ontology_2(self):
self._check_equals("A big wastage horse",
"A big waste horse", 34,
holmes_manager_coref)
def test_derived_multiword_parent_also_matched_by_ontology_3(self):
self._check_equals("A big waste horse",
"A big gymnastics equipment", 26,
holmes_manager_coref)
def test_derived_multiword_parent_also_matched_by_ontology_4(self):
self._check_equals("A big gymnastics equipment",
"A big waste horse", 26,
holmes_manager_coref)
def test_reverse_dependencies_1(self):
self._check_equals("An adopted child",
"Someone adopts a child", 34,
holmes_manager_coref)
def test_reverse_dependencies_2(self):
self._check_equals("Someone adopts a child",
"An adopted child", 34,
holmes_manager_coref)
def test_reverse_dependencies_control(self):
self._check_equals("Adopted and child",
"An adopted child", 14,
holmes_manager_coref)
def test_tough_movement(self):
self._check_equals("A leg is hard to stretch", "He stretched his legs.",
34,
holmes_manager_coref)
def test_tough_movement_control(self):
self._check_equals("A leg is hard to fix", "He stretched his legs.",
10,
holmes_manager_coref)
def test_coreference_double_match_on_governed(self):
holmes_manager_coref.remove_all_documents()
holmes_manager_coref.parse_and_register_document(
"I saw a man. The man walked")
topic_matches = holmes_manager_coref.topic_match_documents_against("A man walks",
relation_score=20, single_word_score=10, single_word_any_tag_score=5)
self.assertEqual(int(topic_matches[0]['score']), 34)
self.assertEqual(topic_matches[0]['sentences_start_index'], 5)
self.assertEqual(topic_matches[0]['sentences_end_index'], 7)
self.assertEqual(topic_matches[0]['start_index'], 6)
self.assertEqual(topic_matches[0]['end_index'], 7)
def test_coreference_double_match_on_governor(self):
holmes_manager_coref.remove_all_documents()
holmes_manager_coref.parse_and_register_document(
"I saw a big man. The man walked")
topic_matches = holmes_manager_coref.topic_match_documents_against(
"A big man", relation_score=20, single_word_score=10, single_word_any_tag_score=5)
self.assertEqual(int(topic_matches[0]['score']), 34)
self.assertEqual(topic_matches[0]['sentences_start_index'], 0)
self.assertEqual(topic_matches[0]['sentences_end_index'], 8)
self.assertEqual(topic_matches[0]['start_index'], 3)
self.assertEqual(topic_matches[0]['end_index'], 7)
def test_coreference_double_match_same_distance(self):
holmes_manager_coref.remove_all_documents()
holmes_manager_coref.parse_and_register_document(
"The man was big. Man walked.")
topic_matches = holmes_manager_coref.topic_match_documents_against("A big man",
relation_score=20, single_word_score=10, single_word_any_tag_score=5)
self.assertEqual(int(topic_matches[0]['score']), 34)
self.assertEqual(topic_matches[0]['sentences_start_index'], 0)
self.assertEqual(topic_matches[0]['sentences_end_index'], 7)
self.assertEqual(topic_matches[0]['start_index'], 1)
self.assertEqual(topic_matches[0]['end_index'], 5)
def test_indexes(self):
holmes_manager_coref.remove_all_documents()
holmes_manager_coref.parse_and_register_document(
"This is an irrelevant sentence. I think a plant grows.")
topic_matches = holmes_manager_coref.topic_match_documents_against(
"A plant grows")
self.assertEqual(topic_matches[0]['sentences_start_index'], 6)
self.assertEqual(topic_matches[0]['sentences_end_index'], 11)
self.assertEqual(topic_matches[0]['start_index'], 9)
self.assertEqual(topic_matches[0]['end_index'], 10)
def test_indexes_with_preceding_non_matched_dependent(self):
holmes_manager_coref.remove_all_documents()
holmes_manager_coref.parse_and_register_document(
"I saw a big dog.")
topic_matches = holmes_manager_coref.topic_match_documents_against(
"A big dog")
self.assertEqual(topic_matches[0]['sentences_start_index'], 0)
self.assertEqual(topic_matches[0]['sentences_end_index'], 5)
self.assertEqual(topic_matches[0]['start_index'], 3)
self.assertEqual(topic_matches[0]['end_index'], 4)
def test_indexes_with_subsequent_non_matched_dependent(self):
holmes_manager_coref.remove_all_documents()
holmes_manager_coref.parse_and_register_document(
"The dog I saw was big.")
topic_matches = holmes_manager_coref.topic_match_documents_against(
"A big dog")
self.assertEqual(topic_matches[0]['sentences_start_index'], 0)
self.assertEqual(topic_matches[0]['sentences_end_index'], 6)
self.assertEqual(topic_matches[0]['start_index'], 1)
self.assertEqual(topic_matches[0]['end_index'], 5)
def test_only_one_result_per_document(self):
holmes_manager_coref.remove_all_documents()
holmes_manager_coref.remove_all_search_phrases()
holmes_manager_coref.parse_and_register_document(
"""
Peter came home. A great deal of irrelevant text. A great deal of irrelevant text.
A great deal of irrelevant text. A great deal of irrelevant text. A great deal of
irrelevant text. A great deal of irrelevant text. A great deal of irrelevant text.
A great deal of irrelevant text. A great deal of irrelevant text. A great deal of
irrelevant text. A great deal of irrelevant text. A great deal of irrelevant text.
A great deal of irrelevant text. A great deal of irrelevant text. A great deal of
irrelevant text. Peter came home.
""")
self.assertEqual(
len(holmes_manager_coref.topic_match_documents_against("Peter")), 2)
self.assertEqual(len(holmes_manager_coref.topic_match_documents_against("Peter",
only_one_result_per_document=True)), 1)
def test_match_cutoff(self):
holmes_manager_coref.remove_all_documents()
holmes_manager_coref.remove_all_search_phrases()
holmes_manager_coref.parse_and_register_document(
"""
A cat.
A great deal of irrelevant text. A great deal of irrelevant text. A great deal of
irrelevant text. A great deal of irrelevant text. A great deal of irrelevant text.
A great deal of irrelevant text. A great deal of irrelevant text. A great deal of
irrelevant text. A great deal of irrelevant text. A great deal of irrelevant text.
A great deal of irrelevant text. A great deal of irrelevant text. A great deal of
irrelevant text. A great deal of irrelevant text. A great deal of irrelevant text.
The dog chased the cat.
""")
topic_matches = holmes_manager_coref.topic_match_documents_against(
"The dog chased the cat")
self.assertEqual(topic_matches[0]['start_index'], 117)
self.assertEqual(topic_matches[0]['end_index'], 120)
def test_result_ordering_by_match_length_different_documents_2(self):
holmes_manager_coref.remove_all_documents()
holmes_manager_coref.remove_all_search_phrases()
holmes_manager_coref.parse_and_register_document("""
A dog chased a cat.
A great deal of irrelevant text. A great deal of irrelevant text. A great deal of
irrelevant text. A great deal of irrelevant text. A great deal of irrelevant text.
A great deal of irrelevant text. A great deal of irrelevant text. A great deal of
irrelevant text. A great deal of irrelevant text. A great deal of irrelevant text.
A great deal of irrelevant text. A great deal of irrelevant text. A great deal of
irrelevant text. A great deal of irrelevant text. A great deal of irrelevant text.
A dog chased a cat. A cat
""")
topic_matches = holmes_manager_coref.topic_match_documents_against(
"The dog chased the cat")
self.assertEqual(
topic_matches[0]['end_index'] - topic_matches[0]['start_index'], 6)
self.assertEqual(
topic_matches[1]['end_index'] - topic_matches[1]['start_index'], 3)
def test_dictionaries(self):
holmes_manager_coref.remove_all_documents()
holmes_manager_coref.remove_all_search_phrases()
holmes_manager_coref.parse_and_register_document("A dog chased a cat. A great deal of irrelevant text. A great deal of irrelevant text. A great deal of irrelevant text. A great deal of irrelevant text. A great deal of irrelevant text. A great deal of irrelevant text. A great deal of irrelevant text. A great deal of irrelevant text. A great deal of irrelevant text. A great deal of irrelevant text. A great deal of irrelevant text. A great deal of irrelevant text. A great deal of irrelevant text. A great deal of irrelevant text. A great deal of irrelevant text. A dog chased a cat. A cat. Another irrelevant sentence.")
holmes_manager_coref.parse_and_register_document("Dogs and cats.",
"animals")
topic_match_dictionaries = \
holmes_manager_coref.topic_match_documents_against(
"The dog chased the cat", use_frequency_factor=False)
self.assertEqual(topic_match_dictionaries,
[{'document_label': '', 'text': 'A dog chased a cat. A cat.', 'text_to_match': 'The dog chased the cat', 'rank': '1=', 'index_within_document': 115, 'subword_index': None, 'start_index': 112, 'end_index': 118, 'sentences_start_index': 111, 'sentences_end_index': 119, 'sentences_character_start_index': 515, 'sentences_character_end_index': 541, 'score': 993.4666666666667, 'word_infos': [[2, 5, 'overlapping_relation', False, 'Matches DOG directly.'], [6, 12, 'overlapping_relation', False, 'Matches CHASE directly.'], [15, 18, 'overlapping_relation', True, 'Matches CAT directly.'], [22, 25, 'single', False, 'Matches CAT directly.']], 'answers': []}, {'document_label': '', 'text': 'A dog chased a cat.', 'text_to_match': 'The dog chased the cat', 'rank': '1=', 'index_within_document': 4, 'subword_index': None, 'start_index': 1, 'end_index': 4, 'sentences_start_index': 0, 'sentences_end_index': 5, 'sentences_character_start_index': 0, 'sentences_character_end_index': 19, 'score': 993.4666666666667, 'word_infos': [[2, 5, 'overlapping_relation', False, 'Matches DOG directly.'], [6, 12, 'overlapping_relation', False, 'Matches CHASE directly.'], [15, 18, 'overlapping_relation', True, 'Matches CAT directly.']], 'answers': []}, {'document_label': 'animals', 'text': 'Dogs and cats.', 'text_to_match': 'The dog chased the cat', 'rank': '3', 'index_within_document': 2, 'subword_index': None, 'start_index': 0, 'end_index': 2, 'sentences_start_index': 0, 'sentences_end_index': 3, 'sentences_character_start_index': 0, 'sentences_character_end_index': 14, 'score': 98.66666666666667, 'word_infos': [[0, 4, 'single', False, 'Matches DOG directly.'], [9, 13, 'single', True, 'Matches CAT directly.']], 'answers': []}])
topic_match_dictionaries = \
holmes_manager_coref.topic_match_documents_against(
"The dog chased the cat", tied_result_quotient=0.01, use_frequency_factor=False)
self.assertEqual(topic_match_dictionaries,
[{'document_label': '', 'text': 'A dog chased a cat. A cat.', 'text_to_match': 'The dog chased the cat', 'rank': '1=', 'index_within_document': 115, 'subword_index': None, 'start_index': 112, 'end_index': 118, 'sentences_start_index': 111, 'sentences_end_index': 119, 'sentences_character_start_index': 515, 'sentences_character_end_index': 541, 'score': 993.4666666666667, 'word_infos': [[2, 5, 'overlapping_relation', False, 'Matches DOG directly.'], [6, 12, 'overlapping_relation', False, 'Matches CHASE directly.'], [15, 18, 'overlapping_relation', True, 'Matches CAT directly.'], [22, 25, 'single', False, 'Matches CAT directly.']], 'answers': []}, {'document_label': '', 'text': 'A dog chased a cat.', 'text_to_match': 'The dog chased the cat', 'rank': '1=', 'index_within_document': 4, 'subword_index': None, 'start_index': 1, 'end_index': 4, 'sentences_start_index': 0, 'sentences_end_index': 5, 'sentences_character_start_index': 0, 'sentences_character_end_index': 19, 'score': 993.4666666666667, 'word_infos': [[2, 5, 'overlapping_relation', False, 'Matches DOG directly.'], [6, 12, 'overlapping_relation', False, 'Matches CHASE directly.'], [15, 18, 'overlapping_relation', True, 'Matches CAT directly.']], 'answers': []}, {'document_label': 'animals', 'text': 'Dogs and cats.', 'text_to_match': 'The dog chased the cat', 'rank': '1=', 'index_within_document': 2, 'subword_index': None, 'start_index': 0, 'end_index': 2, 'sentences_start_index': 0, 'sentences_end_index': 3, 'sentences_character_start_index': 0, 'sentences_character_end_index': 14, 'score': 98.66666666666667, 'word_infos': [[0, 4, 'single', False, 'Matches DOG directly.'], [9, 13, 'single', True, 'Matches CAT directly.']], 'answers': []}])
def test_dictionaries_with_multiword_in_relation_not_final(self):
holmes_manager_coref.remove_all_documents()
holmes_manager_coref.remove_all_search_phrases()
holmes_manager_coref.parse_and_register_document(
"Richard Paul Hudson came home")
topic_match_dictionaries = \
holmes_manager_coref.topic_match_documents_against(
"Richard Paul Hudson was coming")
self.assertEqual(topic_match_dictionaries,
[{'document_label': '', 'text': 'Richard Paul Hudson came home', 'text_to_match': 'Richard Paul Hudson was coming', 'rank': '1', 'index_within_document': 3, 'subword_index': None, 'start_index': 0, 'end_index': 3, 'sentences_start_index': 0, 'sentences_end_index': 4, 'sentences_character_start_index': 0, 'sentences_character_end_index': 29, 'score': 369.3333333333333, 'word_infos': [[0, 19, 'relation', False, 'Matches RICHARD PAUL HUDSON directly.'], [20, 24, 'relation', True, 'Matches COME directly.']], 'answers': []}])
def test_dictionaries_with_multiword_alone(self):
holmes_manager_coref.remove_all_documents()
holmes_manager_coref.remove_all_search_phrases()
holmes_manager_coref.parse_and_register_document("Richard Paul Hudson")
topic_match_dictionaries = \
holmes_manager_coref.topic_match_documents_against(
"Richard Paul Hudson")
self.assertEqual(topic_match_dictionaries,
[{'document_label': '', 'text': 'Richard Paul Hudson', 'text_to_match': 'Richard Paul Hudson', 'rank': '1', 'index_within_document': 2, 'subword_index': None, 'start_index': 0, 'end_index': 2, 'sentences_start_index': 0, 'sentences_end_index': 2, 'sentences_character_start_index': 0, 'sentences_character_end_index': 19, 'score': 50, 'word_infos': [[0, 19, 'single', True, 'Matches RICHARD PAUL HUDSON directly.']], 'answers': []}])
def test_dictionaries_with_multiword_alone_and_entity_token_in_text_to_match(self):
holmes_manager_coref.remove_all_documents()
holmes_manager_coref.remove_all_search_phrases()
holmes_manager_coref.parse_and_register_document("Richard Paul Hudson")
topic_match_dictionaries = \
holmes_manager_coref.topic_match_documents_against(
"ENTITYPERSON")
self.assertEqual(topic_match_dictionaries,
[{'document_label': '', 'text': 'Richard Paul Hudson', 'text_to_match': 'ENTITYPERSON', 'rank': '1', 'index_within_document': 2, 'subword_index': None, 'start_index': 0, 'end_index': 2, 'sentences_start_index': 0, 'sentences_end_index': 2, 'sentences_character_start_index': 0, 'sentences_character_end_index': 19, 'score': 50.0, 'word_infos': [[0, 19, 'single', True, 'Has an entity label matching ENTITYPERSON.']], 'answers': []}])
def test_dictionaries_with_multiword_as_single_word_and_relation(self):
holmes_manager_coref.remove_all_documents()
holmes_manager_coref.remove_all_search_phrases()
holmes_manager_coref.parse_and_register_document(
"Can somebody give Harry Potter his present")
topic_match_dictionaries = \
holmes_manager_coref.topic_match_documents_against(
"Somebody gives a present to Harry")
self.assertEqual(topic_match_dictionaries,
[{'document_label': '', 'text': 'Can somebody give Harry Potter his present', 'text_to_match': 'Somebody gives a present to Harry', 'rank': '1', 'index_within_document': 6, 'subword_index': None, 'start_index': 2, 'end_index': 6, 'sentences_start_index': 0, 'sentences_end_index': 6, 'sentences_character_start_index': 0, 'sentences_character_end_index': 42, 'score': 922.1333333333332, 'word_infos': [[13, 17, 'overlapping_relation', False, 'Matches GIVE directly.'], [18, 30, 'overlapping_relation', False, 'Is a synonym of HARRY in the ontology.'], [35, 42, 'overlapping_relation', True, 'Matches PRESENT directly.']], 'answers': []}])
def test_result_ordering_by_match_length_different_documents_1(self):
holmes_manager_coref.remove_all_documents()
holmes_manager_coref.remove_all_search_phrases()
holmes_manager_coref.parse_and_register_document(
"A dog chased a cat.", '1')
holmes_manager_coref.parse_and_register_document(
"A dog chased a cat. A cat.", '2')
topic_matches = holmes_manager_coref.topic_match_documents_against(
"The dog chased the cat")
self.assertEqual(topic_matches[0]['end_index'], 7)
self.assertEqual(topic_matches[1]['end_index'], 4)
def test_filtering_with_topic_matches(self):
holmes_manager_coref.remove_all_documents()
holmes_manager_coref.remove_all_search_phrases()
holmes_manager_coref.parse_and_register_document(
"The dog chased the cat", "T11")
holmes_manager_coref.parse_and_register_document(
"The dog chased the cat", "T12")
holmes_manager_coref.parse_and_register_document(
"The dog chased the cat", "T21")
holmes_manager_coref.parse_and_register_document(
"The dog chased the cat", "T22")
topic_matches = \
holmes_manager_coref.topic_match_documents_against(
"The dog chased the cat")
self.assertEqual(len(topic_matches), 4)
topic_matches = \
holmes_manager_coref.topic_match_documents_against(
"The dog chased the cat", document_label_filter="T")