in lib/src/license_detection/token_matcher.dart [127:182]
List<MatchRange> getTargetMatchedRanges(
LicenseWithNGrams knownLicense,
LicenseWithNGrams unknownLicense,
int n,
) {
var offsetMap = <int, List<MatchRange>>{};
var matches = <MatchRange>[];
for (var tgtChecksum in unknownLicense.nGrams) {
var srcChecksums = knownLicense.checksumMap[tgtChecksum.checksum];
// Check if source contains the checksum.
if (srcChecksums == null) {
continue;
}
// Iterate over all the trigrams in source having the same checksums.
for (var i = 0; i < srcChecksums.length; i++) {
var srcChecksum = srcChecksums[i];
final offset = tgtChecksum.start - srcChecksum.start;
// Check if this source checksum extend the last match
// and update the last match for this offset accordingly.
var matches = offsetMap[offset];
if (matches != null && (matches.last.input.end == tgtChecksum.end - 1)) {
matches.last = matches.last.update(
inputEnd: tgtChecksum.end,
srcEnd: srcChecksum.end,
);
offsetMap[offset] = matches;
continue;
}
// Add new instance of matchRange if doesn't extend the last
// match of the same offset.
offsetMap.putIfAbsent(offset, () => []).add(
MatchRange(Range(tgtChecksum.start, tgtChecksum.end),
Range(srcChecksum.start, srcChecksum.end), n),
);
}
}
for (var list in offsetMap.values) {
// Update the token count of match range.
for (var match in list) {
final start = match.input.start;
final end = match.input.end;
match = match.update(newClaim: end - start);
matches.add(match);
}
}
// Sort the matches based on the number of tokens covered in match
// range in descending order.
matches.sort(_compareTokenCount);
return List.unmodifiable(matches);
}