forked from aboutcode-org/scancode-toolkit
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtokenize.py
More file actions
362 lines (290 loc) · 12.3 KB
/
tokenize.py
File metadata and controls
362 lines (290 loc) · 12.3 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
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
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
# -*- coding: utf-8 -*-
#
# Copyright (c) nexB Inc. and others. All rights reserved.
# ScanCode is a trademark of nexB Inc.
# SPDX-License-Identifier: Apache-2.0
# See http://www.apache.org/licenses/LICENSE-2.0 for the license text.
# See https://github.com/nexB/scancode-toolkit for support or download.
# See https://aboutcode.org for more information about nexB OSS projects.
#
import re
from collections import defaultdict
from binascii import crc32
from itertools import islice
from licensedcode.stopwords import STOPWORDS
from textcode.analysis import numbered_text_lines
"""
Utilities to break texts in lines and tokens (aka. words) with specialized
version for queries and rules texts.
"""
def query_lines(
location=None,
query_string=None,
strip=True,
start_line=1,
plain_text=False,
):
"""
Return an iterable of tuples (line number, text line) given a file at
`location` or a `query string`. Include empty lines.
Line numbers start at ``start_line`` which is 1-based by default.
If `plain_text` is True treat the file as a plain text file and do not
attempt to detect its type and extract its content with special procedures.
This is used mostly when loading license texts and rules.
"""
# TODO: OPTIMIZE: tokenizing line by line may be rather slow
# we could instead get lines and tokens at once in a batch?
numbered_lines = []
if location:
numbered_lines = numbered_text_lines(
location,
demarkup=False,
start_line=start_line,
plain_text=plain_text,
)
elif query_string:
if strip:
keepends = False
else:
keepends = True
numbered_lines = enumerate(
query_string.splitlines(keepends),
start_line,
)
for line_number, line in numbered_lines:
if strip:
yield line_number, line.strip()
else:
yield line_number, line.rstrip('\n') + '\n'
# Split on whitespace and punctuations: keep only characters and numbers and +
# when in the middle or end of a word. Keeping the trailing + is important for
# licenses name such as GPL2+. The use a double negation "not non word" meaning
# "words" to define the character ranges
query_pattern = '[^_\\W]+\\+?[^_\\W]*'
word_splitter = re.compile(query_pattern, re.UNICODE).findall
key_phrase_pattern = '(?:' + query_pattern + '|\\{\\{|\\}\\})'
key_phrase_splitter = re.compile(key_phrase_pattern, re.UNICODE).findall
KEY_PHRASE_OPEN = '{{'
KEY_PHRASE_CLOSE = '}}'
# FIXME: this should be folded in a single pass tokenization with the index_tokenizer
def key_phrase_tokenizer(text, stopwords=STOPWORDS):
"""
Yield tokens from a rule ``text`` including key phrases {{brace}} markers.
This tokenizer behaves the same as as the ``index_tokenizer`` returning also
KEY_PHRASE_OPEN and KEY_PHRASE_CLOSE as separate tokens so that they can be
used to parse key phrases.
>>> x = list(key_phrase_splitter('{{AGPL-3.0 GNU Affero License v3.0}}'))
>>> assert x == ['{{', 'AGPL', '3', '0', 'GNU', 'Affero', 'License', 'v3', '0', '}}'], x
>>> x = list(key_phrase_splitter('{{{AGPL{{{{Affero }}License}}0}}'))
>>> assert x == ['{{', 'AGPL', '{{', '{{', 'Affero', '}}', 'License', '}}', '0', '}}'], x
>>> list(index_tokenizer('')) == []
True
>>> x = list(index_tokenizer('{{AGPL-3.0 GNU Affero License v3.0}}'))
>>> assert x == ['agpl', '3', '0', 'gnu', 'affero', 'license', 'v3', '0']
>>> x = list(key_phrase_tokenizer('{{AGPL-3.0 GNU Affero License v3.0}}'))
>>> assert x == ['{{', 'agpl', '3', '0', 'gnu', 'affero', 'license', 'v3', '0', '}}']
"""
if not text:
return
for token in key_phrase_splitter(text.lower()):
if token and token not in stopwords:
yield token
def index_tokenizer(text, stopwords=STOPWORDS):
"""
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! + _ -'))
>>> assert x == ['some', 'text', 'with', 'spaces']
>>> x = list(index_tokenizer('{{}some }}Text with spAces! + _ -'))
>>> assert x == ['some', 'text', 'with', 'spaces']
>>> x = list(index_tokenizer('{{Hi}}some {{}}Text with{{noth+-_!@ing}} {{junk}}spAces! + _ -{{}}'))
>>> assert x == ['hi', 'some', 'text', 'with', 'noth+', 'ing', 'junk', 'spaces']
>>> stops = set(['quot', 'lt', 'gt'])
>>> x = list(index_tokenizer('some "< markup >"', stopwords=stops))
>>> assert x == ['some', 'markup']
"""
if not text:
return []
words = word_splitter(text.lower())
return (token for token in words if token and token not in stopwords)
def index_tokenizer_with_stopwords(text, stopwords=STOPWORDS):
"""
Return a tuple of (tokens, stopwords_by_pos) for a rule
``text`` using index tokenizing rules where tokens is a list of tokens and
stopwords_by_pos is a mapping of {pos: stops count} where "pos" is a token
position and "stops count" is the number of stopword tokens after this
position if any. For stopwords at the start, the position is using the magic
-1 key. Use the lowercase ``stopwords`` set.
For example::
>>> toks, stops = index_tokenizer_with_stopwords('')
>>> assert toks == [], (toks, stops)
>>> assert stops == {}
>>> toks, stops = index_tokenizer_with_stopwords('some Text with spAces! + _ -')
>>> assert toks == ['some', 'text', 'with', 'spaces'], (toks, stops)
>>> assert stops == {}
>>> toks, stops = index_tokenizer_with_stopwords('{{}some }}Text with spAces! + _ -')
>>> assert toks == ['some', 'text', 'with', 'spaces'], (toks, stops)
>>> assert stops == {}
>>> toks, stops = index_tokenizer_with_stopwords('{{Hi}}some {{}}Text with{{noth+-_!@ing}} {{junk}}spAces! + _ -{{}}')
>>> assert toks == ['hi', 'some', 'text', 'with', 'noth+', 'ing', 'junk', 'spaces'], (toks, stops)
>>> assert stops == {}
>>> stops = set(['quot', 'lt', 'gt'])
>>> toks, stops = index_tokenizer_with_stopwords('some "< markup >"', stopwords=stops)
>>> assert toks == ['some', 'markup'], (toks, stops)
>>> assert stops == {0: 2, 1: 2}
>>> toks, stops = index_tokenizer_with_stopwords('{{g', stopwords=stops)
>>> assert toks == ['g'], (toks, stops)
>>> assert stops == {}
"""
if not text:
return [], {}
tokens = []
tokens_append = tokens.append
# we use a defaultdict as a convenience at construction time
# TODO: use the actual words and not just a count
stopwords_by_pos = defaultdict(int)
pos = -1
for token in word_splitter(text.lower()):
if token:
if token in stopwords:
# If we have not yet started, then all tokens seen so far
# are stopwords and we keep a count of them in the magic
# "-1" position.
stopwords_by_pos[pos] += 1
else:
pos += 1
tokens_append(token)
return tokens, dict(stopwords_by_pos)
def query_tokenizer(text):
"""
Return an iterable of tokens from a unicode query text. Do not ignore stop
words. They are handled at a later stage in a query.
For example::
>>> list(query_tokenizer(''))
[]
>>> x = list(query_tokenizer('some Text with spAces! + _ -'))
>>> assert x == ['some', 'text', 'with', 'spaces']
>>> x = list(query_tokenizer('{{}some }}Text with spAces! + _ -'))
>>> assert x == ['some', 'text', 'with', 'spaces']
>>> x = list(query_tokenizer('{{Hi}}some {{}}Text with{{noth+-_!@ing}} {{junk}}spAces! + _ -{{}}'))
>>> assert x == ['hi', 'some', 'text', 'with', 'noth+', 'ing', 'junk', 'spaces']
"""
if not text:
return []
words = word_splitter(text.lower())
return (token for token in words if token)
# Alternate pattern which is the opposite of query_pattern used for
# matched text collection
not_query_pattern = '[_\\W\\s\\+]+[_\\W\\s]?'
# collect tokens and non-token texts in two different groups
_text_capture_pattern = (
'(?P<token>' +
query_pattern +
')' +
'|' +
'(?P<punct>' +
not_query_pattern +
')'
)
tokens_and_non_tokens = re.compile(_text_capture_pattern, re.UNICODE).finditer
def matched_query_text_tokenizer(text):
"""
Return an iterable of tokens and non-tokens punctuation from a unicode query
text keeping everything (including punctuations, line endings, etc.)
The returned iterable contains 2-tuples of:
- True if the string is a text token or False if this is not
(such as punctuation, spaces, etc).
- the corresponding string.
This is used to reconstruct the matched query text for reporting.
"""
if not text:
return
for match in tokens_and_non_tokens(text):
if match:
mgd = match.groupdict()
token = mgd.get('token')
punct = mgd.get('punct')
if token:
yield True, token
elif punct:
yield False, punct
else:
# this should never happen
raise Exception('Internal error in matched_query_text_tokenizer')
def ngrams(iterable, ngram_length):
"""
Return an iterable of ngrams of length `ngram_length` given an `iterable`.
Each ngram is a tuple of `ngram_length` items.
The returned iterable is empty if the input iterable contains less than
`ngram_length` items.
Note: this is a fairly arcane but optimized way to compute ngrams.
For example:
>>> list(ngrams([1,2,3,4,5], 2))
[(1, 2), (2, 3), (3, 4), (4, 5)]
>>> list(ngrams([1,2,3,4,5], 4))
[(1, 2, 3, 4), (2, 3, 4, 5)]
>>> list(ngrams([1,2,3,4], 2))
[(1, 2), (2, 3), (3, 4)]
>>> list(ngrams([1,2,3], 2))
[(1, 2), (2, 3)]
>>> list(ngrams([1,2], 2))
[(1, 2)]
>>> list(ngrams([1], 2))
[]
This also works with arrays or tuples:
>>> from array import array
>>> list(ngrams(array('h', [1,2,3,4,5]), 2))
[(1, 2), (2, 3), (3, 4), (4, 5)]
>>> list(ngrams(tuple([1,2,3,4,5]), 2))
[(1, 2), (2, 3), (3, 4), (4, 5)]
"""
return zip(*(islice(iterable, i, None) for i in range(ngram_length)))
def select_ngrams(ngrams, with_pos=False):
"""
Return an iterable as a subset of a sequence of ngrams using the hailstorm
algorithm. If `with_pos` is True also include the starting position for the
ngram in the original sequence.
Definition from the paper: http://www2009.eprints.org/7/1/p61.pdf
The algorithm first fingerprints every token and then selects a shingle s
if the minimum fingerprint value of all k tokens in s occurs at the first
or the last position of s (and potentially also in between). Due to the
probabilistic properties of Rabin fingerprints the probability that a
shingle is chosen is 2/k if all tokens in the shingle are different.
For example:
>>> list(select_ngrams([(2, 1, 3), (1, 1, 3), (5, 1, 3), (2, 6, 1), (7, 3, 4)]))
[(2, 1, 3), (1, 1, 3), (5, 1, 3), (2, 6, 1), (7, 3, 4)]
Positions can also be included. In this case, tuple of (pos, ngram) are returned:
>>> list(select_ngrams([(2, 1, 3), (1, 1, 3), (5, 1, 3), (2, 6, 1), (7, 3, 4)], with_pos=True))
[(0, (2, 1, 3)), (1, (1, 1, 3)), (2, (5, 1, 3)), (3, (2, 6, 1)), (4, (7, 3, 4))]
This works also from a generator:
>>> list(select_ngrams(x for x in [(2, 1, 3), (1, 1, 3), (5, 1, 3), (2, 6, 1), (7, 3, 4)]))
[(2, 1, 3), (1, 1, 3), (5, 1, 3), (2, 6, 1), (7, 3, 4)]
"""
ngram = None
last = None
for pos, ngram in enumerate(ngrams):
# FIXME: use a proper hash
nghs = []
for ng in ngram:
if isinstance(ng, str):
ng = bytearray(ng, encoding='utf-8')
else:
ng = bytearray(str(ng).encode('utf-8'))
nghs.append(crc32(ng) & 0xffffffff)
min_hash = min(nghs)
if with_pos:
ngram = (pos, ngram,)
if min_hash in (nghs[0], nghs[-1]):
yield ngram
last = ngram
else:
# always yield the first or last ngram too.
if pos == 0:
yield ngram
last = ngram
if last != ngram:
yield ngram