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

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

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: 160 / 230
Version history: 3 change(s)
Referenced in: [show references]