forked from clips/pattern
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest_en.py
1116 lines (1011 loc) · 47.4 KB
/
test_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
# -*- coding: utf-8 -*-
import os, sys; sys.path.insert(0, os.path.join(os.path.dirname(__file__), ".."))
import unittest
import random
import subprocess
from pattern import text
from pattern import en
try:
PATH = os.path.dirname(os.path.realpath(__file__))
except:
PATH = ""
#---------------------------------------------------------------------------------------------------
class TestInflection(unittest.TestCase):
def setUp(self):
pass
def test_indefinite_article(self):
# Assert "a" or "an".
for article, word in (
("an", "hour"),
("an", "FBI"),
("a", "bear"),
("a", "one-liner"),
("a", "European"),
("a", "university"),
("a", "uterus"),
("an", "owl"),
("an", "yclept"),
("a", "year")):
self.assertEqual(en.article(word, function=en.INDEFINITE), article)
self.assertEqual(en.inflect.article("heir", function=en.DEFINITE), "the")
self.assertEqual(en.inflect.referenced("ewe"), "a ewe")
print "pattern.en.inflect.article()"
def test_pluralize(self):
# Assert "octopodes" for classical plural of "octopus".
# Assert "octopuses" for modern plural.
self.assertEqual("octopodes", en.inflect.pluralize("octopus", classical=True))
self.assertEqual("octopuses", en.inflect.pluralize("octopus", classical=False))
# Assert the accuracy of the pluralization algorithm.
from pattern.db import Datasheet
i, n = 0, 0
for sg, pl in Datasheet.load(os.path.join(PATH, "corpora", "wordforms-en-celex.csv")):
if en.inflect.pluralize(sg) == pl:
i +=1
n += 1
self.assertTrue(float(i) / n > 0.95)
print "pattern.en.inflect.pluralize()"
def test_singularize(self):
# Assert the accuracy of the singularization algorithm.
from pattern.db import Datasheet
i, n = 0, 0
for sg, pl in Datasheet.load(os.path.join(PATH, "corpora", "wordforms-en-celex.csv")):
if en.inflect.singularize(pl) == sg:
i +=1
n += 1
self.assertTrue(float(i) / n > 0.95)
print "pattern.en.inflect.singularize()"
def test_find_lemma(self):
# Assert the accuracy of the verb lemmatization algorithm.
# Note: the accuracy is higher (95%) when measured on CELEX word forms
# (probably because en.verbs has high percentage irregular verbs).
i, n = 0, 0
for v1, v2 in en.inflect.verbs.inflections.items():
if en.inflect.verbs.find_lemma(v1) == v2:
i += 1
n += 1
self.assertTrue(float(i) / n > 0.90)
print "pattern.en.inflect.verbs.find_lemma()"
def test_find_lexeme(self):
# Assert the accuracy of the verb conjugation algorithm.
i, n = 0, 0
for v, lexeme1 in en.inflect.verbs.infinitives.items():
lexeme2 = en.inflect.verbs.find_lexeme(v)
for j in range(len(lexeme2)):
if lexeme1[j] == lexeme2[j] or \
lexeme1[j] == "" and \
lexeme1[j>5 and 10 or 0] == lexeme2[j]:
i += 1
n += 1
self.assertTrue(float(i) / n > 0.90)
print "pattern.en.inflect.verbs.find_lexeme()"
def test_conjugate(self):
# Assert different tenses with different conjugations.
for (v1, v2, tense) in (
("be", "be", en.INFINITIVE),
("be", "am", (en.PRESENT, 1, en.SINGULAR)),
("be", "are", (en.PRESENT, 2, en.SINGULAR)),
("be", "is", (en.PRESENT, 3, en.SINGULAR)),
("be", "are", (en.PRESENT, 0, en.PLURAL)),
("be", "being", (en.PRESENT + en.PARTICIPLE,)),
("be", "was", (en.PAST, 1, en.SINGULAR)),
("be", "were", (en.PAST, 2, en.SINGULAR)),
("be", "was", (en.PAST, 3, en.SINGULAR)),
("be", "were", (en.PAST, 0, en.PLURAL)),
("be", "were", (en.PAST, 0, None)),
("be", "been", (en.PAST + en.PARTICIPLE,)),
("be", "am", "1sg"),
("be", "are", "2sg"),
("be", "is", "3sg"),
("be", "are", "1pl"),
("be", "are", "2pl"),
("be", "are", "3pl"),
("be", "are", "pl"),
("be", "being", "part"),
("be", "was", "1sgp"),
("be", "were", "2sgp"),
("be", "was", "3sgp"),
("be", "were", "1ppl"),
("be", "were", "2ppl"),
("be", "were", "3ppl"),
("be", "were", "p"),
("be", "were", "ppl"),
("be", "been", "ppart"),
("be", "am not", "1sg-"),
("be", "aren't", "2sg-"),
("be", "isn't", "3sg-"),
("be", "aren't", "1pl-"),
("be", "aren't", "2pl-"),
("be", "aren't", "3pl-"),
("be", "aren't", "pl-"),
("be", "wasn't", "1sgp-"),
("be", "weren't", "2sgp-"),
("be", "wasn't", "3sgp-"),
("be", "weren't", "1ppl-"),
("be", "weren't", "2ppl-"),
("be", "weren't", "3ppl-"),
("be", "weren't", "ppl-"),
("had", "have", "inf"),
("had", "have", "1sg"),
("had", "have", "2sg"),
("had", "has", "3sg"),
("had", "have", "pl"),
("had", "having", "part"),
("has", "had", "1sgp"),
("has", "had", "2sgp"),
("has", "had", "3sgp"),
("has", "had", "ppl"),
("has", "had", "p"),
("has", "had", "ppart"),
("will", "will", "1sg"),
("will", "will", "2sg"),
("will", "will", "3sg"),
("will", "will", "1pl"),
("imaginerify", "imaginerifying", "part"),
("imaginerify", "imaginerified", "3sgp"),
("imaginerify", None, "1sg-")):
self.assertEqual(en.inflect.conjugate(v1, tense), v2)
print "pattern.en.inflect.conjugate()"
def test_lemma(self):
# Assert the infinitive of "weren't".
v = en.inflect.lemma("weren't")
self.assertEqual(v, "be")
print "pattern.en.inflect.lemma()"
def test_lexeme(self):
# Assert all inflections of "be".
v = en.inflect.lexeme("be")
self.assertEqual(v, [
"be", "am", "are", "is", "being",
"was", "were", "been",
"am not", "aren't", "isn't", "wasn't", "weren't"
])
v = en.inflect.lexeme("imaginerify")
self.assertEqual(v, [
"imaginerify", "imaginerifies", "imaginerifying", "imaginerified"
])
print "pattern.en.inflect.lexeme()"
def test_tenses(self):
# Assert tense recognition.
self.assertTrue((en.inflect.PRESENT, 1, en.inflect.SINGULAR) in en.inflect.tenses("am"))
self.assertTrue("1sg" in en.inflect.tenses("am"))
self.assertTrue("1sg" in en.inflect.tenses("will"))
self.assertTrue("2sg-" in en.inflect.tenses("won't"))
self.assertTrue("part" in en.inflect.tenses("imaginarifying"))
print "pattern.en.inflect.tenses()"
def test_comparative(self):
# Assert "nice" => "nicer".
self.assertEqual(en.inflect.comparative("nice"), "nicer")
print "pattern.en.inflect.comparative()"
def test_superlative(self):
# Assert "nice" => "nicest"
self.assertEqual(en.inflect.superlative("nice"), "nicest")
# Assert "important" => "most important"
self.assertEqual(en.inflect.superlative("important"), "most important")
print "pattern.en.inflect.superlative()"
#---------------------------------------------------------------------------------------------------
class TestQuantification(unittest.TestCase):
def setUp(self):
pass
def test_extract_leading_zeros(self):
# Assert "zero zero one" => ("one", 2).
from pattern.text.en.inflect_quantify import zshift
v = zshift("zero zero one")
self.assertEqual(v, ("one", 2))
v = zshift("0 0 one")
self.assertEqual(v, ("one", 2))
print "pattern.en.quantify._extract_leading_zeros()"
def test_numerals(self):
# Assert number to numerals.
for x, s in (
( 1.5, "one point five"),
( 15, "fifteen"),
( 150, "one hundred and fifty"),
( 151, "one hundred and fifty-one"),
( 1510, "one thousand five hundred and ten"),
( 15101, "fifteen thousand one hundred and one"),
( 150101, "one hundred and fifty thousand one hundred and one"),
(1500101, "one million, five hundred thousand one hundred and one")):
self.assertEqual(en.numerals(x), s)
print "pattern.en.numerals()"
def test_number(self):
# Assert numeric string = actual number (after rounding).
for i in range(100):
x = random.random()
y = en.number(en.numerals(x, round=10))
self.assertAlmostEqual(x, y, places=10)
print "pattern.en.number()"
def test_quantify(self):
# Assert quantification algorithm.
for a, s in (
( 2 * ["carrot"], "a pair of carrots"),
( 4 * ["carrot"], "several carrots"),
( 9 * ["carrot"], "a number of carrots"),
( 19 * ["carrot"], "a score of carrots"),
( 23 * ["carrot"], "dozens of carrots"),
( 201 * ["carrot"], "hundreds of carrots"),
(1001 * ["carrot"], "thousands of carrots"),
({"carrot": 4, "parrot": 2}, "several carrots and a pair of parrots")):
self.assertEqual(en.quantify(a), s)
print "pattern.en.quantify()"
def test_reflect(self):
self.assertEqual(en.reflect(""), "a string")
self.assertEqual(en.reflect(["","",""]), "several strings")
self.assertEqual(en.reflect(en.reflect), "a function")
print "pattern.en.reflect()"
#---------------------------------------------------------------------------------------------------
class TestSpelling(unittest.TestCase):
def test_spelling(self):
# Assert case-sensitivity + numbers.
for a, b in (
( ".", "." ),
( "?", "?" ),
( "!", "!" ),
( "I", "I" ),
( "a", "a" ),
( "42", "42" ),
("3.14", "3.14"),
( "The", "The" ),
( "the", "the" )):
self.assertEqual(en.suggest(a)[0][0], b)
# Assert spelling suggestion accuracy.
# Note: simply training on more text will not improve accuracy.
i = j = 0.0
from pattern.db import Datasheet
for correct, wrong in Datasheet.load(os.path.join(PATH, "corpora", "spelling-birkbeck.csv")):
for w in wrong.split(" "):
if en.suggest(w)[0][0] == correct:
i += 1
else:
j += 1
self.assertTrue(i / (i+j) > 0.70)
print "pattern.en.suggest()"
#---------------------------------------------------------------------------------------------------
class TestParser(unittest.TestCase):
def setUp(self):
pass
def test_tokenize(self):
# Assert list with two sentences.
# The tokenizer should at least handle common abbreviations and punctuation.
v = en.tokenize("The cat is eating (e.g., a fish). Yum!")
self.assertEqual(v, ["The cat is eating ( e.g. , a fish ) .", "Yum !"])
print "pattern.en.tokenize()"
def _test_morphological_rules(self, function=en.parser.morphology.apply):
""" For each word in WordNet that is not in Brill's lexicon,
test if the given tagger((word, "NN")) yields an improved (word, tag).
Returns the relative scores for nouns, verbs, adjectives and adverbs.
"""
scores = []
for tag, lexicon in (
("NN", en.wordnet.NOUNS),
("VB", en.wordnet.VERBS),
("JJ", en.wordnet.ADJECTIVES),
("RB", en.wordnet.ADVERBS)):
i, n = 0, 0
for word in lexicon:
word = word.form
if word not in en.lexicon:
if function([word, "NN"])[1].startswith(tag):
i += 1
n += 1
scores.append(float(i) / n)
return scores
def test_default_suffix_rules(self):
# Assert part-of-speech tag for unknown tokens.
for a, b in (
(["eating", "NN"], ["eating", "VBG"]),
(["tigers", "NN"], ["tigers", "NNS"]),
(["really", "NN"], ["really", "RB"]),
(["foolish", "NN"], ["foolish", "JJ"])):
self.assertEqual(text._suffix_rules(a), b)
# Test with words in WordNet that are not in Brill's lexicon.
# Given are the scores for detection of nouns, verbs, adjectives and adverbs.
# The baseline should increase (not decrease) when the algorithm is modified.
v = self._test_morphological_rules(function=text._suffix_rules)
self.assertTrue(v[0] > 0.91) # NN
self.assertTrue(v[1] > 0.23) # VB
self.assertTrue(v[2] > 0.38) # JJ
self.assertTrue(v[3] > 0.60) # RB
print "pattern.text._suffix_rules()"
def test_apply_morphological_rules(self):
# Assert part-of-speech tag for unknown tokens (Brill's lexical rules).
v = self._test_morphological_rules(function=en.parser.morphology.apply)
self.assertTrue(v[0] > 0.85) # NN
self.assertTrue(v[1] > 0.19) # VB
self.assertTrue(v[2] > 0.65) # JJ
self.assertTrue(v[3] > 0.59) # RB
print "pattern.en.parser.morphology.apply()"
def test_apply_context_rules(self):
# Assert part-of-speech tags based on word context.
for a, b in ( # Rule:
([["", "JJ"], ["", "JJ"], ["", ","]], [["", "JJ"], ["", "NN"], ["", ","]]), # SURROUNDTAG
([["", "NNP"], ["", "RB"]], [["", "NNP"], ["", "NNP"]]), # PREVTAG
([["", "NN"], ["", "PRP$"]], [["", "VB"], ["", "PRP$"]]), # NEXTTAG
([["phone", ""], ["", "VBZ"]], [["phone", ""], ["", "NNS"]]), # PREVWD
([["", "VB"], ["countries", ""]], [["", "JJ"], ["countries", ""]]), # NEXTWD
([["close", "VB"], ["to", ""]], [["close", "RB"], ["to", ""]]), # RBIGRAM
([["very", ""], ["much", "JJ"]], [["very", ""], ["much", "RB"]]), # LBIGRAM
([["such", "JJ"], ["as", "DT"]], [["such", "JJ"], ["as", "IN"]]), # WDNEXTWD
([["be", "VB"]], [["be", "VB"]])): # CURWD
self.assertEqual(en.parser.context.apply(a), b)
print "pattern.en.parser.context.apply()"
def test_find_tags(self):
# Assert part-of-speech-tag annotation.
v = en.parser.find_tags(["black", "cat"])
self.assertEqual(v, [["black", "JJ"], ["cat", "NN"]])
self.assertEqual(en.parser.find_tags(["felix"])[0][1], "NN")
self.assertEqual(en.parser.find_tags(["Felix"])[0][1], "NNP")
print "pattern.en.parser.find_tags()"
def test_find_chunks(self):
# Assert chunk tag annotation.
v = en.parser.find_chunks([["black", "JJ"], ["cat", "NN"]])
self.assertEqual(v, [["black", "JJ", "B-NP", "O"], ["cat", "NN", "I-NP", "O"]])
# Assert the accuracy of the chunker.
# For example, in "The very black cat must be really meowing really loud in the yard.":
# - "The very black" (NP)
# - "must be really meowing" (VP)
# - "really loud" (ADJP)
# - "in" (PP)
# - "the yard" (NP)
v = en.parser.find_chunks([
["","DT"], ["","RB"], ["","JJ"], ["","NN"],
["","MD"], ["","RB"], ["","VBZ"], ["","VBG"],
["","RB"], ["","JJ"],
["","IN"],
["","CD"], ["","NNS"]
])
self.assertEqual(v, [
["", "DT", "B-NP", "O"], ["", "RB", "I-NP", "O"], ["", "JJ", "I-NP", "O"], ["", "NN", "I-NP", "O"],
["", "MD", "B-VP", "O"], ["", "RB", "I-VP", "O"], ["", "VBZ", "I-VP", "O"], ["", "VBG", "I-VP", "O"],
["", "RB", "B-ADJP", "O"], ["", "JJ", "I-ADJP", "O"],
["", "IN", "B-PP", "B-PNP"],
["", "CD", "B-NP", "I-PNP"], ["", "NNS", "I-NP", "I-PNP"]])
# Assert commas inside chunks.
# - "the big, black cat"
v = en.parser.find_chunks([
["", "DT"], ["", "JJ"], ["", ","], ["", "JJ"], ["", "NN"]
])
self.assertEqual(v, [
["", "DT", "B-NP", "O"],
["", "JJ", "I-NP", "O"],
["", ",", "I-NP", "O"],
["", "JJ", "I-NP", "O"],
["", "NN", "I-NP", "O"]
])
# - "big, black and furry"
v = en.parser.find_chunks([
["", "JJ"], ["", ","], ["", "JJ"], ["", "CC"], ["", "JJ"]
])
self.assertEqual(v, [
["", "JJ", "B-ADJP", "O"],
["", ",", "I-ADJP", "O"],
["", "JJ", "I-ADJP", "O"],
["", "CC", "I-ADJP", "O"],
["", "JJ", "I-ADJP", "O"]
])
# - big, and very black (= two chunks "big" and "very black")
v = en.parser.find_chunks([
["", "JJ"], ["", ","], ["", "CC"], ["", "RB"], ["", "JJ"]
])
self.assertEqual(v, [
["", "JJ", "B-ADJP", "O"],
["", ",", "O", "O"],
["", "CC", "O", "O"],
["", "RB", "B-ADJP", "O"],
["", "JJ", "I-ADJP", "O"]
])
# Assert cases for which we have written special rules.
# - "perhaps you" (ADVP + NP)
v = en.parser.find_chunks([["","RB"], ["","PRP"]])
self.assertEqual(v, [["","RB","B-ADVP", "O"], ["","PRP","B-NP", "O"]])
# - "very nice cats" (NP)
v = en.parser.find_chunks([["","RB"], ["","JJ"], ["","PRP"]])
self.assertEqual(v, [["","RB","B-NP", "O"], ["","JJ","I-NP", "O"], ["","PRP","I-NP", "O"]])
print "pattern.en.parser.find_chunks()"
def test_find_labels(self):
# Assert relation tag annotation (SBJ/OBJ).
v = en.parser.find_labels([
["", "", "NP"], ["", "", "NP"],
["", "", "VP"], ["", "", "VP"],
["", "", "NP"]])
self.assertEqual(v, [
["", "", "NP", "NP-SBJ-1"], ["", "", "NP", "NP-SBJ-1"],
["", "", "VP", "VP-1"], ["", "", "VP", "VP-1"],
["", "", "NP", "NP-OBJ-1"]])
print "pattern.en.parser.find_labels()"
def test_find_prepositions(self):
# Assert preposition tag annotation (PP + NP).
v = en.parser.find_prepositions([
["", "", "NP"],
["", "", "VP"],
["", "", "PP"],
["", "", "NP"],
["", "", "NP"],])
self.assertEqual(v, [
["", "", "NP", "O"],
["", "", "VP", "O"],
["", "", "PP", "B-PNP"],
["", "", "NP", "I-PNP"],
["", "", "NP", "I-PNP"]])
# Assert PNP's with consecutive PP's.
v = en.parse("The cat was looking at me from up on the roof with interest.", prepositions=True)
self.assertEqual(v,
"The/DT/B-NP/O cat/NN/I-NP/O " \
"was/VBD/B-VP/O looking/VBG/I-VP/O " \
"at/IN/B-PP/B-PNP me/PRP/B-NP/I-PNP " \
"from/IN/B-PP/B-PNP up/IN/I-PP/I-PNP on/IN/I-PP/I-PNP the/DT/B-NP/I-PNP roof/NN/I-NP/I-PNP " \
"with/IN/B-PP/B-PNP interest/NN/B-NP/I-PNP " \
"././O/O"
)
print "pattern.en.parser.find_prepositions()"
def test_find_lemmata(self):
# Assert lemmata for nouns and verbs.
v = en.parser.find_lemmata([["cats", "NNS"], ["wearing", "VBG"], ["hats", "NNS"]])
self.assertEqual(v, [
["cats", "NNS", "cat"],
["wearing", "VBG", "wear"],
["hats", "NNS", "hat"]])
print "pattern.en.parser.find_lemmata()"
def test_named_entity_recognition(self):
# Assert named entities.
v = en.parser.parse("Arnold Schwarzenegger is cool.", chunks=False)
self.assertEqual(v,
"Arnold/NNP-PERS Schwarzenegger/NNP-PERS is/VBZ cool/JJ ./."
)
print "pattern.en.parser.entities.apply()"
def test_parse(self):
# Assert parsed output with Penn Treebank II tags (slash-formatted).
# 1) "the black cat" is a noun phrase, "on the mat" is a prepositional noun phrase.
v = en.parser.parse("The black cat sat on the mat.")
self.assertEqual(v,
"The/DT/B-NP/O black/JJ/I-NP/O cat/NN/I-NP/O " + \
"sat/VBD/B-VP/O " + \
"on/IN/B-PP/B-PNP the/DT/B-NP/I-PNP mat/NN/I-NP/I-PNP ././O/O"
)
# 2) "the black cat" is the subject, "a fish" is the object.
v = en.parser.parse("The black cat is eating a fish.", relations=True)
self.assertEqual(v,
"The/DT/B-NP/O/NP-SBJ-1 black/JJ/I-NP/O/NP-SBJ-1 cat/NN/I-NP/O/NP-SBJ-1 " + \
"is/VBZ/B-VP/O/VP-1 eating/VBG/I-VP/O/VP-1 " + \
"a/DT/B-NP/O/NP-OBJ-1 fish/NN/I-NP/O/NP-OBJ-1 ././O/O/O"
)
# 3) "chasing" and "mice" lemmata are "chase" and "mouse".
v = en.parser.parse("The black cat is chasing mice.", lemmata=True)
self.assertEqual(v,
"The/DT/B-NP/O/the black/JJ/I-NP/O/black cat/NN/I-NP/O/cat " + \
"is/VBZ/B-VP/O/be chasing/VBG/I-VP/O/chase " + \
"mice/NNS/B-NP/O/mouse ././O/O/."
)
# 4) Assert unicode.
self.assertTrue(isinstance(v, unicode))
# 5) Assert unicode for faulty input (bytestring with unicode characters).
self.assertTrue(isinstance(en.parse("ø ü"), unicode))
self.assertTrue(isinstance(en.parse("ø ü", tokenize=True, tags=False, chunks=False), unicode))
self.assertTrue(isinstance(en.parse("ø ü", tokenize=False, tags=False, chunks=False), unicode))
self.assertTrue(isinstance(en.parse("o u", encoding="ascii"), unicode))
# 6) Assert optional parameters (i.e., setting all to False).
self.assertEqual(en.parse("ø ü.", tokenize=True, tags=False, chunks=False), u"ø ü .")
self.assertEqual(en.parse("ø ü.", tokenize=False, tags=False, chunks=False), u"ø ü.")
# 7) Assert the accuracy of the English tagger.
i, n = 0, 0
for corpus, a in (("tagged-en-wsj.txt", (0.968, 0.945)), ("tagged-en-oanc.txt", (0.929, 0.932))):
for sentence in open(os.path.join(PATH, "corpora", corpus)).readlines():
sentence = sentence.decode("utf-8").strip()
s1 = [w.split("/") for w in sentence.split(" ")]
s2 = [[w for w, pos in s1]]
s2 = en.parse(s2, tokenize=False)
s2 = [w.split("/") for w in s2.split(" ")]
for j in range(len(s1)):
if s1[j][1] == s2[j][1].split("-")[0]:
i += 1
n += 1
#print corpus, float(i) / n
self.assertTrue(float(i) / n > (en.parser.model and a[0] or a[1]))
print "pattern.en.parse()"
def test_tagged_string(self):
# Assert splitable TaggedString with language and tags properties.
v = en.parser.parse("The black cat sat on the mat.", relations=True, lemmata=True)
self.assertEqual(v.language, "en")
self.assertEqual(v.tags,
["word", "part-of-speech", "chunk", "preposition", "relation", "lemma"])
self.assertEqual(v.split(text.TOKENS)[0][0],
["The", "DT", "B-NP", "O", "NP-SBJ-1", "the"])
print "pattern.en.parse().split()"
def test_parsetree(self):
# Assert parsetree(s) == Text.
v = en.parsetree("The cat purs.")
self.assertTrue(isinstance(v, en.Text))
print "pattern.en.parsetree()"
def test_split(self):
# Assert split(parse(s)) == Text.
v = en.split(en.parse("The cat purs."))
self.assertTrue(isinstance(v, en.Text))
print "pattern.en.split()"
def test_tag(self):
# Assert [("black", "JJ"), ("cats", "NNS")].
v = en.tag("black cats")
self.assertEqual(v, [("black", "JJ"), ("cats", "NNS")])
v = en.tag("")
self.assertEqual(v, [])
print "pattern.en.tag()"
def test_ngrams(self):
# Assert n-grams with and without punctuation marks / sentence marks.
s = "The cat is napping."
v1 = en.ngrams(s, n=2)
v2 = en.ngrams(s, n=3, punctuation=en.PUNCTUATION.strip("."))
self.assertEqual(v1, [("The", "cat"), ("cat", "is"), ("is", "napping")])
self.assertEqual(v2, [("The", "cat", "is"), ("cat", "is", "napping"), ("is", "napping", ".")])
s = "The cat purrs. The dog barks."
v1 = en.ngrams(s, n=2)
v2 = en.ngrams(s, n=2, continuous=True)
self.assertEqual(v1, [("The", "cat"), ("cat", "purrs"), ("The", "dog"), ("dog", "barks")])
self.assertEqual(v2, [("The", "cat"), ("cat", "purrs"), ("purrs", "The"), ("The", "dog"), ("dog", "barks")])
print "pattern.en.ngrams()"
def test_command_line(self):
# Assert parsed output from the command-line (example from the documentation).
p = ["python", "-m", "pattern.en", "-s", "Nice cat.", "-OTCRL"]
p = subprocess.Popen(p, stdout=subprocess.PIPE)
p.wait()
v = p.stdout.read()
v = v.strip()
self.assertEqual(v, "Nice/JJ/B-NP/O/O/nice cat/NN/I-NP/O/O/cat ././O/O/O/.")
print "python -m pattern.en"
#---------------------------------------------------------------------------------------------------
class TestParseTree(unittest.TestCase):
def setUp(self):
# Parse sentences to test on.
# Creating a Text creates Sentence, Chunk, PNP and Word.
# Creating a Sentence tests Sentence.append() and Sentence.parse_token().
self.text = "I'm eating pizza with a fork. What a tasty pizza!"
self.text = en.Text(en.parse(self.text, relations=True, lemmata=True))
def test_copy(self):
# Assert deepcopy of Text, Sentence, Chunk, PNP and Word.
self.text = self.text.copy()
print "pattern.en.Text.copy()"
def test_xml(self):
# Assert XML export and import.
self.text = en.Text.from_xml(self.text.xml)
print "pattern.en.Text.xml"
print "pattern.en.Text.from_xml()"
def test_text(self):
# Assert Text.
self.assertEqual(self.text.sentences[0].string, "I 'm eating pizza with a fork .")
self.assertEqual(self.text.sentences[1].string, "What a tasty pizza !")
print "pattern.en.Text"
def test_sentence(self):
# Assert Sentence.
v = self.text[0]
self.assertTrue(v.start == 0)
self.assertTrue(v.stop == 8)
self.assertTrue(v.string == "I 'm eating pizza with a fork .")
self.assertTrue(v.subjects == [self.text[0].chunks[0]])
self.assertTrue(v.verbs == [self.text[0].chunks[1]])
self.assertTrue(v.objects == [self.text[0].chunks[2]])
self.assertTrue(v.nouns == [self.text[0].words[3], self.text[0].words[6]])
# Sentence.string must be unicode.
self.assertTrue(isinstance(v.string, unicode) == True)
self.assertTrue(isinstance(unicode(v), unicode) == True)
self.assertTrue(isinstance(str(v), str) == True)
print "pattern.en.Sentence"
def test_sentence_constituents(self):
# Assert in-order list of Chunk, PNP and Word.
v = self.text[0].constituents(pnp=True)
self.assertEqual(v, [
self.text[0].chunks[0],
self.text[0].chunks[1],
self.text[0].chunks[2],
self.text[0].pnp[0],
self.text[0].words[7],
])
print "pattern.en.Sentence.constituents()"
def test_slice(self):
# Assert sentence slice.
v = self.text[0].slice(start=4, stop=6)
self.assertTrue(v.parent == self.text[0])
self.assertTrue(v.string == "with a")
# Assert sentence slice tag integrity.
self.assertTrue(v.words[0].type == "IN")
self.assertTrue(v.words[1].chunk == None)
print "pattern.en.Slice"
def test_chunk(self):
# Assert chunk with multiple words ("a fork").
v = self.text[0].chunks[4]
self.assertTrue(v.start == 5)
self.assertTrue(v.stop == 7)
self.assertTrue(v.string == "a fork")
self.assertTrue(v.lemmata == ["a", "fork"])
self.assertTrue(v.words == [self.text[0].words[5], self.text[0].words[6]])
self.assertTrue(v.head == self.text[0].words[6])
self.assertTrue(v.type == "NP")
self.assertTrue(v.role == None)
self.assertTrue(v.pnp != None)
# Assert chunk that is subject/object of the sentence ("pizza").
v = self.text[0].chunks[2]
self.assertTrue(v.role == "OBJ")
self.assertTrue(v.relation == 1)
self.assertTrue(v.related == [self.text[0].chunks[0], self.text[0].chunks[1]])
self.assertTrue(v.subject == self.text[0].chunks[0])
self.assertTrue(v.verb == self.text[0].chunks[1])
self.assertTrue(v.object == None)
# Assert chunk traversal.
self.assertEqual(v.nearest("VP"), self.text[0].chunks[1])
self.assertEqual(v.previous(), self.text[0].chunks[1])
self.assertEqual(v.next(), self.text[0].chunks[3])
print "pattern.en.Chunk"
def test_chunk_conjunctions(self):
# Assert list of conjunct/disjunct chunks ("black cat" AND "white cat").
v = en.Sentence(en.parse("black cat and white cat"))
self.assertEqual(v.chunk[0].conjunctions, [(v.chunk[1], en.AND)])
print "pattern.en.Chunk.conjunctions()"
def test_chunk_modifiers(self):
# Assert list of nearby adjectives and adverbs with no role, for VP.
v = en.Sentence(en.parse("Perhaps you should go."))
self.assertEqual(v.chunk[2].modifiers, [v.chunk[0]]) # should <=> perhaps
print "pattern.en.Chunk.modifiers"
def test_pnp(self):
# Assert PNP chunk ("with a fork").
v = self.text[0].pnp[0]
self.assertTrue(v.string == "with a fork")
self.assertTrue(v.chunks == [self.text[0].chunks[3], self.text[0].chunks[4]])
self.assertTrue(v.pp == self.text[0].chunks[3])
print "pattern.en.PNP"
def test_word(self):
# Assert word tags ("fork" => NN).
v = self.text[0].words[6]
self.assertTrue(v.index == 6)
self.assertTrue(v.string == "fork")
self.assertTrue(v.lemma == "fork")
self.assertTrue(v.type == "NN")
self.assertTrue(v.chunk == self.text[0].chunks[4])
self.assertTrue(v.pnp != None)
for i, tags in enumerate([
["I", "PRP", "B-NP", "O", "NP-SBJ-1", "i"],
["'m", "VBP", "B-VP", "O", "VP-1", "be"],
["eating", "VBG", "I-VP", "O", "VP-1", "eat"],
["pizza", "NN", "B-NP", "O", "NP-OBJ-1", "pizza"],
["with", "IN", "B-PP", "B-PNP", "O", "with"],
["a", "DT", "B-NP", "I-PNP", "O", "a"],
["fork", "NN", "I-NP", "I-PNP", "O", "fork"],
[".", ".", "O", "O", "O", "."]]):
self.assertEqual(self.text[0].words[i].tags, tags)
print "pattern.en.Word"
def test_word_custom_tags(self):
# Assert word custom tags ("word/part-of-speech/.../some-custom-tag").
s = en.Sentence("onion/NN/FOOD", token=[en.WORD, en.POS, "semantic_type"])
v = s.words[0]
self.assertEqual(v.semantic_type, "FOOD")
self.assertEqual(v.custom_tags["semantic_type"], "FOOD")
self.assertEqual(v.copy().custom_tags["semantic_type"], "FOOD")
# Assert addition of new custom tags.
v.custom_tags["taste"] = "pungent"
self.assertEqual(s.token, [en.WORD, en.POS, "semantic_type", "taste"])
print "pattern.en.Word.custom_tags"
def test_find(self):
# Assert first item for which given function is True.
v = text.tree.find(lambda x: x>10, [1,2,3,11,12])
self.assertEqual(v, 11)
print "pattern.text.tree.find()"
def test_zip(self):
# Assert list of zipped tuples, using default to balance uneven lists.
v = text.tree.zip([1,2,3], [4,5,6,7], default=0)
self.assertEqual(v, [(1,4), (2,5), (3,6), (0,7)])
print "pattern.text.tree.zip()"
def test_unzip(self):
v = text.tree.unzip(1, [(1,4), (2,5), (3,6)])
self.assertEqual(v, [4,5,6])
print "pattern.text.tree.unzip()"
def test_unique(self):
# Assert list copy with unique items.
v = text.tree.unique([1,1,1])
self.assertEqual(len(v), 1)
self.assertEqual(v[0], 1)
print "pattern.text.tree.unique()"
def test_map(self):
# Assert dynamic Map().
v = text.tree.Map(lambda x: x+1, [1,2,3])
self.assertEqual(list(v), [2,3,4])
self.assertEqual(v.items[0], 1)
print "pattern.text.tree.Map()"
#---------------------------------------------------------------------------------------------------
class TestModality(unittest.TestCase):
def setUp(self):
pass
def test_imperative(self):
# Assert True for sentences that are orders, commands, warnings.
from pattern.text.en.modality import imperative
for b, s in (
(True, "Do your homework!"),
(True, "Do not listen to me."),
(True, "Turn that off, will you."),
(True, "Let's help him."),
(True, "Help me!"),
(True, "You will help me."),
(False, "Do it if you think it is necessary."),
(False, "I hope you will help me."),
(False, "I can help you."),
(False, "I can help you if you let me.")):
self.assertEqual(imperative(en.Sentence(en.parse(s))), b)
print "pattern.en.modality.imperative()"
def test_conditional(self):
# Assert True for sentences that contain possible or imaginary situations.
from pattern.text.en.modality import conditional
for b, s in (
(True, "We ought to help him."),
(True, "We could help him."),
(True, "I will help you."),
(True, "I hope you will help me."),
(True, "I can help you if you let me."),
(False, "You will help me."),
(False, "I can help you.")):
self.assertEqual(conditional(en.Sentence(en.parse(s))), b)
# Assert predictive mood.
s = "I will help you."
v = conditional(en.Sentence(en.parse(s)), predictive=False)
self.assertEqual(v, False)
# Assert speculative mood.
s = "I will help you if you pay me."
v = conditional(en.Sentence(en.parse(s)), predictive=False)
self.assertEqual(v, True)
print "pattern.en.modality.conditional()"
def test_subjunctive(self):
# Assert True for sentences that contain wishes, judgments or opinions.
from pattern.text.en.modality import subjunctive
for b, s in (
(True, "I wouldn't do that if I were you."),
(True, "I wish I knew."),
(True, "I propose that you be on time."),
(True, "It is a bad idea to be late."),
(False, "I will be late.")):
self.assertEqual(subjunctive(en.Sentence(en.parse(s))), b)
print "pattern.en.modality.subjunctive()"
def test_negated(self):
# Assert True for sentences that contain "not", "n't" or "never".
for b, s in (
(True, "Not true?"),
(True, "Never true."),
(True, "Isn't true."),):
self.assertEqual(en.negated(en.Sentence(en.parse(s))), b)
print "pattern.en.negated()"
def test_mood(self):
# Assert imperative mood.
v = en.mood(en.Sentence(en.parse("Do your homework!")))
self.assertEqual(v, en.IMPERATIVE)
# Assert conditional mood.
v = en.mood(en.Sentence(en.parse("We ought to help him.")))
self.assertEqual(v, en.CONDITIONAL)
# Assert subjunctive mood.
v = en.mood(en.Sentence(en.parse("I wouldn't do that if I were you.")))
self.assertEqual(v, en.SUBJUNCTIVE)
# Assert indicative mood.
v = en.mood(en.Sentence(en.parse("The weather is nice today.")))
self.assertEqual(v, en.INDICATIVE)
print "pattern.en.mood()"
def test_modality(self):
# Assert -1.0 => +1.0 representing the degree of certainty.
v = en.modality(en.Sentence(en.parse("I wish it would stop raining.")))
self.assertTrue(v < 0)
v = en.modality(en.Sentence(en.parse("It will surely stop raining soon.")))
self.assertTrue(v > 0)
# Assert the accuracy of the modality algorithm.
# Given are the scores for the CoNLL-2010 Shared Task 1 Wikipedia uncertainty data:
# http://www.inf.u-szeged.hu/rgai/conll2010st/tasks.html#task1
# The baseline should increase (not decrease) when the algorithm is modified.
from pattern.db import Datasheet
from pattern.metrics import test
sentences = []
for certain, sentence in Datasheet.load(os.path.join(PATH, "corpora", "uncertainty-conll2010.csv")):
sentence = en.parse(sentence, chunks=False, light=True)
sentence = en.Sentence(sentence)
sentences.append((sentence, int(certain) > 0))
A, P, R, F = test(lambda sentence: en.modality(sentence) > 0.5, sentences)
#print A, P, R, F
self.assertTrue(A > 0.69)
self.assertTrue(P > 0.72)
self.assertTrue(R > 0.64)
self.assertTrue(F > 0.68)
print "pattern.en.modality()"
#---------------------------------------------------------------------------------------------------
class TestSentiment(unittest.TestCase):
def setUp(self):
pass
def test_sentiment_avg(self):
# Assert 2.5.
from pattern.text import avg
v = avg([1,2,3,4])
self.assertEqual(v, 2.5)
print "pattern.text.avg"
def test_sentiment(self):
# Assert < 0 for negative adjectives and > 0 for positive adjectives.
self.assertTrue(en.sentiment("wonderful")[0] > 0)
self.assertTrue(en.sentiment("horrible")[0] < 0)
self.assertTrue(en.sentiment(en.wordnet.synsets("horrible", pos="JJ")[0])[0] < 0)
self.assertTrue(en.sentiment(en.Text(en.parse("A bad book. Really horrible.")))[0] < 0)
# Assert that :) and :( are recognized.
self.assertTrue(en.sentiment(":)")[0] > 0)
self.assertTrue(en.sentiment(":(")[0] < 0)
# Assert the accuracy of the sentiment analysis (for the positive class).
# Given are the scores for Pang & Lee's polarity dataset v2.0:
# http://www.cs.cornell.edu/people/pabo/movie-review-data/
# The baseline should increase (not decrease) when the algorithm is modified.
from pattern.db import Datasheet
from pattern.metrics import test
reviews = []
for score, review in Datasheet.load(os.path.join(PATH, "corpora", "polarity-en-pang&lee1.csv")):
reviews.append((review, int(score) > 0))
from time import time
t = time()
A, P, R, F = test(lambda review: en.positive(review), reviews)
#print A, P, R, F
self.assertTrue(A > 0.753)
self.assertTrue(P > 0.768)
self.assertTrue(R > 0.725)
self.assertTrue(F > 0.746)
# Assert the accuracy of the sentiment analysis on short text (for the positive class).
# Given are the scores for Pang & Lee's sentence polarity dataset v1.0:
# http://www.cs.cornell.edu/people/pabo/movie-review-data/
reviews = []
for score, review in Datasheet.load(os.path.join(PATH, "corpora", "polarity-en-pang&lee2.csv")):
reviews.append((review, int(score) > 0))
A, P, R, F = test(lambda review: en.positive(review), reviews)
#print A, P, R, F
self.assertTrue(A > 0.654)
self.assertTrue(P > 0.660)
self.assertTrue(R > 0.636)
self.assertTrue(F > 0.648)
print "pattern.en.sentiment()"
def test_sentiment_twitter(self):
sanders = os.path.join(PATH, "corpora", "polarity-en-sanders.csv")
if os.path.exists(sanders):
# Assert the accuracy of the sentiment analysis on tweets.
# Given are the scores for Sanders Twitter Sentiment Corpus:
# http://www.sananalytics.com/lab/twitter-sentiment/
# Positive + neutral is taken as polarity >= 0.0,
# Negative is taken as polarity < 0.0.
# Since there are a lot of neutral cases,
# and the algorithm predicts 0.0 by default (i.e., majority class) the results are good.
# Distinguishing negative from neutral from positive is a much harder task
from pattern.db import Datasheet
from pattern.metrics import test
reviews = []
for i, id, date, tweet, polarity, topic in Datasheet.load(sanders):
if polarity != "irrelevant":
reviews.append((tweet, polarity in ("positive", "neutral")))
A, P, R, F = test(lambda review: en.positive(review, threshold=0.0), reviews)
#print A, P, R, F
self.assertTrue(A > 0.824)
self.assertTrue(P > 0.879)
self.assertTrue(R > 0.911)
self.assertTrue(F > 0.895)
def test_sentiment_assessment(self):
# Assert that en.sentiment() has a fine-grained "assessments" property.
v = en.sentiment("A warm and pleasant day.").assessments
self.assertTrue(v[1][0][0] == "pleasant")
self.assertTrue(v[1][1] > 0)
print "pattern.en.sentiment().assessments"
def test_polarity(self):
# Assert that en.polarity() yields en.sentiment()[0].
s = "A great day!"
self.assertTrue(en.polarity(s) == en.sentiment(s)[0])
print "pattern.en.polarity()"
def test_subjectivity(self):
# Assert that en.subjectivity() yields en.sentiment()[1].
s = "A great day!"
self.assertTrue(en.subjectivity(s) == en.sentiment(s)[1])
print "pattern.en.subjectivity()"
def test_positive(self):
# Assert that en.positive() yields polarity >= 0.1.
s = "A great day!"
self.assertTrue(en.positive(s))
print "pattern.en.subjectivity()"
def test_sentiwordnet(self):
# Assert < 0 for negative words and > 0 for positive words.
try:
from pattern.text.en.wordnet import SentiWordNet
lexicon = SentiWordNet()
lexicon.load()
except ImportError, e:
# SentiWordNet data file is not installed in default location, stop test.
print e; return
self.assertTrue(lexicon["wonderful"][0] > 0)
self.assertTrue(lexicon["horrible"][0] < 0)
print "pattern.en.sentiment.SentiWordNet"
#---------------------------------------------------------------------------------------------------