-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest_tregex.py
1590 lines (1402 loc) · 79.1 KB
/
test_tregex.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
#!/usr/bin/env python3
# translated from https://github.com/stanfordnlp/CoreNLP/blob/efc66a9cf49fecba219dfaa4025315ad966285cc/test/src/edu/stanford/nlp/trees/tregex/TregexTest.java
# last modified at Apr 2, 2022 (https://github.com/stanfordnlp/CoreNLP/commits/efc66a9cf49fecba219dfaa4025315ad966285cc/test/src/edu/stanford/nlp/trees/tregex/TregexTest.java)
from typing import Union
from pytregex.exceptions import ParseException
from pytregex.tree import Tree
from pytregex.tregex import TregexPattern
from .base_tmpl import BaseTmpl
from .base_tmpl import tree as tree_string
class TestTregex(BaseTmpl):
def setUp(self):
self.tree_string = tree_string
return super().setUp()
def test_JoãoSilva(self):
tregex1 = TregexPattern("PNT=p >>- (__=l >, (__=t <- (__=r <, __=m <- (__ <, CONJ <- __=z))))")
tregex2 = TregexPattern(
"PNT=p >>- (/(.+)/#1%var=l >, (__=t <- (__=r <, /(.+)/#1%var=m <- (__ <, CONJ <- /(.+)/#1%var=z))))"
)
tregex3 = TregexPattern("PNT=p >>- (__=l >, (__=t <- (__=r <, ~l <- (__ <, CONJ <- ~l))))")
tree_string = "(T (X (N (N Moe (PNT ,)))) (NP (X (N Curly)) (NP (CONJ and) (X (N Larry)))))"
self.assertTrue(tregex1.findall(tree_string))
# self.assertTrue(tregex2.findall(tree_string))
# self.assertTrue(tregex3.findall(tree_string))
def test_no_results(self):
pMWE = TregexPattern("/^MW/")
self.assertFalse(pMWE.findall("(Foo)"))
def test_one_result(self):
pMWE = TregexPattern("/^MW/")
matches = pMWE.findall("(ROOT (MWE (N 1) (N 2) (N 3)))")
self.assertEqual(1, len(matches))
self.assertEqual("(MWE (N 1) (N 2) (N 3))", matches[0].tostring())
def test_two_results(self):
pMWE = TregexPattern("/^MW/")
matches = pMWE.findall("(ROOT (MWE (N 1) (N 2) (N 3)) (MWV (A B)))")
self.assertEqual(2, len(matches))
self.assertEqual("(MWE (N 1) (N 2) (N 3))", matches[0].tostring())
self.assertEqual("(MWV (A B))", matches[1].tostring())
def test_reuse(self):
"""
a tregex pattern should be able to go more than once. just like me.
"""
pMWE = TregexPattern("/^MW/")
matches = pMWE.findall("(ROOT (MWE (N 1) (N 2) (N 3)) (MWV (A B)))")
self.assertEqual(2, len(matches))
matches = pMWE.findall("(ROOT (MWE (N 1) (N 2) (N 3)))")
self.assertEqual(1, len(matches))
matches = pMWE.findall("(Foo)")
self.assertEqual(0, len(matches))
def test_ith_child(self):
# A is the ith child of B
self.run_test(
"/.*/ >1 root",
"(root (a (foo 1 2) (bar 3 4)) (b (baz 5)))",
"(a (foo 1 2) (bar 3 4))",
)
self.run_test("/.*/ >1 a", "(root (a (foo 1 2) (bar 3 4)) (b (baz 5)))", "(foo 1 2)")
self.run_test("/.*/ >2 a", "(root (a (foo 1 2) (bar 3 4)) (b (baz 5)))", "(bar 3 4)")
self.run_test("/.*/ >1 foo", "(root (a (foo 1 2) (bar 3 4)) (b (baz 5)))", "(1)")
self.run_test("/.*/ >2 foo", "(root (a (foo 1 2) (bar 3 4)) (b (baz 5)))", "(2)")
self.run_test("/.*/ >1 bar", "(root (a (foo 1 2) (bar 3 4)) (b (baz 5)))", "(3)")
self.run_test("/.*/ >2 bar", "(root (a (foo 1 2) (bar 3 4)) (b (baz 5)))", "(4)")
# A is the -ith child of B
self.run_test("/.*/ >-1 root", "(root (a (foo 1 2) (bar 3 4)) (b (baz 5)))", "(b (baz 5))")
self.run_test("/.*/ >-1 a", "(root (a (foo 1 2) (bar 3 4)) (b (baz 5)))", "(bar 3 4)")
self.run_test("/.*/ >-2 a", "(root (a (foo 1 2) (bar 3 4)) (b (baz 5)))", "(foo 1 2)")
self.run_test("/.*/ >-1 foo", "(root (a (foo 1 2) (bar 3 4)) (b (baz 5)))", "(2)")
self.run_test("/.*/ >-2 bar", "(root (a (foo 1 2) (bar 3 4)) (b (baz 5)))", "(3)")
self.run_test("/.*/ >-1 bar", "(root (a (foo 1 2) (bar 3 4)) (b (baz 5)))", "(4)")
self.run_test("/.*/ >-1 b", "(root (a (foo 1 2) (bar 3 4)) (b (baz 5)))", "(baz 5)")
self.run_test("/.*/ >-2 b", "(root (a (foo 1 2) (bar 3 4)) (b (baz 5)))")
# B is the ith child of A
self.run_test("/.*/ <1 root", "(root (a (foo 1 2) (bar 3 4)) (b (baz 5)))")
self.run_test(
"/.*/ <1 a",
"(root (a (foo 1 2) (bar 3 4)) (b (baz 5)))",
"(root (a (foo 1 2) (bar 3 4)) (b (baz 5)))",
)
self.run_test("/.*/ <1 /1/", "(root (a (foo 1 2) (bar 3 4)) (b (baz 5)))", "(foo 1 2)")
self.run_test("/.*/ <1 /2/", "(root (a (foo 1 2) (bar 3 4)) (b (baz 5)))")
self.run_test("/.*/ <1 bar", "(root (a (foo 1 2) (bar 3 4)) (b (baz 5)))")
self.run_test(
"/.*/ <2 bar",
"(root (a (foo 1 2) (bar 3 4)) (b (baz 5)))",
"(a (foo 1 2) (bar 3 4))",
)
self.run_test("/.*/ <3 bar", "(root (a (foo 1 2) (bar 3 4)) (b (baz 5)))")
self.run_test("/.*/ <1 /3/", "(root (a (foo 1 2) (bar 3 4)) (b (baz 5)))", "(bar 3 4)")
self.run_test("/.*/ <1 /4/", "(root (a (foo 1 2) (bar 3 4)) (b (baz 5)))")
self.run_test("/.*/ <2 /4/", "(root (a (foo 1 2) (bar 3 4)) (b (baz 5)))", "(bar 3 4)")
# B is the -ith child of A
self.run_test("/.*/ <-1 root", "(root (a (foo 1 2) (bar 3 4)) (b (baz 5)))")
self.run_test("/.*/ <-1 a", "(root (a (foo 1 2) (bar 3 4)) (b (baz 5)))")
self.run_test(
"/.*/ <-2 a",
"(root (a (foo 1 2) (bar 3 4)) (b (baz 5)))",
"(root (a (foo 1 2) (bar 3 4)) (b (baz 5)))",
)
self.run_test("/.*/ <-1 /1/", "(root (a (foo 1 2) (bar 3 4)) (b (baz 5)))")
self.run_test("/.*/ <-2 /1/", "(root (a (foo 1 2) (bar 3 4)) (b (baz 5)))", "(foo 1 2)")
self.run_test("/.*/ <-1 /2/", "(root (a (foo 1 2) (bar 3 4)) (b (baz 5)))", "(foo 1 2)")
self.run_test("/.*/ <-2 /2/", "(root (a (foo 1 2) (bar 3 4)) (b (baz 5)))")
self.run_test(
"/.*/ <-1 bar",
"(root (a (foo 1 2) (bar 3 4)) (b (baz 5)))",
"(a (foo 1 2) (bar 3 4))",
)
self.run_test("/.*/ <-1 /3/", "(root (a (foo 1 2) (bar 3 4)) (b (baz 5)))")
self.run_test("/.*/ <-2 /3/", "(root (a (foo 1 2) (bar 3 4)) (b (baz 5)))", "(bar 3 4)")
self.run_test("/.*/ <-1 /4/", "(root (a (foo 1 2) (bar 3 4)) (b (baz 5)))", "(bar 3 4)")
def test_test(self):
"""
reruns one of the simpler tests using the test class to make sure
the test class works
"""
pattern = TregexPattern("/^MW/")
self.run_test(
pattern,
"(ROOT (MWE (N 1) (N 2) (N 3)) (MWV (A B)))",
"(MWE (N 1) (N 2) (N 3))",
"(MWV (A B))",
)
def test_word_disjunction(self):
pattern = TregexPattern("a|b|c << bar")
self.run_test(pattern, "(a (bar 1))", "(a (bar 1))")
self.run_test(pattern, "(b (bar 1))", "(b (bar 1))")
self.run_test(pattern, "(c (bar 1))", "(c (bar 1))")
self.run_test(pattern, "(d (bar 1))")
self.run_test(pattern, "(e (bar 1))")
self.run_test(pattern, "(f (bar 1))")
self.run_test(pattern, "(g (bar 1))")
pattern = TregexPattern("a|b|c|d|e|f << bar")
self.run_test(pattern, "(a (bar 1))", "(a (bar 1))")
self.run_test(pattern, "(b (bar 1))", "(b (bar 1))")
self.run_test(pattern, "(c (bar 1))", "(c (bar 1))")
self.run_test(pattern, "(d (bar 1))", "(d (bar 1))")
self.run_test(pattern, "(e (bar 1))", "(e (bar 1))")
self.run_test(pattern, "(f (bar 1))", "(f (bar 1))")
self.run_test(pattern, "(g (bar 1))")
def test_dominates(self):
dominates_pattern = TregexPattern("foo << bar")
self.run_test(dominates_pattern, "(foo (bar 1))", "(foo (bar 1))")
self.run_test(dominates_pattern, "(foo (a (bar 1)))", "(foo (a (bar 1)))")
self.run_test(dominates_pattern, "(foo (a (b (bar 1))))", "(foo (a (b (bar 1))))")
self.run_test(dominates_pattern, "(foo (a (b 1) (bar 2)))", "(foo (a (b 1) (bar 2)))")
self.run_test(dominates_pattern, "(foo (a (b 1) (c 2) (bar 3)))", "(foo (a (b 1) (c 2) (bar 3)))")
self.run_test(dominates_pattern, "(foo (baz 1))")
self.run_test(dominates_pattern, "(a (foo (bar 1)))", "(foo (bar 1))")
self.run_test(dominates_pattern, "(a (foo (baz (bar 1))))", "(foo (baz (bar 1)))")
self.run_test(
dominates_pattern,
"(a (foo (bar 1)) (foo (bar 2)))",
"(foo (bar 1))",
"(foo (bar 2))",
)
dominated_pattern = TregexPattern("foo >> bar")
self.run_test(dominated_pattern, "(foo (bar 1))")
self.run_test(dominated_pattern, "(foo (a (bar 1)))")
self.run_test(dominated_pattern, "(foo (a (b (bar 1))))")
self.run_test(dominated_pattern, "(foo (a (b 1) (bar 2)))")
self.run_test(dominated_pattern, "(foo (a (b 1) (c 2) (bar 3)))")
self.run_test(dominated_pattern, "(bar (foo 1))", "(foo 1)")
self.run_test(dominated_pattern, "(bar (a (foo 1)))", "(foo 1)")
self.run_test(dominated_pattern, "(bar (a (foo (b 1))))", "(foo (b 1))")
self.run_test(dominated_pattern, "(bar (a (foo 1) (foo 2)))", "(foo 1)", "(foo 2)")
self.run_test(dominated_pattern, "(bar (foo (foo 1)))", "(foo (foo 1))", "(foo 1)")
self.run_test(dominated_pattern, "(a (bar (foo 1)))", "(foo 1)")
def test_immediately_dominates(self):
dominates_pattern = TregexPattern("foo < bar")
self.run_test(dominates_pattern, "(foo (bar 1))", "(foo (bar 1))")
self.run_test(dominates_pattern, "(foo (a (bar 1)))")
self.run_test(dominates_pattern, "(a (foo (bar 1)))", "(foo (bar 1))")
self.run_test(dominates_pattern, "(a (foo (baz 1) (bar 2)))", "(foo (baz 1) (bar 2))")
self.run_test(
dominates_pattern,
"(a (foo (bar 1)) (foo (bar 2)))",
"(foo (bar 1))",
"(foo (bar 2))",
)
dominated_pattern = TregexPattern("foo > bar")
self.run_test(dominated_pattern, "(foo (bar 1))")
self.run_test(dominated_pattern, "(foo (a (bar 1)))")
self.run_test(dominated_pattern, "(foo (a (b (bar 1))))")
self.run_test(dominated_pattern, "(foo (a (b 1) (bar 2)))")
self.run_test(dominated_pattern, "(foo (a (b 1) (c 2) (bar 3)))")
self.run_test(dominated_pattern, "(a (foo bar))")
self.run_test(dominated_pattern, "(bar (foo 1))", "(foo 1)")
self.run_test(dominated_pattern, "(bar (a (foo 1)))")
self.run_test(dominated_pattern, "(bar (foo 1) (foo 2))", "(foo 1)", "(foo 2)")
self.run_test(dominated_pattern, "(bar (foo (foo 1)))", "(foo (foo 1))")
self.run_test(dominated_pattern, "(a (bar (foo 1)))", "(foo 1)")
def test_sister(self):
pattern = TregexPattern("/.*/ $ foo")
self.run_test(pattern, "(a (foo 1) (bar 2))", "(bar 2)")
self.run_test(pattern, "(a (bar 1) (foo 2))", "(bar 1)")
self.run_test(pattern, "(a (foo 1) (bar 2) (baz 3))", "(bar 2)", "(baz 3)")
self.run_test(pattern, "(a (foo (bar 2)) (baz 3))", "(baz 3)")
self.run_test(pattern, "(a (foo (bar 2)) (baz (bif 3)))", "(baz (bif 3))")
self.run_test(pattern, "(a (foo (bar 2)))")
self.run_test(pattern, "(a (foo 1))")
pattern = TregexPattern("bar|baz $ foo")
self.run_test(pattern, "(a (foo 1) (bar 2))", "(bar 2)")
self.run_test(pattern, "(a (bar 1) (foo 2))", "(bar 1)")
self.run_test(pattern, "(a (foo 1) (bar 2) (baz 3))", "(bar 2)", "(baz 3)")
self.run_test(pattern, "(a (foo (bar 2)) (baz 3))", "(baz 3)")
self.run_test(pattern, "(a (foo (bar 2)) (baz (bif 3)))", "(baz (bif 3))")
self.run_test(pattern, "(a (foo (bar 2)))")
self.run_test(pattern, "(a (foo 1))")
pattern = TregexPattern("/.*/ $ foo")
self.run_test(pattern, "(a (foo 1) (foo 2))", "(foo 1)", "(foo 2)")
self.run_test(pattern, "(a (foo 1))")
pattern = TregexPattern("foo $ foo")
self.run_test(pattern, "(a (foo 1) (foo 2))", "(foo 1)", "(foo 2)")
self.run_test(pattern, "(a (foo 1))")
pattern = TregexPattern("foo $ foo=a")
matches = pattern.findall("(a (foo 1) (foo 2) (foo 3))")
nodes = pattern.get_nodes("a")
self.assertEqual(6, len(matches))
self.assertEqual("(foo 1)", matches[0].tostring())
self.assertEqual("(foo 2)", nodes[0].tostring())
self.assertEqual("(foo 1)", matches[1].tostring())
self.assertEqual("(foo 3)", nodes[1].tostring())
self.assertEqual("(foo 2)", matches[2].tostring())
self.assertEqual("(foo 1)", nodes[2].tostring())
self.assertEqual("(foo 2)", matches[3].tostring())
self.assertEqual("(foo 3)", nodes[3].tostring())
self.assertEqual("(foo 3)", matches[4].tostring())
self.assertEqual("(foo 1)", nodes[4].tostring())
self.assertEqual("(foo 3)", matches[5].tostring())
self.assertEqual("(foo 2)", nodes[5].tostring())
self.run_test("foo $ foo", "(a (foo 1))")
def test_precedes_follows(self):
# precedes
pattern = TregexPattern("/.*/ .. foo")
self.run_test(pattern, "(a (foo 1) (bar 2))")
self.run_test(pattern, "(a (bar 1) (foo 2))", "(bar 1)", "(1)")
self.run_test(pattern, "(a (bar 1) (baz 2) (foo 3))", "(bar 1)", "(1)", "(baz 2)", "(2)")
self.run_test(pattern, "(a (foo 1) (baz 2) (bar 3))")
self.run_test(pattern, "(a (bar (foo 1)) (baz 2))")
self.run_test(pattern, "(a (bar 1) (baz (foo 2)))", "(bar 1)", "(1)")
self.run_test(pattern, "(a (bar 1) (baz 2) (bif (foo 3)))", "(bar 1)", "(1)", "(baz 2)", "(2)")
self.run_test(pattern, "(a (bar (foo 1)) (baz 2) (bif 3))")
self.run_test(pattern, "(a (bar 1) (baz 2) (foo (bif 3)))", "(bar 1)", "(1)", "(baz 2)", "(2)")
self.run_test(pattern, "(a (bar 1) (foo (bif 2)) (baz 3))", "(bar 1)", "(1)")
# follows
pattern = TregexPattern("/.*/ ,, foo")
self.run_test(pattern, "(a (foo 1) (bar 2))", "(bar 2)", "(2)")
self.run_test(pattern, "(a (bar 1) (foo 2))")
self.run_test(pattern, "(a (bar 1) (baz 2) (foo 3))")
self.run_test(pattern, "(a (foo 1) (baz 2) (bar 3))", "(baz 2)", "(2)", "(bar 3)", "(3)")
self.run_test(pattern, "(a (bar (foo 1)) (baz 2))", "(baz 2)", "(2)")
self.run_test(pattern, "(a (bar 1) (baz (foo 2)))")
self.run_test(pattern, "(a (bar 1) (baz 2) (bif (foo 3)))")
self.run_test(pattern, "(a (bar (foo 1)) (baz 2) (bif 3))", "(baz 2)", "(2)", "(bif 3)", "(3)")
self.run_test(pattern, "(a (bar 1) (baz 2) (foo (bif 3)))")
self.run_test(pattern, "(a (foo (bif 1)) (bar 2) (baz 3))", "(bar 2)", "(2)", "(baz 3)", "(3)")
self.run_test(pattern, "(a (bar 1) (foo (bif 2)) (baz 3))", "(baz 3)", "(3)")
def test_immediate_precedes_follows(self):
# immediate precedes
pattern = TregexPattern("/.*/ . foo")
self.run_test(pattern, "(a (foo 1) (bar 2))")
self.run_test(pattern, "(a (bar 1) (foo 2))", "(bar 1)", "(1)")
self.run_test(pattern, "(a (bar 1) (baz 2) (foo 3))", "(baz 2)", "(2)")
self.run_test(pattern, "(a (foo 1) (baz 2) (bar 3))")
self.run_test(pattern, "(a (bar (foo 1)) (baz 2))")
self.run_test(pattern, "(a (bar 1) (baz (foo 2)))", "(bar 1)", "(1)")
self.run_test(pattern, "(a (bar 1) (baz 2) (bif (foo 3)))", "(baz 2)", "(2)")
self.run_test(pattern, "(a (bar (foo 1)) (baz 2) (bif 3))")
self.run_test(pattern, "(a (bar 1) (baz 2) (foo (bif 3)))", "(baz 2)", "(2)")
self.run_test(pattern, "(a (bar 1) (foo (bif 2)) (baz 3))", "(bar 1)", "(1)")
self.run_test(
pattern,
"(a (bar 1) (foo 2) (baz 3) (foo 4) (bif 5))",
"(bar 1)",
"(1)",
"(baz 3)",
"(3)",
)
self.run_test(pattern, "(a (bar 1) (foo 2) (foo 3) (baz 4))", "(bar 1)", "(1)", "(foo 2)", "(2)")
self.run_test(pattern, "(a (b (c 1) (d 2)) (foo))", "(b (c 1) (d 2))", "(d 2)", "(2)")
self.run_test(pattern, "(a (b (c 1) (d 2)) (bar (foo 3)))", "(b (c 1) (d 2))", "(d 2)", "(2)")
self.run_test(pattern, "(a (b (c 1) (d 2)) (bar (baz 3) (foo 4)))", "(baz 3)", "(3)")
self.run_test(pattern, "(a (b (c 1) (d 2)) (bar (baz 2 3) (foo 4)))", "(baz 2 3)", "(3)")
# immediate follows
pattern = TregexPattern("/.*/ , foo")
self.run_test(pattern, "(a (foo 1) (bar 2))", "(bar 2)", "(2)")
self.run_test(pattern, "(a (bar 1) (foo 2))")
self.run_test(pattern, "(a (bar 1) (baz 2) (foo 3))")
self.run_test(pattern, "(a (foo 1) (baz 2) (bar 3))", "(baz 2)", "(2)")
self.run_test(pattern, "(a (bar (foo 1)) (baz 2))", "(baz 2)", "(2)")
self.run_test(pattern, "(a (bar 1) (baz (foo 2)))")
self.run_test(pattern, "(a (bar 1) (baz 2) (bif (foo 3)))")
self.run_test(pattern, "(a (bar (foo 1)) (baz 2) (bif 3))", "(baz 2)", "(2)")
self.run_test(pattern, "(a (bar 1) (baz 2) (foo (bif 3)))")
self.run_test(pattern, "(a (foo (bif 1)) (bar 2) (baz 3))", "(bar 2)", "(2)")
self.run_test(pattern, "(a (bar 1) (foo (bif 2)) (baz 3))", "(baz 3)", "(3)")
self.run_test(
pattern,
"(a (bar 1) (foo 2) (baz 3) (foo 4) (bif 5))",
"(baz 3)",
"(3)",
"(bif 5)",
"(5)",
)
self.run_test(pattern, "(a (bar 1) (foo 2) (foo 3) (baz 4))", "(foo 3)", "(3)", "(baz 4)", "(4)")
self.run_test(pattern, "(a (foo) (b (c 1) (d 2)))", "(b (c 1) (d 2))", "(c 1)", "(1)")
self.run_test(pattern, "(a (bar (foo 3)) (b (c 1) (d 2)))", "(b (c 1) (d 2))", "(c 1)", "(1)")
self.run_test(
pattern,
"(a (bar (baz 3) (foo 4)) (b (c 1) (d 2)))",
"(b (c 1) (d 2))",
"(c 1)",
"(1)",
)
self.run_test(pattern, "(a (bar (foo 4) (baz 3)) (b (c 1) (d 2)))", "(baz 3)", "(3)")
def test_left_right_most_descendant(self):
# B leftmost descendant of A
self.run_test(
"/.*/ <<, /1/",
"(root (a (foo 1 2) (bar 3 4)) (b (baz 5)))",
"(root (a (foo 1 2) (bar 3 4)) (b (baz 5)))",
"(a (foo 1 2) (bar 3 4))",
"(foo 1 2)",
)
self.run_test("/.*/ <<, /2/", "(root (a (foo 1 2) (bar 3 4)) (b (baz 5)))")
self.run_test(
"/.*/ <<, foo",
"(root (a (foo 1 2) (bar 3 4)) (b (baz 5)))",
"(root (a (foo 1 2) (bar 3 4)) (b (baz 5)))",
"(a (foo 1 2) (bar 3 4))",
)
self.run_test("/.*/ <<, baz", "(root (a (foo 1 2) (bar 3 4)) (b (baz 5)))", "(b (baz 5))")
# B rightmost descendant of A
self.run_test("/.*/ <<- /1/", "(root (a (foo 1 2) (bar 3 4)) (b (baz 5)))")
self.run_test("/.*/ <<- /2/", "(root (a (foo 1 2) (bar 3 4)) (b (baz 5)))", "(foo 1 2)")
self.run_test(
"/.*/ <<- /4/",
"(root (a (foo 1 2) (bar 3 4)) (b (baz 5)))",
"(a (foo 1 2) (bar 3 4))",
"(bar 3 4)",
)
# A leftmost descendant of B
self.run_test(
"/.*/ >>, root",
"(root (a (foo 1 2) (bar 3 4)) (b (baz 5)))",
"(a (foo 1 2) (bar 3 4))",
"(foo 1 2)",
"(1)",
)
self.run_test("/.*/ >>, a", "(root (a (foo 1 2) (bar 3 4)) (b (baz 5)))", "(foo 1 2)", "(1)")
self.run_test("/.*/ >>, bar", "(root (a (foo 1 2) (bar 3 4)) (b (baz 5)))", "(3)")
# A rightmost descendant of B
self.run_test(
"/.*/ >>- root",
"(root (a (foo 1 2) (bar 3 4)) (b (baz 5)))",
"(b (baz 5))",
"(baz 5)",
"(5)",
)
self.run_test("/.*/ >>- a", "(root (a (foo 1 2) (bar 3 4)) (b (baz 5)))", "(bar 3 4)", "(4)")
self.run_test("/.*/ >>- /1/", "(root (a (foo 1 2) (bar 3 4)) (b (baz 5)))")
def test_first_last_child(self):
# A is the first child of B
self.run_test(
"/.*/ >, root",
"(root (a (foo 1 2) (bar 3 4)) (b (baz 5)))",
"(a (foo 1 2) (bar 3 4))",
)
self.run_test("/.*/ >, a", "(root (a (foo 1 2) (bar 3 4)) (b (baz 5)))", "(foo 1 2)")
self.run_test("/.*/ >, foo", "(root (a (foo 1 2) (bar 3 4)) (b (baz 5)))", "(1)")
self.run_test("/.*/ >, bar", "(root (a (foo 1 2) (bar 3 4)) (b (baz 5)))", "(3)")
# A is the last child of B
self.run_test("/.*/ >- root", "(root (a (foo 1 2) (bar 3 4)) (b (baz 5)))", "(b (baz 5))")
self.run_test("/.*/ >- a", "(root (a (foo 1 2) (bar 3 4)) (b (baz 5)))", "(bar 3 4)")
self.run_test("/.*/ >- foo", "(root (a (foo 1 2) (bar 3 4)) (b (baz 5)))", "(2)")
self.run_test("/.*/ >- bar", "(root (a (foo 1 2) (bar 3 4)) (b (baz 5)))", "(4)")
self.run_test("/.*/ >- b", "(root (a (foo 1 2) (bar 3 4)) (b (baz 5)))", "(baz 5)")
# B is the first child of A
self.run_test("/.*/ <, root", "(root (a (foo 1 2) (bar 3 4)) (b (baz 5)))")
self.run_test(
"/.*/ <, a",
"(root (a (foo 1 2) (bar 3 4)) (b (baz 5)))",
"(root (a (foo 1 2) (bar 3 4)) (b (baz 5)))",
)
self.run_test("/.*/ <, /1/", "(root (a (foo 1 2) (bar 3 4)) (b (baz 5)))", "(foo 1 2)")
self.run_test("/.*/ <, /2/", "(root (a (foo 1 2) (bar 3 4)) (b (baz 5)))")
self.run_test("/.*/ <, bar", "(root (a (foo 1 2) (bar 3 4)) (b (baz 5)))")
self.run_test("/.*/ <, /3/", "(root (a (foo 1 2) (bar 3 4)) (b (baz 5)))", "(bar 3 4)")
self.run_test("/.*/ <, /4/", "(root (a (foo 1 2) (bar 3 4)) (b (baz 5)))")
# B is the last child of A
self.run_test("/.*/ <- root", "(root (a (foo 1 2) (bar 3 4)) (b (baz 5)))")
self.run_test("/.*/ <- a", "(root (a (foo 1 2) (bar 3 4)) (b (baz 5)))")
self.run_test("/.*/ <- /1/", "(root (a (foo 1 2) (bar 3 4)) (b (baz 5)))")
self.run_test("/.*/ <- /2/", "(root (a (foo 1 2) (bar 3 4)) (b (baz 5)))", "(foo 1 2)")
self.run_test(
"/.*/ <- bar",
"(root (a (foo 1 2) (bar 3 4)) (b (baz 5)))",
"(a (foo 1 2) (bar 3 4))",
)
self.run_test("/.*/ <- /3/", "(root (a (foo 1 2) (bar 3 4)) (b (baz 5)))")
self.run_test("/.*/ <- /4/", "(root (a (foo 1 2) (bar 3 4)) (b (baz 5)))", "(bar 3 4)")
def test_only_child(self):
self.run_test("foo <: bar", "(foo (bar 1))", "(foo (bar 1))")
self.run_test("foo <: bar", "(foo (bar 1) (bar 2))")
self.run_test("foo <: bar", "(foo)")
self.run_test("foo <: bar", "(foo (baz (bar)))")
self.run_test("foo <: bar", "(foo 1)")
self.run_test("bar >: foo", "(foo (bar 1))", "(bar 1)")
self.run_test("bar >: foo", "(foo (bar 1) (bar 2))")
self.run_test("bar >: foo", "(foo)")
self.run_test("bar >: foo", "(foo (baz (bar)))")
self.run_test("bar >: foo", "(bar (foo 1))")
self.run_test("/.*/ >: foo", "(a (foo (bar 1)) (foo (baz 2)))", "(bar 1)", "(baz 2)")
def test_preceding_following_sister(self):
# test preceding sisters
preceding = TregexPattern("/.*/ $.. baz")
self.run_test(preceding, "(a (foo 1) (bar 2) (baz 3))", "(foo 1)", "(bar 2)")
self.run_test(preceding, "(root (b (foo 1)) (a (foo 1) (bar 2) (baz 3)))", "(foo 1)", "(bar 2)")
self.run_test(preceding, "(root (a (foo 1) (bar 2) (baz 3)) (b (foo 1)))", "(foo 1)", "(bar 2)")
self.run_test(preceding, "(a (foo 1) (baz 2) (bar 3))", "(foo 1)")
self.run_test(preceding, "(a (baz 1) (foo 2) (bar 3))")
# test immediately preceding sisters
impreceding = TregexPattern("/.*/ $. baz")
self.run_test(impreceding, "(a (foo 1) (bar 2) (baz 3))", "(bar 2)")
self.run_test(impreceding, "(root (b (foo 1)) (a (foo 1) (bar 2) (baz 3)))", "(bar 2)")
self.run_test(impreceding, "(root (a (foo 1) (bar 2) (baz 3)) (b (foo 1)))", "(bar 2)")
self.run_test(impreceding, "(a (foo 1) (baz 2) (bar 3))", "(foo 1)")
self.run_test(impreceding, "(a (baz 1) (foo 2) (bar 3))")
# test following sisters
following = TregexPattern("/.*/ $,, baz")
self.run_test(following, "(a (foo 1) (bar 2) (baz 3))")
self.run_test(following, "(root (b (foo 1)) (a (foo 1) (bar 2) (baz 3)))")
self.run_test(following, "(root (a (foo 1) (bar 2) (baz 3)) (b (foo 1)))")
self.run_test(following, "(root (a (baz 1) (bar 2) (foo 3)) (b (foo 1)))", "(bar 2)", "(foo 3)")
self.run_test(following, "(a (foo 1) (baz 2) (bar 3))", "(bar 3)")
self.run_test(following, "(a (baz 1) (foo 2) (bar 3))", "(foo 2)", "(bar 3)")
# test immediately following sisters
imfollowing = TregexPattern("/.*/ $, baz")
self.run_test(imfollowing, "(a (foo 1) (bar 2) (baz 3))")
self.run_test(imfollowing, "(root (b (foo 1)) (a (foo 1) (bar 2) (baz 3)))")
self.run_test(imfollowing, "(root (a (foo 1) (bar 2) (baz 3)) (b (foo 1)))")
self.run_test(imfollowing, "(root (a (baz 1) (bar 2) (foo 3)) (b (foo 1)))", "(bar 2)")
self.run_test(imfollowing, "(a (foo 1) (baz 2) (bar 3))", "(bar 3)")
self.run_test(imfollowing, "(a (baz 1) (foo 2) (bar 3))", "(foo 2)")
# TODO
# def test_category_functions(self):
# Function<String, String> fooCategory = new Function<String, String>()
# public String apply(String label)
# if (label == null)
# return label
#
# if (label.equals("bar"))
# return "foo"
#
# return label
#
#
# TregexPatternCompiler fooCompiler = new TregexPatternCompiler(fooCategory)
#
# TregexPattern fooTregex = fooCompiler("@foo > bar")
# self.run_test(fooTregex, "(bar (foo 0))", "(foo 0)")
# self.run_test(fooTregex, "(bar (bar 0))", "(bar 0)")
# self.run_test(fooTregex, "(foo (foo 0))")
# self.run_test(fooTregex, "(foo (bar 0))")
#
# Function<String, String> barCategory = new Function<String, String>()
# public String apply(String label)
# if (label == null)
# return label
#
# if (label.equals("foo"))
# return "bar"
#
# return label
#
#
# TregexPatternCompiler barCompiler = new TregexPatternCompiler(barCategory)
#
# TregexPattern barTregex = barCompiler("@bar > foo")
# self.run_test(barTregex, "(bar (foo 0))")
# self.run_test(barTregex, "(bar (bar 0))")
# self.run_test(barTregex, "(foo (foo 0))", "(foo 0)")
# self.run_test(barTregex, "(foo (bar 0))", "(bar 0)")
#
# # These should still work, since the tregex patterns have
# # different category functions. Old enough versions of tregex do
# # not allow for that.
# self.run_test(fooTregex, "(bar (foo 0))", "(foo 0)")
# self.run_test(fooTregex, "(bar (bar 0))", "(bar 0)")
# self.run_test(fooTregex, "(foo (foo 0))")
# self.run_test(fooTregex, "(foo (bar 0))")
def test_dominate_unary_chain(self):
self.run_test("foo <<: bar", "(a (foo (b (c (d (bar))))))", "(foo (b (c (d (bar)))))")
self.run_test("foo <<: bar", "(a (foo (b (c (d (bar) (baz))))))")
self.run_test("foo <<: bar", "(a (foo (b (c (d (bar)) (baz)))))")
self.run_test("foo <<: bar", "(a (foo (b (c (d (bar))) (baz))))")
self.run_test("foo <<: bar", "(a (foo (b (c (d (bar)))) (baz)))")
self.run_test("foo <<: bar", "(a (foo (b (c (d (bar))))) (baz))", "(foo (b (c (d (bar)))))")
self.run_test("foo <<: bar", "(a (foo (b (c (bar)))))", "(foo (b (c (bar))))")
self.run_test("foo <<: bar", "(a (foo (b (bar))))", "(foo (b (bar)))")
self.run_test("foo <<: bar", "(a (foo (bar)))", "(foo (bar))")
self.run_test("bar >>: foo", "(a (foo (b (c (d (bar))))))", "(bar)")
self.run_test("bar >>: foo", "(a (foo (b (c (d (bar) (baz))))))")
self.run_test("bar >>: foo", "(a (foo (b (c (d (bar)) (baz)))))")
self.run_test("bar >>: foo", "(a (foo (b (c (d (bar))) (baz))))")
self.run_test("bar >>: foo", "(a (foo (b (c (d (bar)))) (baz)))")
self.run_test("bar >>: foo", "(a (foo (b (c (d (bar))))) (baz))", "(bar)")
self.run_test("bar >>: foo", "(a (foo (b (c (bar)))))", "(bar)")
self.run_test("bar >>: foo", "(a (foo (b (bar))))", "(bar)")
self.run_test("bar >>: foo", "(a (foo (bar)))", "(bar)")
def test_precedes_described_chain(self):
self.run_test("DT .+(JJ) NN", "(NP (DT the) (JJ large) (JJ green) (NN house))", "(DT the)")
self.run_test(
"DT .+(@JJ) /^NN/",
"(NP (PDT both) (DT the) (JJ-SIZE large) (JJ-COLOUR green) (NNS houses))",
"(DT the)",
)
self.run_test("NN ,+(JJ) DT", "(NP (DT the) (JJ large) (JJ green) (NN house))", "(NN house)")
self.run_test(
"NNS ,+(@JJ) /^DT/",
"(NP (PDT both) (DT the) (JJ-SIZE large) (JJ-COLOUR green) (NNS houses))",
"(NNS houses)",
)
self.run_test(
"NNS ,+(/^(JJ|DT).*$/) PDT",
"(NP (PDT both) (DT the) (JJ-SIZE large) (JJ-COLOUR green) (NNS houses))",
"(NNS houses)",
)
self.run_test(
"NNS ,+(@JJ) JJ",
"(NP (PDT both) (DT the) (JJ large) (JJ-COLOUR green) (NNS houses))",
"(NNS houses)",
)
def test_dominate_described_chain(self):
self.run_test("foo <+(bar) baz", "(a (foo (baz)))", "(foo (baz))")
self.run_test("foo <+(bar) baz", "(a (foo (bar (baz))))", "(foo (bar (baz)))")
self.run_test("foo <+(bar) baz", "(a (foo (bar (bar (baz)))))", "(foo (bar (bar (baz))))")
self.run_test("foo <+(bar) baz", "(a (foo (bif (baz))))")
self.run_test("foo <+(!bif) baz", "(a (foo (bif (baz))))")
self.run_test("foo <+(!bif) baz", "(a (foo (bar (baz))))", "(foo (bar (baz)))")
self.run_test("foo <+(/b/) baz", "(a (foo (bif (baz))))", "(foo (bif (baz)))")
self.run_test("foo <+(/b/) baz", "(a (foo (bar (bif (baz)))))", "(foo (bar (bif (baz))))")
self.run_test(
"foo <+(bar) baz",
"(a (foo (bar (blah 1) (bar (baz)))))",
"(foo (bar (blah 1) (bar (baz))))",
)
self.run_test("baz >+(bar) foo", "(a (foo (baz)))", "(baz)")
self.run_test("baz >+(bar) foo", "(a (foo (bar (baz))))", "(baz)")
self.run_test("baz >+(bar) foo", "(a (foo (bar (bar (baz)))))", "(baz)")
self.run_test("baz >+(bar) foo", "(a (foo (bif (baz))))")
self.run_test("baz >+(!bif) foo", "(a (foo (bif (baz))))")
self.run_test("baz >+(!bif) foo", "(a (foo (bar (baz))))", "(baz)")
self.run_test("baz >+(/b/) foo", "(a (foo (bif (baz))))", "(baz)")
self.run_test("baz >+(/b/) foo", "(a (foo (bar (bif (baz)))))", "(baz)")
self.run_test("baz >+(bar) foo", "(a (foo (bar (blah 1) (bar (baz)))))", "(baz)")
def test_segmented_and_equals_expressions(self):
self.run_test("foo : bar", "(a (foo) (bar))", "(foo)")
self.run_test("foo : bar", "(a (foo))")
self.run_test("(foo << bar) : (foo << baz)", "(a (foo (bar 1)) (foo (baz 2)))", "(foo (bar 1))")
self.run_test("(foo << ban) $++ (foo << baz)", "(a (foo (bar 1)) (foo (baz 2)))")
self.run_test("(foo << bar) == (foo << baz)", "(a (foo (bar)) (foo (baz)))")
self.run_test("(foo << bar) : (foo << baz)", "(a (foo (bar) (baz)))", "(foo (bar) (baz))")
self.run_test("(foo << bar) == (foo << baz)", "(a (foo (bar) (baz)))", "(foo (bar) (baz))")
self.run_test("(foo << bar) : (baz >> a)", "(a (foo (bar) (baz)))", "(foo (bar) (baz))")
self.run_test("(foo << bar) == (baz >> a)", "(a (foo (bar) (baz)))")
self.run_test("foo == foo", "(a (foo (bar)))", "(foo (bar))")
self.run_test("foo << bar == foo", "(a (foo (bar)) (foo (baz)))", "(foo (bar))")
self.run_test("foo << bar == foo", "(a (foo (bar) (baz)))", "(foo (bar) (baz))")
self.run_test("foo << bar == foo << baz", "(a (foo (bar) (baz)))", "(foo (bar) (baz))")
self.run_test("foo << bar : (foo << baz)", "(a (foo (bar)) (foo (baz)))", "(foo (bar))")
def test_two_children(self):
self.run_test("foo << bar << baz", "(foo (bar 1) (baz 2))", "(foo (bar 1) (baz 2))")
# this is a poorly written pattern that will match 4 times
self.run_test(
"foo << __ << baz",
"(foo (bar 1) (baz 2))",
"(foo (bar 1) (baz 2))",
"(foo (bar 1) (baz 2))",
"(foo (bar 1) (baz 2))",
"(foo (bar 1) (baz 2))",
)
# this one also matches 4 times
self.run_test(
"foo << bar << __",
"(foo (bar 1) (baz 2))",
"(foo (bar 1) (baz 2))",
"(foo (bar 1) (baz 2))",
"(foo (bar 1) (baz 2))",
"(foo (bar 1) (baz 2))",
)
# this one also matches 4 times
self.run_test(
"foo << __ << __",
"(foo (bar 1))",
"(foo (bar 1))",
"(foo (bar 1))",
"(foo (bar 1))",
"(foo (bar 1))",
)
# same thing, just making sure variable assignment doesn't throw
# it off
self.run_test(
"foo << __=a << __=b",
"(foo (bar 1))",
"(foo (bar 1))",
"(foo (bar 1))",
"(foo (bar 1))",
"(foo (bar 1))",
)
# 16 times! hopefully no one writes patterns like this
self.run_test(
"foo << __ << __",
"(foo (bar 1) (baz 2))",
"(foo (bar 1) (baz 2))",
"(foo (bar 1) (baz 2))",
"(foo (bar 1) (baz 2))",
"(foo (bar 1) (baz 2))",
"(foo (bar 1) (baz 2))",
"(foo (bar 1) (baz 2))",
"(foo (bar 1) (baz 2))",
"(foo (bar 1) (baz 2))",
"(foo (bar 1) (baz 2))",
"(foo (bar 1) (baz 2))",
"(foo (bar 1) (baz 2))",
"(foo (bar 1) (baz 2))",
"(foo (bar 1) (baz 2))",
"(foo (bar 1) (baz 2))",
"(foo (bar 1) (baz 2))",
"(foo (bar 1) (baz 2))",
)
# note: this matches because we set a=(bar 1), b=(1)
# TODO
# self.run_test("(foo << __=a << __=b) : (=a !== =b)", "(foo (bar 1))",
# "(foo (bar 1))", "(foo (bar 1))")
# self.run_test("(foo < __=a < __=b) : (=a !== =b)", "(foo (bar 1))")
# TODO
# 12 times: 16 possible ways to match the nodes, but 4 of them
# are ruled out because they are the same node matching twice
# self.run_test("(foo << __=a << __=b) : (=a !== =b)",
# "(foo (bar 1) (baz 2))",
# "(foo (bar 1) (baz 2))", "(foo (bar 1) (baz 2))",
# "(foo (bar 1) (baz 2))", "(foo (bar 1) (baz 2))",
# "(foo (bar 1) (baz 2))", "(foo (bar 1) (baz 2))",
# "(foo (bar 1) (baz 2))", "(foo (bar 1) (baz 2))",
# "(foo (bar 1) (baz 2))", "(foo (bar 1) (baz 2))",
# "(foo (bar 1) (baz 2))", "(foo (bar 1) (baz 2))")
# TODO
# would need three unique descendants, but we only have two, so
# this pattern doesn't match anything
# self.run_test("(foo << __=a << __=b << __=c) : " +
# "(=a !== =b) : (=a !== =c) : (=b !== =c)",
# "(foo (bar 1))")
# TODO: this should work, but it doesn't even parse
# self.run_test("foo << __=a << __=b : !(=a == =b)", "(foo (bar 1))")
def test_named(self):
tree_string = "(a (foo 1) (bar 2) (bar 3))"
pattern = TregexPattern("foo=a $ bar=b")
matches = pattern.findall(tree_string)
self.assertEqual(2, len(matches))
self.assertEqual("(foo 1)", matches[0].tostring())
self.assertEqual("(foo 1)", pattern.get_nodes("a")[0].tostring())
self.assertEqual("(bar 2)", pattern.get_nodes("b")[0].tostring())
self.assertEqual("(foo 1)", matches[1].tostring())
self.assertEqual("(foo 1)", pattern.get_nodes("a")[1].tostring())
self.assertEqual("(bar 3)", pattern.get_nodes("b")[1].tostring())
def test_month_day_year(self):
"""
An example from our code which looks for month-day-year patterns
in PTB. Relies on the pattern splitting and variable matching
features.
"""
MONTH_REGEX = "January|February|March|April|May|June|July|August|September|October|November|December|Jan\\.|Feb\\.|Mar\\.|Apr\\.|Aug\\.|Sep\\.|Sept\\.|Oct\\.|Nov\\.|Dec\\."
testPattern = (
"NP=root <1 (NP=monthdayroot <1 (NNP=month <: /"
+ MONTH_REGEX
+ "/) <2 (CD=day <: __)) <2 (/^,$/=comma <: /^,$/) <3 (NP=yearroot <: (CD=year <:"
" __)) : (=root <- =yearroot) : (=monthdayroot <- =day)"
)
# TODO
# self.run_test(testPattern, "(ROOT (S (NP (NNP Mr.) (NNP Good)) (VP (VBZ devotes) (NP (RB much) (JJ serious) (NN space)) (PP (TO to) (NP (NP (DT the) (NNS events)) (PP (IN of) (NP (NP (NP (NNP Feb.) (CD 25)) (, ,) (NP (CD 1942))) (, ,) (SBAR (WHADVP (WRB when)) (S (NP (JJ American) (NNS gunners)) (VP (VBD spotted) (NP (NP (JJ strange) (NNS lights)) (PP (IN in) (NP (NP (DT the) (NN sky)) (PP (IN above) (NP (NNP Los) (NNP Angeles)))))))))))))) (. .)))", "(NP (NP (NNP Feb.) (CD 25)) (, ,) (NP (CD 1942)))")
# self.run_test(testPattern, "(ROOT (S (NP (DT The) (JJ preferred) (NNS shares)) (VP (MD will) (VP (VB carry) (NP (NP (DT a) (JJ floating) (JJ annual) (NN dividend)) (ADJP (JJ equal) (PP (TO to) (NP (NP (CD 72) (NN %)) (PP (IN of) (NP (NP (DT the) (JJ 30-day) (NNS bankers) (POS ')) (NN acceptance) (NN rate))))))) (PP (IN until) (NP (NP (NNP Dec.) (CD 31)) (, ,) (NP (CD 1994)))))) (. .)))", "(NP (NP (NNP Dec.) (CD 31)) (, ,) (NP (CD 1994)))")
# self.run_test(testPattern, "(ROOT (S (NP (PRP It)) (VP (VBD said) (SBAR (S (NP (NN debt)) (VP (VBD remained) (PP (IN at) (NP (NP (DT the) (QP ($ $) (CD 1.22) (CD billion))) (SBAR (WHNP (DT that)) (S (VP (VBZ has) (VP (VBD prevailed) (PP (IN since) (NP (JJ early) (CD 1989))))))))) (, ,) (SBAR (IN although) (S (NP (IN that)) (VP (VBN compared) (PP (IN with) (NP (NP (QP ($ $) (CD 911) (CD million))) (PP (IN at) (NP (NP (NNP Sept.) (CD 30)) (, ,) (NP (CD 1988))))))))))))) (. .)))", "(NP (NP (NNP Sept.) (CD 30)) (, ,) (NP (CD 1988)))")
# self.run_test(testPattern, "(ROOT (S (NP (DT The) (JJ new) (NNS notes)) (VP (MD will) (VP (VB bear) (NP (NN interest)) (PP (PP (IN at) (NP (NP (CD 5.5) (NN %)) (PP (IN through) (NP (NP (NNP July) (CD 31)) (, ,) (NP (CD 1991)))))) (, ,) (CC and) (ADVP (RB thereafter)) (PP (IN at) (NP (CD 10) (NN %)))))) (. .)))", "(NP (NP (NNP July) (CD 31)) (, ,) (NP (CD 1991)))")
# self.run_test(testPattern, "(ROOT (S (NP (NP (NNP Francis) (NNP M.) (NNP Wheat)) (, ,) (NP (NP (DT a) (JJ former) (NNPS Securities)) (CC and) (NP (NNP Exchange) (NNP Commission) (NN member))) (, ,)) (VP (VBD headed) (NP (NP (DT the) (NN panel)) (SBAR (WHNP (WDT that)) (S (VP (VBD had) (VP (VP (VBN studied) (NP (DT the) (NNS issues)) (PP (IN for) (NP (DT a) (NN year)))) (CC and) (VP (VBD proposed) (NP (DT the) (NNP FASB)) (PP (IN on) (NP (NP (NNP March) (CD 30)) (, ,) (NP (CD 1972))))))))))) (. .)))", "(NP (NP (NNP March) (CD 30)) (, ,) (NP (CD 1972)))")
# self.run_test(testPattern, "(ROOT (S (NP (DT The) (NNP FASB)) (VP (VBD had) (NP (PRP$ its) (JJ initial) (NN meeting)) (PP (IN on) (NP (NP (NNP March) (CD 28)) (, ,) (NP (CD 1973))))) (. .)))", "(NP (NP (NNP March) (CD 28)) (, ,) (NP (CD 1973)))")
# self.run_test(testPattern, "(ROOT (S (S (PP (IN On) (NP (NP (NNP Dec.) (CD 13)) (, ,) (NP (CD 1973)))) (, ,) (NP (PRP it)) (VP (VBD issued) (NP (PRP$ its) (JJ first) (NN rule)))) (: ;) (S (NP (PRP it)) (VP (VBD required) (S (NP (NNS companies)) (VP (TO to) (VP (VB disclose) (NP (NP (JJ foreign) (NN currency) (NNS translations)) (PP (IN in) (NP (NNP U.S.) (NNS dollars))))))))) (. .)))", "(NP (NP (NNP Dec.) (CD 13)) (, ,) (NP (CD 1973)))")
# self.run_test(testPattern, "(ROOT (S (NP (NP (NNP Fidelity) (NNPS Investments)) (, ,) (NP (NP (DT the) (NN nation) (POS 's)) (JJS largest) (NN fund) (NN company)) (, ,)) (VP (VBD said) (SBAR (S (NP (NN phone) (NN volume)) (VP (VBD was) (NP (NP (QP (RBR more) (IN than) (JJ double)) (PRP$ its) (JJ typical) (NN level)) (, ,) (CC but) (ADVP (RB still)) (NP (NP (NN half) (DT that)) (PP (IN of) (NP (NP (NNP Oct.) (CD 19)) (, ,) (NP (CD 1987)))))))))) (. .)))", "(NP (NP (NNP Oct.) (CD 19)) (, ,) (NP (CD 1987)))")
# self.run_test(testPattern, "(ROOT (S (NP (JJ SOFT) (NN CONTACT) (NNS LENSES)) (VP (VP (VBP WON) (NP (JJ federal) (NN blessing)) (PP (IN on) (NP (NP (NNP March) (CD 18)) (, ,) (NP (CD 1971))))) (, ,) (CC and) (VP (ADVP (RB quickly)) (VBD became) (NP (NN eye) (NNS openers)) (PP (IN for) (NP (PRP$ their) (NNS makers))))) (. .)))", "(NP (NP (NNP March) (CD 18)) (, ,) (NP (CD 1971)))")
# self.run_test(testPattern, "(ROOT (NP (NP (NP (VBN Annualized) (NN interest) (NNS rates)) (PP (IN on) (NP (JJ certain) (NNS investments))) (SBAR (IN as) (S (VP (VBN reported) (PP (IN by) (NP (DT the) (NNP Federal) (NNP Reserve) (NNP Board))) (PP (IN on) (NP (DT a) (JJ weekly-average) (NN basis))))))) (: :) (NP-TMP (NP (CD 1989)) (CC and) (NP (NP (NNP Wednesday)) (NP (NP (NNP October) (CD 4)) (, ,) (NP (CD 1989))))) (. .)))", "(NP (NP (NNP October) (CD 4)) (, ,) (NP (CD 1989)))")
# self.run_test(testPattern, "(ROOT (S (S (ADVP (RB Together))) (, ,) (NP (DT the) (CD two) (NNS stocks)) (VP (VP (VBD wreaked) (NP (NN havoc)) (PP (IN among) (NP (NN takeover) (NN stock) (NNS traders)))) (, ,) (CC and) (VP (VBD caused) (NP (NP (DT a) (ADJP (CD 7.3) (NN %)) (NN drop)) (PP (IN in) (NP (DT the) (NNP Dow) (NNP Jones) (NNP Transportation) (NNP Average))) (, ,) (ADJP (JJ second) (PP (IN in) (NP (NN size))) (PP (RB only) (TO to) (NP (NP (DT the) (NN stock-market) (NN crash)) (PP (IN of) (NP (NP (NNP Oct.) (CD 19)) (, ,) (NP (CD 1987)))))))))) (. .)))", "(NP (NP (NNP Oct.) (CD 19)) (, ,) (NP (CD 1987)))")
def test_doc_examples(self):
self.run_test("S < VP < NP", "(S (VP) (NP))", "(S (VP) (NP))")
self.run_test("S < VP < NP", "(a (S (VP) (NP)) (S (NP) (VP)))", "(S (VP) (NP))", "(S (NP) (VP))")
self.run_test("S < VP < NP", "(S (VP (NP)))")
self.run_test("S < VP & < NP", "(S (VP) (NP))", "(S (VP) (NP))")
self.run_test("S < VP & < NP", "(a (S (VP) (NP)) (S (NP) (VP)))", "(S (VP) (NP))", "(S (NP) (VP))")
self.run_test("S < VP & < NP", "(S (VP (NP)))")
self.run_test("S < VP << NP", "(S (VP (NP)))", "(S (VP (NP)))")
self.run_test("S < VP << NP", "(S (VP) (foo NP))", "(S (VP) (foo NP))")
self.run_test("S < (VP < NP)", "(S (VP (NP)))", "(S (VP (NP)))")
self.run_test("S < (NP $++ VP)", "(S (NP) (VP))", "(S (NP) (VP))")
self.run_test("S < (NP $++ VP)", "(S (NP VP))")
self.run_test("(NP < NN || < NNS)", "((NP NN) (NP foo) (NP NNS))", "(NP NN)", "(NP NNS)")
self.run_test(
"(NP (< NN || < NNS) & > S)",
"(foo (S (NP NN) (NP foo) (NP NNS)) (NP NNS))",
"(NP NN)",
"(NP NNS)",
)
self.run_test(
"(NP [< NN || < NNS] & > S)",
"(foo (S (NP NN) (NP foo) (NP NNS)) (NP NNS))",
"(NP NN)",
"(NP NNS)",
)
def test_complex(self):
"""
More complex tests, often based on examples from our source code
"""
test_pattern = "S < (NP=m1 $.. (VP < ((/VB/ < /^(am|are|is|was|were|'m|'re|'s|be)$/) $.. NP=m2)))"
test_tree = (
"(S (NP (NP (DT The) (JJ next) (NN stop)) (PP (IN on) (NP (DT the) (NN itinerary))))"
" (VP (VBD was) (NP (NP (NNP Chad)) (, ,) (SBAR (WHADVP (WRB where)) (S (NP (NNP"
" Chen)) (VP (VBD dined) (PP (IN with) (NP (NP (NNP Chad) (POS 's)) (NNP President)"
" (NNP Idris) (NNP Debi)))))))) (. .))"
)
self.run_test(test_pattern, "(ROOT " + test_tree + ")", test_tree)
test_tree = (
"(S (NP (NNP Chen) (NNP Shui) (HYPH -) (NNP bian)) (VP (VBZ is) (NP (NP (DT the) (JJ"
" first) (NML (NNP ROC) (NN president))) (SBAR (S (ADVP (RB ever)) (VP (TO to) (VP"
" (VB travel) (PP (IN to) (NP (JJ western) (NNP Africa))))))))) (. .))"
)
self.run_test(test_pattern, "(ROOT " + test_tree + ")", test_tree)
test_tree = (
"(ROOT (S (NP (PRP$ My) (NN dog)) (VP (VBZ is) (VP (VBG eating) (NP (DT a) (NN sausage)))) (. .)))"
)
self.run_test(test_pattern, test_tree)
test_tree = "(ROOT (S (NP (PRP He)) (VP (MD will) (VP (VB be) (ADVP (RB here) (RB soon)))) (. .)))"
self.run_test(test_pattern, test_tree)
test_pattern = "/^NP(?:-TMP|-ADV)?$/=m1 < (NP=m2 $- /^,$/ $-- NP=m3 !$ CC|CONJP)"
test_tree = (
"(ROOT (S (NP (NP (NP (NP (DT The) (NNP ROC) (POS 's)) (NN ambassador)) (PP (IN to)"
" (NP (NNP Nicaragua)))) (, ,) (NP (NNP Antonio) (NNP Tsai)) (, ,)) (ADVP (RB"
" bluntly)) (VP (VBD argued) (PP (IN in) (NP (NP (DT a) (NN briefing)) (PP (IN with)"
" (NP (NNP Chen))))) (SBAR (IN that) (S (NP (NP (NP (NNP Taiwan) (POS 's)) (JJ"
" foreign) (NN assistance)) (PP (IN to) (NP (NNP Nicaragua)))) (VP (VBD was) (VP"
" (VBG being) (ADJP (JJ misused))))))) (. .)))"
)
expected_result = (
"(NP (NP (NP (NP (DT The) (NNP ROC) (POS 's)) (NN ambassador)) (PP (IN to) (NP (NNP"
" Nicaragua)))) (, ,) (NP (NNP Antonio) (NNP Tsai)) (, ,))"
)
self.run_test(test_pattern, test_tree, expected_result)
test_tree = (
"(ROOT (S (PP (IN In) (NP (NP (DT the) (NN opinion)) (PP (IN of) (NP (NP (NNP"
" Norman) (NNP Hsu)) (, ,) (NP (NP (NN vice) (NN president)) (PP (IN of) (NP (NP (DT"
" a) (NNS foods) (NN company)) (SBAR (WHNP (WHNP (WP$ whose) (NN family)) (PP (IN"
" of) (NP (CD four)))) (S (VP (VBD had) (VP (VBN spent) (NP (QP (DT a) (JJ few))"
" (NNS years)) (PP (IN in) (NP (NNP New) (NNP Zealand))) (PP (IN before) (S (VP (VBG"
' moving) (PP (IN to) (NP (NNP Dongguan))))))))))))))))) (, ,) (`` ") (NP (NP (DT'
" The) (JJ first) (NN thing)) (VP (TO to) (VP (VB do)))) (VP (VBZ is) (S (VP (VB"
" ask) (NP (DT the) (NNS children)) (NP (PRP$ their) (NN reason)) (PP (IN for) (S"
" (VP (VBG saying) (NP (JJ such) (NNS things)))))))) (. .)))"
)
expected_result = (
"(NP (NP (NNP Norman) (NNP Hsu)) (, ,) (NP (NP (NN vice) (NN president)) (PP (IN of)"
" (NP (NP (DT a) (NNS foods) (NN company)) (SBAR (WHNP (WHNP (WP$ whose) (NN"
" family)) (PP (IN of) (NP (CD four)))) (S (VP (VBD had) (VP (VBN spent) (NP (QP (DT"
" a) (JJ few)) (NNS years)) (PP (IN in) (NP (NNP New) (NNP Zealand))) (PP (IN"
" before) (S (VP (VBG moving) (PP (IN to) (NP (NNP Dongguan))))))))))))))"
)
self.run_test(test_pattern, test_tree, expected_result)
test_tree = (
"(ROOT (S (NP (NP (NNP Banana)) (, ,) (NP (NN orange)) (, ,) (CC and) (NP (NN"
" apple))) (VP (VBP are) (NP (NNS fruits))) (. .)))"
)
self.run_test(test_pattern, test_tree)
test_tree = (
"(ROOT (S (NP (PRP He)) (, ,) (ADVP (RB however)) (, ,) (VP (VBZ does) (RB not) (VP"
" (VB look) (ADJP (JJ fine)))) (. .)))"
)
self.run_test(test_pattern, test_tree)
def test_complex2(self):
"""
More complex patterns to test
"""
inputTrees = [
(
"(ROOT (S (NP (PRP You)) (VP (VBD did) (VP (VB go) (WHADVP (WRB How) (JJ long))"
" (PP (IN for)))) (. .)))"
),
(
"(ROOT (S (NP (NNS Raccoons)) (VP (VBP do) (VP (VB come) (WHADVP (WRB When))"
" (PRT (RP out)))) (. .)))"
),
("(ROOT (S (NP (PRP She)) (VP (VBZ is) (VP (WHADVP (WRB Where)) (VBG working))) (. .)))"),
"(ROOT (S (NP (PRP You)) (VP (VBD did) (VP (WHNP (WP What)) (VB do))) (. .)))",
(
"(ROOT (S (NP (PRP You)) (VP (VBD did) (VP (VB do) (PP (IN in) (NP (NNP"
" Australia))) (WHNP (WP What)))) (. .)))"
),
]
pattern = "WHADVP=whadvp > VP $+ /[A-Z]*/=last ![$++ (PP < NP)]"
self.run_test(pattern, inputTrees[0], "(WHADVP (WRB How) (JJ long))")
self.run_test(pattern, inputTrees[1], "(WHADVP (WRB When))")
self.run_test(pattern, inputTrees[2], "(WHADVP (WRB Where))")
self.run_test(pattern, inputTrees[3])
self.run_test(pattern, inputTrees[4])
pattern = "VP < (/^WH/=wh $++ /^VB/=vb)"
self.run_test(pattern, inputTrees[0])
self.run_test(pattern, inputTrees[1])
self.run_test(pattern, inputTrees[2], "(VP (WHADVP (WRB Where)) (VBG working))")
self.run_test(pattern, inputTrees[3], "(VP (WHNP (WP What)) (VB do))")
self.run_test(pattern, inputTrees[4])
pattern = "PP=pp > VP $+ WHNP=whnp"
self.run_test(pattern, inputTrees[0])
self.run_test(pattern, inputTrees[1])
self.run_test(pattern, inputTrees[2])
self.run_test(pattern, inputTrees[3])
self.run_test(pattern, inputTrees[4], "(PP (IN in) (NP (NNP Australia)))")
def test_link(self):
# matched node will be (bar 3), the next bar matches (bar 2), and
# the foo at the end obviously matches the (foo 1)
self.run_test("bar $- (bar $- foo)", "(a (foo 1) (bar 2) (bar 3))", "(bar 3)")
# same thing, but this tests the link functionality, as the
# second match should also be (bar 2)
self.run_test("bar=a $- (~a $- foo)", "(a (foo 1) (bar 2) (bar 3))", "(bar 3)")
# TODO
# won't work, since (bar 3) doesn't satisfy the next-to-foo
# relation, and (bar 2) isn't the same node as (bar 3)
# self.run_test("bar=a $- (=a $- foo)", "(a (foo 1) (bar 2) (bar 3))")
# links can be saved as named nodes as well, so this should work
self.run_test("bar=a $- (~a=b $- foo)", "(a (foo 1) (bar 2) (bar 3))", "(bar 3)")
# run a few of the same tests, but this time dissect the results
# to make sure the captured nodes are the correct nodes
tree_string = "(a (foo 1) (bar 2) (bar 3))"
pattern = TregexPattern("bar=a $- (~a $- foo)")
matches = pattern.findall(tree_string)
self.assertEqual(1, len(matches))
self.assertEqual("(bar 3)", matches[0].tostring())
self.assertEqual("(bar 3)", pattern.get_nodes("a")[0].tostring())
pattern = TregexPattern("bar=a $- (~a=b $- foo)")
matches = pattern.findall(tree_string)
self.assertEqual(1, len(matches))
self.assertEqual("(bar 3)", matches[0].tostring())
self.assertEqual("(bar 3)", pattern.get_nodes("a")[0].tostring())
self.assertEqual("(bar 2)", pattern.get_nodes("b")[0].tostring())
pattern = TregexPattern("bar=a $- (~a=b $- foo=c)")
matches = pattern.findall(tree_string)
self.assertEqual(1, len(matches))
self.assertEqual("(bar 3)", matches[0].tostring())
self.assertEqual("(bar 3)", pattern.get_nodes("a")[0].tostring())
self.assertEqual("(bar 2)", pattern.get_nodes("b")[0].tostring())
self.assertEqual("(foo 1)", pattern.get_nodes("c")[0].tostring())
# TODO
# def test_backref(self)
# """Test another variant of using links, this time with pattern partitions"""
# TregexPattern tregex = TregexPattern.compile("__ <1 B=n <2 ~n");
# Tree tree = treeFromString("(A (B w) (B x))");
# TregexMatcher matcher = tregex.matcher(tree);
# assertTrue(matcher.find());
# Tree match = matcher.getMatch();
# assertEquals("(A (B w) (B x))", match.toString());
# Tree node = pattern.get_nodes("n");
# assertEquals("(B w)", node.toString());
# assertFalse(matcher.find());
#
# tregex = TregexPattern.compile("__ < B=n <2 B=m : (=n !== =m)");
# tree = treeFromString("(A (B w) (B x))");
# matcher = tregex.matcher(tree);
# assertTrue(matcher.find());
# match = matcher.getMatch();
# assertEquals("(A (B w) (B x))", match.toString());
# node = pattern.get_nodes("n");
# assertEquals("(B w)", node.toString());
# assertFalse(matcher.find());
def test_nonsense(self):
# can't name a variable twice
pattern = TregexPattern("foo=a $ bar=a")
self.assertRaises(ParseException, pattern.findall, "(A)")
# another way of doing the same thing
pattern = TregexPattern("foo=a > bar=b $ ~a=b")
self.assertRaises(ParseException, pattern.findall, "(A)")
# ... but this should work
TregexPattern("foo=a > bar=b $ ~a").findall("(A)")
# can't link to a variable that doesn't exist yet
pattern = TregexPattern("~a $- (bar=a $- foo)")
self.assertRaises(ParseException, pattern.findall, "(A)")
# can't reference a variable that doesn't exist yet
# pattern = TregexPattern("=a $- (bar=a $- foo)")
# self.assertRaises(ParseException, pattern.findall, '(A)')
# you'd have to be really demented to do this
pattern = TregexPattern("~a=a $- (bar=b $- foo)")
self.assertRaises(ParseException, pattern.findall, "(A)")