// potentially slow (not using levenLimited) // returns edit distance or null if no match at all static Int mmo2_levenWithSwapsScore(MMOPattern pattern, S s) { ret mmo2_levenWithSwapsScore(pattern, s, false, false); } static Int mmo2_levenWithSwapsScore(MMOPattern pattern, S s, bool startOfLine, bool endOfLine) { S s2 = trim(s); if (pattern == null) null; if (startsWith(s2, "#")) null; // hashtags not handled here if (pattern cast MMOPattern.StartOfLine) ret mmo2_levenWithSwapsScore(pattern.p, s2, true, endOfLine); if (pattern cast MMOPattern.EndOfLine) ret mmo2_levenWithSwapsScore(pattern.p, s2, startOfLine, true); if (pattern cast MMOPattern.Phrase) { if (pattern.quoted) ret mmo2_match(pattern, s2) ? (Int) 0 : null; S p = pattern.phrase; if (startsWith(p, "#")) null; // hashtags not handled here if (endsWith(p, "!")) ret find3(p, s) ? 0 : null; else ret find3_levenWithSwapsDistance(p, s2); } if (pattern cast MMOPattern.And) { int score = 0; for (MMOPattern pat : pattern.l) { Int score2 = mmo2_levenWithSwapsScore(pat, s2, startOfLine, endOfLine); if (score2 == null) null; score += score2; } ret score; } if (pattern cast MMOPattern.Or) { Int min = null; for (MMOPattern pat : pattern.l) min = min_withNull(min, mmo2_levenWithSwapsScore(pat, s2, startOfLine, endOfLine)); ret min; } // not clauses are not checked for typos if (pattern instanceof MMOPattern.Not) ret mmo2_match(pattern, s2) ? 0 : null; fail("what. " + pattern); } static Int mmo2_levenWithSwapsScore(S pattern, S s) { ret mmo2_levenWithSwapsScore(mmo2_parsePattern(pattern), s, false, false); }