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

!747
!multi-line strings

m {
  !include #1001509 // rules & statements
  
  static Map<S, L<S>> synMap = new TreeMap<S, L<S>>();
  
  p {
    statements.add("one is a synonym of 1.");
    statements.add("\"eins\" is a synonym of 1.");
    statements.add("two is a synonym of 2.");
    
    makeSynMap();
    
    ask([[what are the synonyms of 1?]]);
    ask([[what are the synonyms of 2?]]);
  }
  
  static void makeSynMap() {
    for (Map<S, S> m : matchStatements("$1 is a synonym of $2"))
      addSynonym(unquote(m.get("$1")), unquote(m.get("$2")));
  }
  
  static void addSynonym(S a, S b) {
    L<S> la = synMap.get(a), lb = synMap.get(b);
    if (la == null && lb == null) {
      L<S> l = litlist(a, b);
      synMap.put(a, l);
      synMap.put(b, l);
    } else if (la == lb) {
      // nothing to do, they're already synonyms
    } else if (lb == null) {
      la.add(b);
      synMap.put(b, la);
    } else if (la == null) {
      lb.add(a);
      synMap.put(a, lb);
    } else
      mergeLists(la, lb);
  }
  
  static void mergeLists(L<S> la, L<S> lb) {
    for (S b : lb) {
      la.add(b);
      synMap.put(b, la);
    }
  }
  
  static void ask(S q) {
    print("? " + q);
    
    new Map<S, S> map;
    if (match4("what are the synonyms of $1?", q, map))
      print("  => " + structure(getSynonyms(map.get("$1"))));
    else
      print("dunno.");
  }
  
  static L<S> getSynonyms(S x) {
    ret synMap.get(x);
  }
}

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: 552 / 613
Referenced in: [show references]