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 (3766L/23K).

1  
// potentially slow (not using levenLimited)
2  
// returns edit distance or null if no match at all
3  
static Int mmo_levenWithSwapsScore_parsedPattern(MMOPattern pattern, S s) {
4  
  S s2 = trim(s);
5  
  if (pattern == null) null;
6  
  if (startsWith(s2, "#")) null; // hashtags not handled here
7  
  if (pattern cast MMOPattern.Phrase) {
8  
    if (pattern.quoted) ret mmo_match_parsedPattern(pattern, s2) ? (Int) 0 : null;
9  
    S p = pattern.phrase;
10  
    if (startsWith(p, "#")) null; // hashtags not handled here
11  
    if (endsWith(p, "!"))
12  
      ret find3(p, s) ? 0 : null;
13  
    else
14  
      ret find3_levenWithSwapsDistance(p, s2);
15  
  }
16  
  if (pattern cast MMOPattern.And) {
17  
    int score = 0;
18  
    for (MMOPattern pat : pattern.l) {
19  
      Int score2 = mmo_levenWithSwapsScore_parsedPattern(pat, s2);
20  
      if (score2 == null) null;
21  
      score += score2;
22  
    }
23  
    ret score;
24  
  }
25  
  if (pattern cast MMOPattern.Or) {
26  
    Int min = null;
27  
    for (MMOPattern pat : pattern.l)
28  
      min = min_withNull(min, mmo_levenWithSwapsScore_parsedPattern(pat, s2));
29  
    ret min;
30  
  }
31  
  
32  
  // not clauses are not checked for typos
33  
  if (pattern instanceof MMOPattern.Not)
34  
    ret mmo_match_parsedPattern(pattern, s2) ? 0 : null;
35  
36  
  fail("what. " + pattern);
37  
}
38  
39  
static Int mmo_levenWithSwapsScore_parsedPattern(S pattern, S s) {
40  
  ret mmo_levenWithSwapsScore_parsedPattern(mmo_parsePattern(pattern), s);
41  
}

Author comment

Began life as a copy of #1026037

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: #1026502
Snippet name: mmo_levenWithSwapsScore_parsedPattern - like mmo_match_parsedPattern, but return leven difference
Eternal ID of this version: #1026502/11
Text MD5: 980384013927c866e09b13b8362b70df
Transpilation MD5: 0aa87e809e5606ba052bd4b63c0bc7fd
Author: stefan
Category: javax
Type: JavaX fragment (include)
Public (visible to everyone): Yes
Archived (hidden from active list): No
Created/modified: 2020-05-05 21:47:27
Source code size: 1404 bytes / 41 lines
Pitched / IR pitched: No / No
Views / Downloads: 228 / 357
Version history: 10 change(s)
Referenced in: [show references]