Not logged in.  Login/Logout/Register | List snippets | | Create snippet | Upload image | Upload data

49
LINES

< > BotCompany Repo | #1028535 // mmo2_levenWithSwapsScore - like mmo2_match, but return leven difference

JavaX fragment (include) [tags: use-pretranspiled]

Libraryless. Click here for Pure Java version (4051L/25K).

// 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);
}

Author comment

Began life as a copy of #1026502

download  show line numbers  debug dex  old transpilations   

Travelled to 7 computer(s): bhatertpkbcr, mqqgnosmbjvj, pyentgdyhuwx, pzhvpgtvlbxg, tvejysmllsmz, vouqrxazstgt, xrpafgyirdlv

No comments. add comment

Snippet ID: #1028535
Snippet name: mmo2_levenWithSwapsScore - like mmo2_match, but return leven difference
Eternal ID of this version: #1028535/4
Text MD5: 826241ee5b55e95cf97760eba78ee3e6
Transpilation MD5: 818268d34c8461c854d8733bd8744f68
Author: stefan
Category: javax
Type: JavaX fragment (include)
Public (visible to everyone): Yes
Archived (hidden from active list): No
Created/modified: 2020-06-25 13:14:27
Source code size: 1760 bytes / 49 lines
Pitched / IR pitched: No / No
Views / Downloads: 228 / 313
Version history: 3 change(s)
Referenced in: [show references]