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

57
LINES

< > BotCompany Repo | #1000986 // Sentences as patterns

JavaX source code [tags: use-pretranspiled] - run with: x30.jar

Libraryless. Click here for Pure Java version (272L/3K/8K).

1  
!747
2  
3  
m {
4  
  static S sentence1 = "Yes is the opposite of no. No is the opposite of yes.";
5  
  static S sentence2 = "Green is the opposite of white.";
6  
  
7  
  p {
8  
    L<S> tok1 = parse(sentence1);
9  
    L<S> tok2 = parse(sentence2);
10  
    
11  
    Map<S, S> map = makeMapPrefix(tok1, tok2);
12  
    if (map == null)
13  
      print("No match.");
14  
    else
15  
      print(structure(map));
16  
      
17  
    print(complete(sentence1, sentence2));
18  
  }
19  
  
20  
  static L<S> parse(S s) {
21  
    return tokensToLowerCase(javaTok(s));
22  
  }
23  
  
24  
  static S complete(S sentence1, S sentence2) {
25  
    L<S> tok1 = parse(sentence1);
26  
    L<S> tok2 = parse(sentence2);
27  
    Map<S, S> map = makeMapPrefix(tok1, tok2);
28  
    if (map == null) return null;
29  
    new L<S> tok;
30  
    tok.addAll(tok2.subList(0, tok2.size()-1));
31  
    for (int i = tok2.size()-1; i < tok1.size(); i++) {
32  
      S t = tok1.get(i);
33  
      S x = map.get(t);
34  
      tok.add(x != null ? x : t);
35  
    }
36  
    return join(tok);
37  
  }
38  
  
39  
  static Map<S, S> makeMapPrefix(L<S> tok1, L<S> tok2) {
40  
    if (tok1.size() < tok2.size()) return null;
41  
    
42  
    new Map<S, S> map;
43  
    for (int i = 1; i < tok2.size(); i += 2) {
44  
      S t1 = tok1.get(i), t2 = tok2.get(i);
45  
      if (!t1.equals(t2)) {
46  
        S v = map.get(t1);
47  
        if (v == null)
48  
          map.put(t1, t2);
49  
        else if (!v.equals(t2))
50  
          return null; // match fail
51  
      }
52  
    }
53  
    
54  
    // match succeeds
55  
    return map;
56  
  }
57  
}

download  show line numbers  debug dex  old transpilations   

Travelled to 15 computer(s): aoiabmzegqzx, bhatertpkbcr, cbybwowwnfue, cfunsshuasjs, gwrvuhgaqvyk, ishqpsrjomds, lpdgvwnxivlt, mqqgnosmbjvj, onxytkatvevr, pyentgdyhuwx, pzhvpgtvlbxg, teubizvjbppd, tslmcundralx, tvejysmllsmz, vouqrxazstgt

No comments. add comment

Snippet ID: #1000986
Snippet name: Sentences as patterns
Eternal ID of this version: #1000986/1
Text MD5: 001f65287dfed8a581d7ec0d23b0703d
Transpilation MD5: 565e71c9748f31197715766cee50c3ec
Author: stefan
Category: javax
Type: JavaX source code
Public (visible to everyone): Yes
Archived (hidden from active list): No
Created/modified: 2015-09-13 22:27:13
Source code size: 1448 bytes / 57 lines
Pitched / IR pitched: No / Yes
Views / Downloads: 542 / 588
Referenced in: [show references]