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

Method _add_rules

src/licensedcode/index.py:270–577  ·  view source on GitHub ↗

Add a list of Rule objects to the index and constructs optimized and immutable index structures. ``_legalese`` is a sorted mapping of common license-specific words aka. legalese as {token: id} ``_spdx_tokens`` is a set of token strings used in SPDX license identifie

(
        self,
        rules,
        _legalese=common_license_words,
        _spdx_tokens=frozenset(),
        _license_tokens=frozenset(),
    )

Source from the content-addressed store, hash-verified

268 self._print_index_stats()
269
270 def _add_rules(
271 self,
272 rules,
273 _legalese=common_license_words,
274 _spdx_tokens=frozenset(),
275 _license_tokens=frozenset(),
276 ):
277 """
278 Add a list of Rule objects to the index and constructs optimized and
279 immutable index structures.
280
281 ``_legalese`` is a sorted mapping of common license-specific words aka. legalese as {token: id}
282 ``_spdx_tokens`` is a set of token strings used in SPDX license identifiers
283 ``_license_tokens`` is a set of "license" tokens used as start or end of a rule
284 """
285 if self.optimized:
286 raise Exception('Index has been optimized and cannot be updated.')
287
288 # initial dictionary mapping for known legalese tokens
289 ########################################################################
290
291 # TODO: we should start enumerating at 1 below: token ids then become
292 # valid "unichr" values, making it easier downstream when used in
293 # automatons
294
295 self.dictionary = dictionary = dict(_legalese)
296 dictionary_get = dictionary.get
297
298 self.len_legalese = len_legalese = len(set(dictionary.values()))
299 highest_tid = len_legalese -1
300
301 # Add SPDX key tokens to the dictionary: these are always treated as
302 # non-legalese. This may seem weird but they are detected in expressions
303 # alright and some of their tokens exist as rules too (e.g. GPL).
304 # Treating their words as legalese by default creates problems as common
305 # words such as mit may become legalese words even though we do not want
306 # this to happen.
307 ########################################################################
308 for sts in sorted(_spdx_tokens):
309 stid = dictionary_get(sts)
310 if stid is None:
311 # we have a never yet seen token, so we assign a new tokenid
312 highest_tid += 1
313 stid = highest_tid
314 dictionary[sts] = stid
315
316 self.rules_by_rid = rules_by_rid = list(rules)
317 self.rules_by_id = {r.identifier: r for r in self.rules_by_rid}
318 if TRACE_INDEXING:
319 for _rid, _rule in enumerate(rules_by_rid):
320 logger_debug('rules_by_rid:', _rid, _rule)
321
322 # ensure that rules are sorted
323 rules_by_rid.sort()
324 len_rules = len(rules_by_rid)
325
326 # create index data structures
327 # OPTIMIZATION: bind frequently used methods to the local scope for

Callers 3

__init__Method · 0.95
test_index_structuresMethod · 0.80

Calls 11

partialFunction · 0.85
DuplicateRuleErrorClass · 0.85
addMethod · 0.80
compute_thresholdsMethod · 0.80
logger_debugFunction · 0.70
valuesMethod · 0.45
sortMethod · 0.45
tokensMethod · 0.45
appendMethod · 0.45
rule_fileMethod · 0.45
joinMethod · 0.45

Tested by 2

test_index_structuresMethod · 0.64