Return an iterable of tokens from a rule or query ``text`` using index tokenizing rules. Ignore words that exist as lowercase in the ``stopwords`` set. For example:: >>> list(index_tokenizer('')) [] >>> x = list(index_tokenizer('some Text with spAces! + _ -')) >>>
(text, stopwords=STOPWORDS, preserve_case=False)
| 215 | |
| 216 | |
| 217 | def index_tokenizer(text, stopwords=STOPWORDS, preserve_case=False): |
| 218 | """ |
| 219 | Return an iterable of tokens from a rule or query ``text`` using index |
| 220 | tokenizing rules. Ignore words that exist as lowercase in the ``stopwords`` |
| 221 | set. |
| 222 | |
| 223 | For example:: |
| 224 | >>> list(index_tokenizer('')) |
| 225 | [] |
| 226 | >>> x = list(index_tokenizer('some Text with spAces! + _ -')) |
| 227 | >>> assert x == ['some', 'text', 'with', 'spaces'] |
| 228 | |
| 229 | >>> x = list(index_tokenizer('{{}some }}Text with spAces! + _ -')) |
| 230 | >>> assert x == ['some', 'text', 'with', 'spaces'] |
| 231 | |
| 232 | >>> x = list(index_tokenizer('{{Hi}}some {{}}Text with{{noth+-_!@ing}} {{junk}}spAces! + _ -{{}}')) |
| 233 | >>> assert x == ['hi', 'some', 'text', 'with', 'noth+', 'ing', 'junk', 'spaces'] |
| 234 | |
| 235 | >>> stops = set(['quot', 'lt', 'gt']) |
| 236 | >>> x = list(index_tokenizer('some "< markup >"', stopwords=stops)) |
| 237 | >>> assert x == ['some', 'markup'] |
| 238 | """ |
| 239 | if not text: |
| 240 | return [] |
| 241 | if not preserve_case: |
| 242 | text = text.lower() |
| 243 | words = word_splitter(text) |
| 244 | return (token for token in words if token and token not in stopwords) |
| 245 | |
| 246 | |
| 247 | def index_tokenizer_with_stopwords(text, stopwords=STOPWORDS): |
no outgoing calls