A license detection index. An index is queried for license matches found in a query file. The index support multiple strategies for finding exact and approximate matches.
| 129 | |
| 130 | |
| 131 | class LicenseIndex(object): |
| 132 | """ |
| 133 | A license detection index. An index is queried for license matches found in |
| 134 | a query file. The index support multiple strategies for finding exact and |
| 135 | approximate matches. |
| 136 | """ |
| 137 | # slots are not really needed but they help with sanity and avoid an |
| 138 | # unchecked proliferation of new attributes |
| 139 | __slots__ = ( |
| 140 | 'len_legalese', |
| 141 | 'dictionary', |
| 142 | 'digit_only_tids', |
| 143 | |
| 144 | 'rules_by_id', |
| 145 | 'rules_by_rid', |
| 146 | 'tids_by_rid', |
| 147 | |
| 148 | 'high_postings_by_rid', |
| 149 | |
| 150 | 'sets_by_rid', |
| 151 | 'msets_by_rid', |
| 152 | |
| 153 | 'rid_by_hash', |
| 154 | 'rules_automaton', |
| 155 | 'fragments_automaton', |
| 156 | 'starts_automaton', |
| 157 | 'unknown_automaton', |
| 158 | |
| 159 | 'regular_rids', |
| 160 | 'false_positive_rids', |
| 161 | 'approx_matchable_rids', |
| 162 | |
| 163 | 'optimized', |
| 164 | 'all_languages', |
| 165 | ) |
| 166 | |
| 167 | def __init__( |
| 168 | self, |
| 169 | rules=None, |
| 170 | _legalese=common_license_words, |
| 171 | _spdx_tokens=frozenset(), |
| 172 | _license_tokens=frozenset(), |
| 173 | _all_languages=False, |
| 174 | ): |
| 175 | """ |
| 176 | Initialize the index with an iterable of Rule objects. |
| 177 | ``_legalese`` is a sorted mapping of common license-specific words aka. legalese as {token: id} |
| 178 | ``_spdx_tokens`` is a set of tokens used in SPDX license identifiers |
| 179 | ``_license_tokens`` is a set of "license" tokens used as start or end of a rule |
| 180 | If ``_all_languages`` is True, use all spoken languages license and rules. |
| 181 | Otherwise, use only English rules and licenses. |
| 182 | """ |
| 183 | # largest token ID for a "legalese" token. A token with a larger id than |
| 184 | # len_legalese is considered a "junk" very common token |
| 185 | self.len_legalese = 0 |
| 186 | |
| 187 | # mapping of token string > integer token id |
| 188 | self.dictionary = {} |
no outgoing calls