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

Function match_fragments

src/licensedcode/match_aho.py:193–269  ·  view source on GitHub ↗

Return a list of Span by matching the `query_run` against the `automaton` and `idx` index. This is using a BLAST-like matching approach: we match ngram fragments of rules (e.g. a seed) and then we extend left and right.

(idx, query_run)

Source from the content-addressed store, hash-verified

191
192
193def match_fragments(idx, query_run):
194 """
195 Return a list of Span by matching the `query_run` against the `automaton`
196 and `idx` index.
197
198 This is using a BLAST-like matching approach: we match ngram fragments of
199 rules (e.g. a seed) and then we extend left and right.
200 """
201 if TRACE_FRAG:
202 logger_debug('-------------->match_fragments')
203
204 # Get matches using the AHO Fragments automaton
205 matches = exact_match(
206 idx, query_run, automaton=idx.fragments_automaton, matcher=MATCH_AHO_FRAG)
207 if TRACE_FRAG:
208 logger_debug('match_fragments')
209 for m in matches:
210 print(m)
211
212 # Discard fragments that have any already matched positions in any previous matches
213 matches, _discarded = filter_already_matched_overlapping_matches(matches, query_run.query)
214
215 # Merge matches with a zero max distance, e.g. contiguous or overlapping
216 # with matches to the same rule
217 from licensedcode.match import merge_matches
218 matches = merge_matches(matches, max_dist=0)
219
220 # extend matched fragments left and right. We group by rule
221 from licensedcode.seq import extend_match
222
223 rules_by_rid = idx.rules_by_rid
224 tids_by_rid = idx.tids_by_rid
225 len_legalese = idx.len_legalese
226
227 alo = qbegin = query_run.start
228 ahi = query_run.end
229 query = query_run.query
230 qtokens = query.tokens
231 matchables = query_run.matchables
232
233 frag_matches = []
234
235 keyf = lambda m: m.rule.rid
236 matches.sort(key=keyf)
237 matches_by_rule = groupby(matches, key=keyf)
238
239 for rid, rule_matches in matches_by_rule:
240 itokens = tids_by_rid[rid]
241 blo, bhi = 0, len(itokens)
242 rule = rules_by_rid[rid]
243
244 for match in rule_matches:
245 i, j , k = match.qstart, match.istart, match.len()
246 # extend alignment left and right as long as we have matchables
247 qpos, ipos, mlen = extend_match(
248 i, j, k, qtokens, itokens,
249 alo, ahi, blo, bhi, matchables)
250

Callers

nothing calls this directly

Calls 10

lenMethod · 0.95
merge_matchesFunction · 0.90
extend_matchFunction · 0.90
SpanClass · 0.90
LicenseMatchClass · 0.90
exact_matchFunction · 0.85
logger_debugFunction · 0.70
sortMethod · 0.45
appendMethod · 0.45

Tested by

no test coverage detected