forked from aimacode/aima-python
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_text.py
More file actions
233 lines (165 loc) · 7.15 KB
/
test_text.py
File metadata and controls
233 lines (165 loc) · 7.15 KB
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
import pytest
import os
import random
from text import * # noqa
from utils import isclose, DataFile
def test_text_models():
flatland = DataFile("EN-text/flatland.txt").read()
wordseq = words(flatland)
P1 = UnigramTextModel(wordseq)
P2 = NgramTextModel(2, wordseq)
P3 = NgramTextModel(3, wordseq)
# The most frequent entries in each model
assert P1.top(10) == [(2081, 'the'), (1479, 'of'), (1021, 'and'),
(1008, 'to'), (850, 'a'), (722, 'i'), (640, 'in'),
(478, 'that'), (399, 'is'), (348, 'you')]
assert P2.top(10) == [(368, ('of', 'the')), (152, ('to', 'the')),
(152, ('in', 'the')), (86, ('of', 'a')),
(80, ('it', 'is')),
(71, ('by', 'the')), (68, ('for', 'the')),
(68, ('and', 'the')), (62, ('on', 'the')),
(60, ('to', 'be'))]
assert P3.top(10) == [(30, ('a', 'straight', 'line')),
(19, ('of', 'three', 'dimensions')),
(16, ('the', 'sense', 'of')),
(13, ('by', 'the', 'sense')),
(13, ('as', 'well', 'as')),
(12, ('of', 'the', 'circles')),
(12, ('of', 'sight', 'recognition')),
(11, ('the', 'number', 'of')),
(11, ('that', 'i', 'had')), (11, ('so', 'as', 'to'))]
assert isclose(P1['the'], 0.0611, rel_tol=0.001)
assert isclose(P2['of', 'the'], 0.0108, rel_tol=0.01)
assert isclose(P3['', '', 'but'], 0.0, rel_tol=0.001)
assert isclose(P3['', '', 'but'], 0.0, rel_tol=0.001)
assert isclose(P3['so', 'as', 'to'], 0.000323, rel_tol=0.001)
assert P2.cond_prob.get(('went',)) is None
assert P3.cond_prob['in', 'order'].dictionary == {'to': 6}
test_string = 'unigram'
wordseq = words(test_string)
P1 = UnigramTextModel(wordseq)
assert P1.dictionary == {('unigram'): 1}
test_string = 'bigram text'
wordseq = words(test_string)
P2 = NgramTextModel(2, wordseq)
assert (P2.dictionary == {('', 'bigram'): 1, ('bigram', 'text'): 1} or
P2.dictionary == {('bigram', 'text'): 1, ('', 'bigram'): 1})
test_string = 'test trigram text'
wordseq = words(test_string)
P3 = NgramTextModel(3, wordseq)
assert ('', '', 'test') in P3.dictionary
assert ('', 'test', 'trigram') in P3.dictionary
assert ('test', 'trigram', 'text') in P3.dictionary
assert len(P3.dictionary) == 3
def test_viterbi_segmentation():
flatland = DataFile("EN-text/flatland.txt").read()
wordseq = words(flatland)
P = UnigramTextModel(wordseq)
text = "itiseasytoreadwordswithoutspaces"
s, p = viterbi_segment(text, P)
assert s == [
'it', 'is', 'easy', 'to', 'read', 'words', 'without', 'spaces']
def test_shift_encoding():
code = shift_encode("This is a secret message.", 17)
assert code == 'Kyzj zj r jvtivk dvjjrxv.'
def test_shift_decoding():
flatland = DataFile("EN-text/flatland.txt").read()
ring = ShiftDecoder(flatland)
msg = ring.decode('Kyzj zj r jvtivk dvjjrxv.')
assert msg == 'This is a secret message.'
def test_rot13_encoding():
code = rot13('Hello, world!')
assert code == 'Uryyb, jbeyq!'
def test_rot13_decoding():
flatland = DataFile("EN-text/flatland.txt").read()
ring = ShiftDecoder(flatland)
msg = ring.decode(rot13('Hello, world!'))
assert msg == 'Hello, world!'
def test_counting_probability_distribution():
D = CountingProbDist()
for i in range(10000):
D.add(random.choice('123456'))
ps = [D[n] for n in '123456']
assert 1 / 7 <= min(ps) <= max(ps) <= 1 / 5
def test_ir_system():
from collections import namedtuple
Results = namedtuple('IRResults', ['score', 'url'])
uc = UnixConsultant()
def verify_query(query, expected):
assert len(expected) == len(query)
for expected, (score, d) in zip(expected, query):
doc = uc.documents[d]
assert "{0:.2f}".format(
expected.score) == "{0:.2f}".format(score * 100)
assert os.path.basename(expected.url) == os.path.basename(doc.url)
return True
q1 = uc.query("how do I remove a file")
assert verify_query(q1, [
Results(76.83, "aima-data/MAN/rm.txt"),
Results(67.83, "aima-data/MAN/tar.txt"),
Results(67.79, "aima-data/MAN/cp.txt"),
Results(66.58, "aima-data/MAN/zip.txt"),
Results(64.58, "aima-data/MAN/gzip.txt"),
Results(63.74, "aima-data/MAN/pine.txt"),
Results(62.95, "aima-data/MAN/shred.txt"),
Results(57.46, "aima-data/MAN/pico.txt"),
Results(43.38, "aima-data/MAN/login.txt"),
Results(41.93, "aima-data/MAN/ln.txt"),
])
q2 = uc.query("how do I delete a file")
assert verify_query(q2, [
Results(75.47, "aima-data/MAN/diff.txt"),
Results(69.12, "aima-data/MAN/pine.txt"),
Results(63.56, "aima-data/MAN/tar.txt"),
Results(60.63, "aima-data/MAN/zip.txt"),
Results(57.46, "aima-data/MAN/pico.txt"),
Results(51.28, "aima-data/MAN/shred.txt"),
Results(26.72, "aima-data/MAN/tr.txt"),
])
q3 = uc.query("email")
assert verify_query(q3, [
Results(18.39, "aima-data/MAN/pine.txt"),
Results(12.01, "aima-data/MAN/info.txt"),
Results(9.89, "aima-data/MAN/pico.txt"),
Results(8.73, "aima-data/MAN/grep.txt"),
Results(8.07, "aima-data/MAN/zip.txt"),
])
q4 = uc.query("word count for files")
assert verify_query(q4, [
Results(128.15, "aima-data/MAN/grep.txt"),
Results(94.20, "aima-data/MAN/find.txt"),
Results(81.71, "aima-data/MAN/du.txt"),
Results(55.45, "aima-data/MAN/ps.txt"),
Results(53.42, "aima-data/MAN/more.txt"),
Results(42.00, "aima-data/MAN/dd.txt"),
Results(12.85, "aima-data/MAN/who.txt"),
])
q5 = uc.query("learn: date")
assert verify_query(q5, [])
q6 = uc.query("2003")
assert verify_query(q6, [
Results(14.58, "aima-data/MAN/pine.txt"),
Results(11.62, "aima-data/MAN/jar.txt"),
])
def test_words():
assert words("``EGAD!'' Edgar cried.") == ['egad', 'edgar', 'cried']
def test_canonicalize():
assert canonicalize("``EGAD!'' Edgar cried.") == 'egad edgar cried'
def test_translate():
text = 'orange apple lemon '
func = lambda x: ('s ' + x) if x ==' ' else x
assert translate(text, func) == 'oranges apples lemons '
def test_bigrams():
assert bigrams('this') == ['th', 'hi', 'is']
assert bigrams(['this', 'is', 'a', 'test']) == [['this', 'is'], ['is', 'a'], ['a', 'test']]
# TODO: for .ipynb
"""
>>> P1.samples(20)
'you thought known but were insides of see in depend by us dodecahedrons just but i words are instead degrees'
>>> P2.samples(20)
'flatland well then can anything else more into the total destruction and circles teach others confine women must be added'
>>> P3.samples(20)
'flatland by edwin a abbott 1884 to the wake of a certificate from nature herself proving the equal sided triangle'
""" # noqa
if __name__ == '__main__':
pytest.main()