-
Notifications
You must be signed in to change notification settings - Fork 0
/
test_pyports.py
211 lines (197 loc) · 10.4 KB
/
test_pyports.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
from math import log2
from collections import OrderedDict
import unittest
import pyports
import json
class Tree(unittest.TestCase):
def test_forward_tree(self):
tree = pyports.Tree()
tree.increment(list('car'), False)
tree.increment(list('cart'), False)
tree.increment(list('cot'), False)
tree.log_normalize()
answer = {"count": 3, "prob": log2(1)-log2(3), 'chain_prob': log2(1)-log2(3), "subtrees":
{"c":
{"count": 3, "prob": 0.0, 'chain_prob': log2(1)-log2(3), "subtrees":
{"a":
{"count": 2, "prob": log2(2)-log2(3), 'chain_prob': (log2(1)-log2(3))+(log2(2)-log2(3)), "subtrees":
{"r":
{"count": 2, "prob": 0.0, 'chain_prob': (log2(1)-log2(3))+(log2(2)-log2(3)), "subtrees":
{"t":
{"count": 1, "prob": log2(1)-log2(2), 'chain_prob': (log2(1)-log2(3))+(log2(2)-log2(3))+(log2(1)-log2(2)), "subtrees":
{}
}
}
}
}
},
"o":
{"count": 1, "prob": log2(1)-log2(3), 'chain_prob': (log2(1)-log2(3))+(log2(1)-log2(3)), "subtrees":
{"t":
{"count": 1, "prob": 0.0, 'chain_prob': (log2(1)-log2(3))+(log2(1)-log2(3)), "subtrees":
{}
}
}
}
}
}
}
}
result = tree._to_dict()
assert result == answer, 'Correct: {}\n\nWas: {}'.format(answer, result)
def test_backward_tree(self):
tree = pyports.Tree()
tree.increment(list('car'), True)
tree.increment(list('cart'), True)
tree.increment(list('cot'), True)
tree.log_normalize()
answer = {"count": 3, "prob": log2(1)-log2(3), 'chain_prob': log2(1)-log2(3), "subtrees":
{"t":
{"count": 2, "prob": log2(2)-log2(3), 'chain_prob': (log2(1)-log2(3))+(log2(2)-log2(3)), "subtrees":
{"o":
{"count": 1, "prob": log2(1)-log2(2), 'chain_prob': (log2(1)-log2(3))+(log2(2)-log2(3))+(log2(1)-log2(2)), "subtrees":
{"c":
{"count": 1, "prob": 0.0, 'chain_prob': (log2(1)-log2(3))+(log2(2)-log2(3))+(log2(1)-log2(2)), "subtrees":
{}
}
}
},
"r":
{"count": 1, "prob": log2(1)-log2(2), 'chain_prob': (log2(1)-log2(3))+(log2(2)-log2(3))+(log2(1)-log2(2)), "subtrees":
{"a":
{"count": 1, "prob": 0.0, 'chain_prob': (log2(1)-log2(3))+(log2(2)-log2(3))+(log2(1)-log2(2)), "subtrees":
{"c":
{"count": 1, "prob": 0.0, 'chain_prob': (log2(1)-log2(3))+(log2(2)-log2(3))+(log2(1)-log2(2)), "subtrees":
{}
}
}
}
}
}
}
},
"r":
{"count": 1, "prob": log2(1)-log2(3), 'chain_prob': (log2(1)-log2(3))+(log2(1)-log2(3)), "subtrees":
{"a":
{"count": 1, "prob": 0.0, 'chain_prob': (log2(1)-log2(3))+(log2(1)-log2(3)), "subtrees":
{"c":
{"count": 1, "prob": 0.0, 'chain_prob': (log2(1)-log2(3))+(log2(1)-log2(3)), "subtrees":
{}
}
}
}
}
}
}
}
result = tree._to_dict()
assert result == answer, 'Correct: {}\n\nWas: {}'.format(answer, result)
def test_from_corpus(self):
corpus = ['car', 'cart', 'cot']
tree = pyports.Tree()
tree.from_corpus(corpus)
answer = {"count": 3, "prob": log2(1)-log2(3), 'chain_prob': log2(1)-log2(3), "subtrees":
{"c":
{"count": 3, "prob": 0.0, 'chain_prob': log2(1)-log2(3), "subtrees":
{"a":
{"count": 2, "prob": log2(2)-log2(3), 'chain_prob': (log2(1)-log2(3))+(log2(2)-log2(3)), "subtrees":
{"r":
{"count": 2, "prob": 0.0, 'chain_prob': (log2(1)-log2(3))+(log2(2)-log2(3)), "subtrees":
{"t":
{"count": 1, "prob": log2(1)-log2(2), 'chain_prob': (log2(1)-log2(3))+(log2(2)-log2(3))+(log2(1)-log2(2)), "subtrees":
{}
}
}
}
}
},
"o":
{"count": 1, "prob": log2(1)-log2(3), 'chain_prob': (log2(1)-log2(3))+(log2(1)-log2(3)), "subtrees":
{"t":
{"count": 1, "prob": 0.0, 'chain_prob': (log2(1)-log2(3))+(log2(1)-log2(3)), "subtrees":
{}
}
}
}
}
}
}
}
result = tree._to_dict()
assert result == answer, 'Correct: {}\n\nWas: {}'.format(answer, result)
class TestSegmenter(unittest.TestCase):
def test_prefix_scores(self):
corpus = ['preseared', 'preheated']
correct_prefixes = {'pre': 38}
segmenter = pyports.Segmenter()
segmenter.add_corpus(corpus)
segmenter.train()
answer_prefixes = segmenter._to_dict(segmenter.prefix_scores)
assert answer_prefixes == correct_prefixes, 'Correct: {}\nWas: {}'.format(correct_prefixes, answer_prefixes)
def test_suffix_scores(self):
corpus = ['preseared', 'preheated']
correct_suffixes = {'ed': 38}
segmenter = pyports.Segmenter()
segmenter.add_corpus(corpus)
segmenter.train()
answer_suffixes = segmenter._to_dict(segmenter.suffix_scores)
assert answer_suffixes == correct_suffixes, 'Correct: {}\nWas: {}'.format(correct_suffixes, answer_suffixes)
def test_prefix_probs(self):
corpus = ['preseared', 'preheated']
correct_prefixes = {'pre': log2(1)-log2(2)}
segmenter = pyports.Segmenter()
segmenter.add_corpus(corpus)
segmenter.train()
answer_prefixes = segmenter._to_dict(segmenter.prefix_probs)
assert answer_prefixes == correct_prefixes, 'Correct: {}\nWas: {}'.format(correct_prefixes, answer_prefixes)
def test_suffix_probs(self):
corpus = ['preseared', 'preheated']
correct_suffixes = {'ed': log2(1)-log2(2)}
segmenter = pyports.Segmenter()
segmenter.add_corpus(corpus)
segmenter.train()
answer_suffixes = segmenter._to_dict(segmenter.suffix_probs)
assert answer_suffixes == correct_suffixes, 'Correct: {}\nWas: {}'.format(correct_suffixes, answer_suffixes)
def test_find_suffix(self):
corpus = ['preseared', 'preheated']
correct_suffixes = ['ed']
segmenter = pyports.Segmenter()
segmenter.add_corpus(corpus)
segmenter.train()
word, answer_suffixes = segmenter._find_suffixes('prefeared')
assert answer_suffixes == correct_suffixes, 'Correct: {}\nWas: {}'.format(correct_suffixes, answer_suffixes)
def test_find_suffixes(self):
corpus = ['dry', 'dryness', 'drying',
'polite', 'politeness', 'politenesses',
'fox', 'foxes',
'hash', 'hashing', 'hashes']
segmenter = pyports.Segmenter()
segmenter.add_corpus(corpus)
segmenter.train()
word, answer_suffixes = segmenter._find_suffixes('politenesses')
correct_suffixes = ['ness', 'es']
assert answer_suffixes == correct_suffixes, 'Correct: {}\nWas: {}'.format(correct_suffixes, answer_suffixes)
def test_find_prefixes(self):
corpus = ['dry', 'predry', 'undry',
'heat', 'preheat', 'repreheat', 'unheat',
'turn', 'return', 'repaint', 'retry']
segmenter = pyports.Segmenter()
segmenter.add_corpus(corpus)
segmenter.train()
word, answer_prefixes = segmenter._find_prefixes('repreheat')
correct_prefixes = ['re', 'pre']
assert answer_prefixes == correct_prefixes, 'Correct: {}\nWas: {}'.format(correct_prefixes, answer_prefixes)
class TestEvaluator(unittest.TestCase):
def test_count(self):
dev = [['pre', 'cogni', 'tion'], ['devalue'], ['evalua', 'te'], ['ef', 'fect']]
gold = [['pre', 'cogni', 'tion'], ['de', 'value'], ['eval', 'uate'], ['effect']]
correct_tp = 2
correct_fp = 2
correct_fn = 2
evaluator = pyports.Evaluator()
tp, fp, fn = evaluator._count(dev, gold)
assert tp == correct_tp, 'Correct: {}\nWas: {}'.format(correct_tp, tp)
assert fp == correct_fp, 'Correct: {}\nWas: {}'.format(correct_tp, fp)
assert fn == correct_fn, 'Correct: {}\nWas: {}'.format(correct_tp, fn)
if __name__ == '__main__':
unittest.main()