|
18 | 18 |
|
19 | 19 | import datetime |
20 | 20 | import re |
| 21 | +import sys |
21 | 22 |
|
22 | 23 |
|
23 | 24 | TEXT_RESULT_LENGTH = 42 |
@@ -201,38 +202,36 @@ def search(self, words, tags): |
201 | 202 | if add_text_to_results: |
202 | 203 | results.append(get_text_with_dots(self.text, 0, TEXT_RESULT_LENGTH)) |
203 | 204 | else: |
204 | | - for word in words: |
205 | | - # Any of the word matches with the date. |
206 | | - if word in str(self): |
207 | | - # We don't want to show search results from the text matching |
208 | | - # this date. |
209 | | - words.remove(word) |
210 | | - results.append(get_text_with_dots(self.text, 0, TEXT_RESULT_LENGTH)) |
211 | | - # if all the words matched agains the date, return |
212 | | - if not words: |
213 | | - return str(self), words |
214 | | - text_result = self.search_in_text(words) |
| 205 | + non_date_words = [word for word in words if word not in str(self)] |
| 206 | + words_contain_date = len(non_date_words) != len(words) |
| 207 | + if words_contain_date: |
| 208 | + results.append(get_text_with_dots(self.text, 0, TEXT_RESULT_LENGTH)) |
| 209 | + # If all the words matched agains the date, return. |
| 210 | + if not non_date_words: |
| 211 | + return str(self), results |
| 212 | + |
| 213 | + text_result = self.search_in_text(non_date_words) |
215 | 214 | if text_result: |
216 | 215 | results.append(text_result) |
217 | | - results.extend(self.search_in_categories(words)) |
| 216 | + results.extend(self.search_in_categories(non_date_words)) |
218 | 217 | return str(self), results |
219 | 218 |
|
220 | 219 | def search_in_text(self, words): |
221 | | - """All words should be present in the text""" |
222 | | - matches = {} |
| 220 | + """ |
| 221 | + If all words are in the text, return a suitable text substring. |
| 222 | + Otherwise, return None. |
| 223 | + """ |
| 224 | + match_word, smallest_index = None, sys.maxsize |
223 | 225 | for word in words: |
224 | | - index = self.text.upper().find(word.upper()) |
| 226 | + index = self.text.lower().find(word.lower()) |
225 | 227 | if index < 0: |
226 | 228 | return |
227 | | - matches[word] = index |
228 | | - |
229 | | - # Of all the matching words, find the one which appears first |
230 | | - match_word = min(matches, key=matches.get) |
231 | | - first_occurrence = matches[match_word] |
| 229 | + if index < smallest_index: |
| 230 | + match_word, smallest_index = word, index |
232 | 231 |
|
233 | | - found_text = self.text[first_occurrence : first_occurrence + len(match_word)] |
| 232 | + found_text = self.text[smallest_index : smallest_index + len(match_word)] |
234 | 233 | return get_text_with_dots( |
235 | | - self.text, first_occurrence, first_occurrence + len(match_word), found_text |
| 234 | + self.text, smallest_index, smallest_index + len(match_word), found_text |
236 | 235 | ) |
237 | 236 |
|
238 | 237 | def search_in_categories(self, words): |
|
0 commit comments