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

41
LINES

< > BotCompany Repo | #1026502 - mmo_levenWithSwapsScore_parsedPattern - like mmo_match_parsedPattern, but return leven difference

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

Libraryless. Click here for Pure Java version (3605L/22K).

// potentially slow (not using levenLimited)
// returns edit distance or null if no match at all
static Int mmo_levenWithSwapsScore_parsedPattern(MMOPattern pattern, S s) {
  S s2 = trim(s);
  if (pattern == null) null;
  if (startsWith(s2, "#")) null; // hashtags not handled here
  if (pattern cast MMOPattern.Phrase) {
    if (pattern.quoted) ret mmo_match_parsedPattern(pattern, s2) ? (Int) 0 : null;
    S p = pattern.phrase;
    if (startsWith(p, "#")) null; // hashtags not handled here
    if (endsWith(p, "!")) {
      if (!find3(p, s)) null;
    } else
      ret find3_levenWithSwapsDistance(p, s2);
  }
  if (pattern cast MMOPattern.And) {
    int score = 0;
    for (MMOPattern pat : pattern.l) {
      Int score2 = mmo_levenWithSwapsScore_parsedPattern(pat, s2);
      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, mmo_levenWithSwapsScore_parsedPattern(pat, s2));
    ret min;
  }
  
  // not clauses are not checked for typos
  if (pattern instanceof MMOPattern.Not)
    ret mmo_match_parsedPattern(pattern, s2) ? 0 : null;

  fail("what. " + pattern);
}

static Int mmo_levenWithSwapsScore_parsedPattern(S pattern, S s) {
  ret mmo_levenWithSwapsScore_parsedPattern(mmo_parsePattern(pattern), s);
}

Author comment

Began life as a copy of #1026037

download  show line numbers  debug dex   

Travelled to 3 computer(s): mqqgnosmbjvj, tvejysmllsmz, xrpafgyirdlv

No comments. add comment

Snippet ID: #1026502
Snippet name: mmo_levenWithSwapsScore_parsedPattern - like mmo_match_parsedPattern, but return leven difference
Eternal ID of this version: #1026502/10
Text MD5: 40c71f93b2590ef48914c973fe78fe9b
Transpilation MD5: 33487b9463b6b521f378b62150073f94
Author: stefan
Category: javax
Type: JavaX fragment (include)
Public (visible to everyone): Yes
Archived (hidden from active list): No
Created/modified: 2020-01-12 15:31:38
Source code size: 1404 bytes / 41 lines
Pitched / IR pitched: No / No
Views / Downloads: 25 / 70
Version history: 9 change(s)
Referenced in: [show references]

Formerly at http://tinybrain.de/1026502 & http://1026502.tinybrain.de