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

Function group_matches

src/licensedcode/detection.py:1820–1868  ·  view source on GitHub ↗

Given a list of ``license_matches`` LicenseMatch objects, yield lists of grouped matches together where each group is less than `lines_threshold` apart, while also considering presence of license intros.

(license_matches, lines_threshold=LINES_THRESHOLD)

Source from the content-addressed store, hash-verified

1818
1819
1820def group_matches(license_matches, lines_threshold=LINES_THRESHOLD):
1821 """
1822 Given a list of ``license_matches`` LicenseMatch objects, yield lists of
1823 grouped matches together where each group is less than `lines_threshold`
1824 apart, while also considering presence of license intros.
1825 """
1826 group_of_license_matches = []
1827
1828 for license_match in license_matches:
1829 # If this is the first match or the start of another group after yielding
1830 # the contents of the previous group
1831 if not group_of_license_matches:
1832 group_of_license_matches.append(license_match)
1833 continue
1834
1835 previous_match = group_of_license_matches[-1]
1836 is_in_group_by_threshold = license_match.start_line <= previous_match.end_line + lines_threshold
1837
1838 # If the previous match is an intro, we should keep this match in the group
1839 # This is regardless of line number difference being more than threshold
1840 if previous_match.rule.is_license_intro:
1841 group_of_license_matches.append(license_match)
1842
1843 # If the current match is an intro, we should create a new group
1844 # This is regardless of line number difference being less than threshold
1845 elif license_match.rule.is_license_intro:
1846 yield group_of_license_matches
1847 group_of_license_matches = [license_match]
1848
1849 # If the current match is a license clue, we send this as a
1850 # seperate group
1851 elif license_match.rule.is_license_clue:
1852 yield group_of_license_matches
1853 yield [license_match]
1854 group_of_license_matches = []
1855
1856 # If none of previous or current match has license intro then we look at line numbers
1857 # If line number difference is within threshold, we keep the current match in the group
1858 elif is_in_group_by_threshold:
1859 group_of_license_matches.append(license_match)
1860
1861 # If line number difference is outside threshold, we make a new group
1862 else:
1863 yield group_of_license_matches
1864 group_of_license_matches = [license_match]
1865
1866 # If not an empty group, this is the last group
1867 if group_of_license_matches:
1868 yield group_of_license_matches
1869
1870
1871def get_referenced_filenames(license_matches):

Callers 3

detect_licensesFunction · 0.85

Calls 1

appendMethod · 0.45

Tested by

no test coverage detected