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).

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  
      if (!find3(p, s)) 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   

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: 30 / 75
Version history: 9 change(s)
Referenced in: [show references]

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