1919
2020from intbitset import intbitset
2121
22- from licensedcode import SMALL_RULE
22+ from licensedcode import SMALL_RULE , match_unknown
2323from licensedcode .legalese import common_license_words
2424from licensedcode import match
2525from licensedcode import match_aho
2626from licensedcode import match_hash
2727from licensedcode import match_seq
2828from licensedcode import match_set
2929from licensedcode import match_spdx_lid
30+ from licensedcode import match_unknown
3031from licensedcode .dmp import match_blocks as match_blocks_dmp
3132from licensedcode .seq import match_blocks as match_blocks_seq
3233from licensedcode import query
@@ -128,6 +129,7 @@ class LicenseIndex(object):
128129 'rules_automaton' ,
129130 'fragments_automaton' ,
130131 'starts_automaton' ,
132+ 'unknown_ngrams' ,
131133
132134 'regular_rids' ,
133135 'false_positive_rids' ,
@@ -136,7 +138,7 @@ class LicenseIndex(object):
136138 'optimized' ,
137139 )
138140
139- def __init__ (self , rules = None , _legalese = common_license_words , _spdx_tokens = frozenset ()):
141+ def __init__ (self , rules = None , _legalese = common_license_words , _spdx_tokens = frozenset (), _unknown_ngram_length = 7 ):
140142 """
141143 Initialize the index with an iterable of Rule objects.
142144 `_legalese` is a set of common license-specific words aka. legalese
@@ -185,6 +187,7 @@ def __init__(self, rules=None, _legalese=common_license_words, _spdx_tokens=froz
185187 self .rules_automaton = match_aho .get_automaton ()
186188 self .fragments_automaton = USE_AHO_FRAGMENTS and match_aho .get_automaton ()
187189 self .starts_automaton = USE_RULE_STARTS and match_aho .get_automaton ()
190+ self .unknown_ngrams = match_aho .get_automaton ()
188191
189192 # disjunctive sets of rule ids: regular and false positive
190193
@@ -206,7 +209,7 @@ def __init__(self, rules=None, _legalese=common_license_words, _spdx_tokens=froz
206209 logger_debug ('LicenseIndex: building index.' )
207210 # index all and optimize
208211 self ._add_rules (
209- rules , _legalese = _legalese , _spdx_tokens = _spdx_tokens )
212+ rules , _legalese = _legalese , _spdx_tokens = _spdx_tokens , , _unknown_ngram_length = _unknown_ngram_length )
210213
211214 if TRACE_TOKEN_DOC_FREQ :
212215 logger_debug ('LicenseIndex: token, frequency' )
@@ -222,7 +225,7 @@ def __init__(self, rules=None, _legalese=common_license_words, _spdx_tokens=froz
222225 '%(duration)f seconds.' % locals ())
223226 self ._print_index_stats ()
224227
225- def _add_rules (self , rules , _legalese = common_license_words , _spdx_tokens = frozenset ()):
228+ def _add_rules (self , rules , _legalese = common_license_words , _spdx_tokens = frozenset (), _unknown_ngram_length = 7 ):
226229 """
227230 Add a list of Rule objects to the index and constructs optimized and
228231 immutable index structures.
@@ -358,6 +361,12 @@ def _add_rules(self, rules, _legalese=common_license_words, _spdx_tokens=frozens
358361 rid_by_hash [rule_hash ] = rid
359362 regular_rids_add (rid )
360363
364+ match_unknown .add_ngrams (
365+ automaton = self .unknown_ngrams ,
366+ tids = rule_token_ids ,
367+ rule_length = rule .length ,
368+ unknown_ngram_length = _unknown_ngram_length ,
369+ )
361370 # Some rules cannot be matched as a sequence are "weak" rules
362371 if not is_weak :
363372 approx_matchable_rids_add (rid )
0 commit comments