MCPcopy Create free account
hub / github.com/aboutcode-org/scancode-toolkit / index_tokenizer

Function index_tokenizer

src/licensedcode/tokenize.py:217–244  ·  view source on GitHub ↗

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)

Source from the content-addressed store, hash-verified

215
216
217def 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 &quot&lt markup &gt&quot', 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
247def index_tokenizer_with_stopwords(text, stopwords=STOPWORDS):

Calls

no outgoing calls