Return a list of token query position Spans, where each Span represents a region of related LicenseMatch contained that Span given a ``matches`` list of LicenseMatch. Matching regions are such that: - all matches in the regions are entirely contained in the region Span Tw
(
matches,
min_tokens_gap=10,
min_lines_gap=3,
trace=TRACE_REGIONS,
)
| 2323 | |
| 2324 | |
| 2325 | def get_matching_regions( |
| 2326 | matches, |
| 2327 | min_tokens_gap=10, |
| 2328 | min_lines_gap=3, |
| 2329 | trace=TRACE_REGIONS, |
| 2330 | ): |
| 2331 | """ |
| 2332 | Return a list of token query position Spans, where each Span represents a |
| 2333 | region of related LicenseMatch contained that Span given a ``matches`` list |
| 2334 | of LicenseMatch. |
| 2335 | |
| 2336 | Matching regions are such that: |
| 2337 | |
| 2338 | - all matches in the regions are entirely contained in the region Span |
| 2339 | |
| 2340 | Two consecutive region Spans are such that: |
| 2341 | |
| 2342 | - there are no overlaping matches between them |
| 2343 | - there are at least ``min_tokens_gap`` unmatched tokens between them |
| 2344 | - OR there are at least ``min_lines_gap`` unmatched lines between them |
| 2345 | """ |
| 2346 | regions = [] |
| 2347 | |
| 2348 | prev_region = None |
| 2349 | prev_region_lines = None |
| 2350 | cur_region = None |
| 2351 | cur_region_lines = None |
| 2352 | |
| 2353 | for match in matches: |
| 2354 | if trace: |
| 2355 | logger_debug('Match:', match) |
| 2356 | if not prev_region: |
| 2357 | prev_region = match.qregion() |
| 2358 | prev_region_lines = match.qregion_lines() |
| 2359 | else: |
| 2360 | cur_region = match.qregion() |
| 2361 | cur_region_lines = match.qregion_lines() |
| 2362 | |
| 2363 | if trace: |
| 2364 | logger_debug( |
| 2365 | ' prev_region:', prev_region, |
| 2366 | 'cur_region:', cur_region, |
| 2367 | 'prev_region.distance_to(cur_region):', |
| 2368 | prev_region.distance_to(cur_region), |
| 2369 | ) |
| 2370 | logger_debug( |
| 2371 | ' prev_region_lines:', prev_region_lines, |
| 2372 | 'cur_region_lines:', cur_region_lines, |
| 2373 | 'prev_region_lines.distance_to(cur_region_lines):', |
| 2374 | prev_region_lines.distance_to(cur_region_lines) |
| 2375 | ) |
| 2376 | |
| 2377 | if (prev_region.distance_to(cur_region) > min_tokens_gap |
| 2378 | or prev_region_lines.distance_to(cur_region_lines) > min_lines_gap |
| 2379 | ): |
| 2380 | |
| 2381 | regions.append(prev_region) |
| 2382 |