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

Function half_match_i

src/licensedcode/dmp.py:446–484  ·  view source on GitHub ↗

Does a substring of shorttext exist within longtext such that the substring is at least half the length of longtext? Args: longtext: Longer string. shorttext: Shorter string. i: Start index of quarter length substring within longtext. Returns: Five element

(longtext, shorttext, i, len_longtext)

Source from the content-addressed store, hash-verified

444
445
446def half_match_i(longtext, shorttext, i, len_longtext):
447 """
448 Does a substring of shorttext exist within longtext such that the substring
449 is at least half the length of longtext?
450
451 Args:
452 longtext: Longer string.
453 shorttext: Shorter string.
454 i: Start index of quarter length substring within longtext.
455
456 Returns:
457 Five element Array, containing:
458 - the prefix of longtext,
459 - the suffix of longtext,
460 - the prefix of shorttext,
461 - the suffix of shorttext
462 - the common middle.
463 Or None if there was no match.
464 """
465 seed = longtext[i:i + len_longtext // 4]
466 best_common = ''
467 j = shorttext.find(seed)
468 while j != -1:
469 prefixLength = common_prefix(longtext[i:], shorttext[j:])
470 suffixLength = common_suffix(longtext[:i], shorttext[:j])
471
472 if len(best_common) < suffixLength + prefixLength:
473 best_common = (shorttext[j - suffixLength:j] + shorttext[j:j + prefixLength])
474 best_longtext_a = longtext[:i - suffixLength]
475 best_longtext_b = longtext[i + prefixLength:]
476 best_shorttext_a = shorttext[:j - suffixLength]
477 best_shorttext_b = shorttext[j + prefixLength:]
478 j = shorttext.find(seed, j + 1)
479
480 if len(best_common) * 2 >= len_longtext:
481 return (
482 best_longtext_a, best_longtext_b,
483 best_shorttext_a, best_shorttext_b,
484 best_common)
485
486
487def cleanup_efficiency(diffs, editcost=4):

Callers 1

half_matchFunction · 0.85

Calls 3

common_prefixFunction · 0.70
common_suffixFunction · 0.70
findMethod · 0.45

Tested by

no test coverage detected