/
test.py
301 lines (234 loc) · 10.4 KB
/
test.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
"""Tests for Yelp snippet creation.
These tests are meant to be run using nosetests.
"""
import snippets
class TestFullMatch(object):
def test_(self):
doc = query = 'pepperoni pizza'
snippet = snippets.highlight_doc(doc, query)
assert snippet == '[[HIGHLIGHT]]pepperoni pizza[[ENDHIGHLIGHT]]'
class TestSingleSentence(object):
def test_single_word_query(self):
doc = 'I really love deep dish pizza.'
query = 'pizza'
snippet = snippets.highlight_doc(doc, query)
assert snippet == 'I really love deep dish [[HIGHLIGHT]]pizza[[ENDHIGHLIGHT]].'
def test_multi_word_query(self):
doc = 'I really love deep dish pizza.'
query = 'deep dish pizza'
snippet = snippets.highlight_doc(doc, query)
assert snippet == 'I really love [[HIGHLIGHT]]deep dish pizza[[ENDHIGHLIGHT]].'
def test_multiple_query_matches_1(self):
doc = 'Their specialty pizza is deep dish pizza.'
query = 'deep dish pizza'
snippet = snippets.highlight_doc(doc, query)
assert snippet == 'Their specialty [[HIGHLIGHT]]pizza[[ENDHIGHLIGHT]] is [[HIGHLIGHT]]deep dish pizza[[ENDHIGHLIGHT]].'
def test_multiple_query_matches_2(self):
doc = 'pizza pepperoni olive pizza olive pizza'
query = 'pepperoni olive pizza'
snippet = snippets.highlight_doc(doc, query)
assert snippet == '[[HIGHLIGHT]]pizza[[ENDHIGHLIGHT]] [[HIGHLIGHT]]pepperoni olive pizza[[ENDHIGHLIGHT]] [[HIGHLIGHT]]olive pizza[[ENDHIGHLIGHT]]'
class TestCaseSensitivity(object):
def test_mixed_doc(self):
doc = 'Pepperoni Pizza is good pizza.'
query = 'pepperoni pizza'
snippet = snippets.highlight_doc(doc, query)
assert snippet == '[[HIGHLIGHT]]Pepperoni Pizza[[ENDHIGHLIGHT]] is good [[HIGHLIGHT]]pizza[[ENDHIGHLIGHT]].'
def test_mixed_query(self):
doc = 'pepperoni pizza is good pizza.'
query = 'pEppeRonI pIzZa'
snippet = snippets.highlight_doc(doc, query)
assert snippet == '[[HIGHLIGHT]]pepperoni pizza[[ENDHIGHLIGHT]] is good [[HIGHLIGHT]]pizza[[ENDHIGHLIGHT]].'
def test_mixed_both(self):
doc = 'pEpPeRoNi PiZzA'
query = 'PePpErOnI pIzZa'
snippet = snippets.highlight_doc(doc, query)
assert snippet == '[[HIGHLIGHT]]pEpPeRoNi PiZzA[[ENDHIGHLIGHT]]'
class TestMaxChars(object):
def test_no_tags(self):
doc = 'I love pepperoni pizza. LOL!'
query = 'pizza'
snippet = snippets.highlight_doc(doc, query, max_chars=10)
assert snippet == 'LOL!'
def test_with_tags(self):
doc = 'I love pepperoni pizza. Pizza!'
query = 'pizza'
snippet = snippets.highlight_doc(doc, query, max_chars=35)
assert snippet == '[[HIGHLIGHT]]Pizza[[ENDHIGHLIGHT]]!'
class TestMaxSents(object):
def test_one(self):
doc = 'I love pepperoni. Pepperoni. Pepperoni.'
query = 'pepperoni'
snippet = snippets.highlight_doc(doc, query, max_sents=1)
assert snippet.count('.') == 1
def test_two(self):
doc = 'I love pepperoni. Pepperoni. Pepperoni.'
query = 'pepperoni'
snippet = snippets.highlight_doc(doc, query, max_sents=2)
assert snippet.count('.') == 2
class TestMaxCharsAndSents(object):
def test(self):
doc = 'Dog! Cat! The sentence with rat is too long.'
query = 'rat'
snippet = snippets.highlight_doc(doc, query, max_chars=10, max_sents=1)
assert snippet in 'Dog!', 'Cat!'
#
# Testing "private" functions
#
class TestFindQuerySpans(object):
def test_single_1(self):
words = query = 'pizza'.split()
spans = snippets._find_query_spans(words, query)
assert spans == [(0, 1)]
def test_single_2(self):
words = 'Pizza is great .'.split()
query = 'pizza'.split()
spans = snippets._find_query_spans(words, query)
assert spans == [(0, 1)]
def test_multiple(self):
words = """My favorite pizza is deep dish . Deep dish pizza is hella
sick pizza bro .""".split()
query = 'deep dish pizza'.split()
spans = snippets._find_query_spans(words, query)
assert spans == [(2, 3), (4, 6), (7, 10), (13, 14)]
def test_overlap(self):
words = 'pizza pepperoni pizza'.split()
query = 'pepperoni pizza'.split()
spans = snippets._find_query_spans(words, query)
assert spans == [(0, 1), (1, 3)]
def test_full(self):
words = 'Pizza ? I love deep dish ! Deep dish pizza is great .'.split()
query = 'deep dish pizza'.split()
spans = snippets._find_query_spans(words, query)
assert spans == [(0, 1), (4, 6), (7, 10)]
#
# Testing "private" string-utility functions
#
class TestSplitIntoSentences(object):
def test_single_period(self):
doc = 'That sure is a nice hat.'
assert len(snippets._split_into_sentences(doc)) == 1
def test_single_with_comma(self):
doc = 'I like apples, oranges, and peas.'
sentences = snippets._split_into_sentences(doc)
assert sentences == ['I like apples, oranges, and peas.']
def test_single_questionmark(self):
doc = 'You like my hat?'
sentences = snippets._split_into_sentences(doc)
assert sentences == ['You like my hat?']
def test_single_exclamationmark(self):
doc = 'Of course I do!'
sentences = snippets._split_into_sentences(doc)
assert sentences == ['Of course I do!']
def test_single_ellipsis(self):
doc = 'The food was alright...'
sentences = snippets._split_into_sentences(doc)
assert sentences == ['The food was alright...']
def test_double_period(self):
doc = 'I am tired. Go away.'
sentences = snippets._split_into_sentences(doc)
assert sentences == ['I am tired.', 'Go away.']
def test_double_marks(self):
doc = 'I am tired! Would you leave?'
sentences = snippets._split_into_sentences(doc)
assert sentences == ['I am tired!', 'Would you leave?']
def test_double_ellipsis(self):
doc = 'Well whatever... Yeah whatever...'
sentences = snippets._split_into_sentences(doc)
print sentences
assert sentences == ['Well whatever...', 'Yeah whatever...']
def test_double_period(self):
doc = 'I am tired. Go away.'
sentences = snippets._split_into_sentences(doc)
assert sentences == ['I am tired.', 'Go away.']
def test_dollar_sign(self):
doc = 'I paid $2.34 for it.'
sentences = snippets._split_into_sentences(doc)
print sentences
assert sentences == ['I paid $2.34 for it.']
def test_no_punctuation(self):
doc = 'I want some Thai food'
sentences = snippets._split_into_sentences(doc)
assert sentences == ['I want some Thai food']
class TestSplitIntoWords(object):
def test_lowercase_period(self):
sentence = 'i am not a frog.'
words = snippets._split_into_words(sentence)
assert words == ['i', 'am', 'not', 'a', 'frog', '.']
def test_mixedcase_period(self):
sentence = 'I am NOT a frog.'
words = snippets._split_into_words(sentence)
assert words == ['I', 'am', 'NOT', 'a', 'frog', '.']
def test_commas(self):
sentence = 'Comma, comma, comma.'
words = snippets._split_into_words(sentence)
assert words == ['Comma', ',', 'comma', ',', 'comma', '.']
def test_exclamationmark(self):
sentence = 'You are SO a frog!'
words = snippets._split_into_words(sentence)
assert words == ['You', 'are', 'SO', 'a', 'frog', '!']
def test_questionmark(self):
sentence = 'Why am I a frog?'
words = snippets._split_into_words(sentence)
assert words == ['Why', 'am', 'I', 'a', 'frog', '?']
def test_ellipsis(self):
sentence = 'The food was horrible...'
words = snippets._split_into_words(sentence)
assert words == ['The', 'food', 'was', 'horrible', '...']
def test_numbers_own_word(self):
sentence = 'There are 4 lights.'
words = snippets._split_into_words(sentence)
assert words == ['There', 'are', '4', 'lights', '.']
def test_numbers_part_word(self):
sentence = 'I am word47word.'
words = snippets._split_into_words(sentence)
assert words == ['I', 'am', 'word47word', '.']
def test_dollar_in_word(self):
sentence = 'It was $10.'
words = snippets._split_into_words(sentence)
assert words == ['It', 'was', '$10', '.']
def test_dollar_own_word_1(self):
sentence = 'That place was $.'
words = snippets._split_into_words(sentence)
assert words == ['That', 'place', 'was', '$', '.']
def test_dollar_own_word_2(self):
sentence = 'That place was $$.'
words = snippets._split_into_words(sentence)
assert words == ['That', 'place', 'was', '$$', '.']
def test_price_1(self):
sentence = 'It costs $47.'
words = snippets._split_into_words(sentence)
assert words == ['It', 'costs', '$47', '.']
def test_price_2(self):
sentence = 'It costs $9.47.'
words = snippets._split_into_words(sentence)
assert words == ['It', 'costs', '$9.47', '.']
def test_price_3(self):
sentence = 'It costs .47.'
words = snippets._split_into_words(sentence)
assert words == ['It', 'costs', '.47', '.']
class TestJoinWords(object):
def test_simple(self):
words = ['How', 'are', 'you']
joined = snippets._join_words(words)
assert joined == 'How are you'
def test_question(self):
words = ['how', 'are', 'you', '?']
joined = snippets._join_words(words)
assert joined == 'how are you?'
def test_exclamation(self):
words = ['Go', 'away', '!']
joined = snippets._join_words(words)
assert joined == "Go away!"
def test_multiple_marks(self):
words = ['Go', 'away', '!', 'I', "don't", 'like', 'you', '.']
joined = snippets._join_words(words)
assert joined == "Go away! I don't like you."
def test_mixed(self):
words = ['I', 'paid', '$24', '.', 'So', 'there', '!']
joined = snippets._join_words(words)
assert joined == 'I paid $24. So there!'
def test_ellipsis(self):
words = ['Whatever', '...', 'Hey', 'there', '.']
joined = snippets._join_words(words)
assert joined == 'Whatever... Hey there.'