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

63
LINES

< > BotCompany Repo | #1001511 // Synonyms

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

Libraryless. Click here for Pure Java version (672L/5K/16K).

1  
!747
2  
!multi-line strings
3  
4  
m {
5  
  !include #1001509 // rules & statements
6  
  
7  
  static Map<S, L<S>> synMap = new TreeMap<S, L<S>>();
8  
  
9  
  p {
10  
    statements.add("one is a synonym of 1.");
11  
    statements.add("\"eins\" is a synonym of 1.");
12  
    statements.add("two is a synonym of 2.");
13  
    
14  
    makeSynMap();
15  
    
16  
    ask([[what are the synonyms of 1?]]);
17  
    ask([[what are the synonyms of 2?]]);
18  
  }
19  
  
20  
  static void makeSynMap() {
21  
    for (Map<S, S> m : matchStatements("$1 is a synonym of $2"))
22  
      addSynonym(unquote(m.get("$1")), unquote(m.get("$2")));
23  
  }
24  
  
25  
  static void addSynonym(S a, S b) {
26  
    L<S> la = synMap.get(a), lb = synMap.get(b);
27  
    if (la == null && lb == null) {
28  
      L<S> l = litlist(a, b);
29  
      synMap.put(a, l);
30  
      synMap.put(b, l);
31  
    } else if (la == lb) {
32  
      // nothing to do, they're already synonyms
33  
    } else if (lb == null) {
34  
      la.add(b);
35  
      synMap.put(b, la);
36  
    } else if (la == null) {
37  
      lb.add(a);
38  
      synMap.put(a, lb);
39  
    } else
40  
      mergeLists(la, lb);
41  
  }
42  
  
43  
  static void mergeLists(L<S> la, L<S> lb) {
44  
    for (S b : lb) {
45  
      la.add(b);
46  
      synMap.put(b, la);
47  
    }
48  
  }
49  
  
50  
  static void ask(S q) {
51  
    print("? " + q);
52  
    
53  
    new Map<S, S> map;
54  
    if (match4("what are the synonyms of $1?", q, map))
55  
      print("  => " + structure(getSynonyms(map.get("$1"))));
56  
    else
57  
      print("dunno.");
58  
  }
59  
  
60  
  static L<S> getSynonyms(S x) {
61  
    ret synMap.get(x);
62  
  }
63  
}

Author comment

Began life as a copy of #1001510

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: #1001511
Snippet name: Synonyms
Eternal ID of this version: #1001511/1
Text MD5: b29e91e132d84cff796f507f94ca5efa
Transpilation MD5: 206380f267bc3c70c077ddd68cc91e54
Author: stefan
Category:
Type: JavaX source code
Public (visible to everyone): Yes
Archived (hidden from active list): No
Created/modified: 2015-10-21 16:31:35
Source code size: 1501 bytes / 63 lines
Pitched / IR pitched: No / Yes
Views / Downloads: 490 / 525
Referenced in: [show references]