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)
| 444 | |
| 445 | |
| 446 | def 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 | |
| 487 | def cleanup_efficiency(diffs, editcost=4): |
no test coverage detected