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

122
LINES

< > BotCompany Repo | #1001508 // Singular & plural (v3)

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

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

1  
!747
2  
!multi-line strings
3  
4  
m {
5  
  static new L<S> rules;
6  
  static new L<S> statements;
7  
  
8  
  p {
9  
    rules.add([["$1 is the singular of $2." <=> "$2 is the plural of $1."]]);
10  
    statements.add("house is the singular of houses.");
11  
    process();
12  
    print(fromLines(statements));
13  
14  
    ask([[how many plurals does "house" have?]]);
15  
    ask([[what are the plurals of "house"?]]);
16  
  }
17  
  
18  
  static void ask(S q) {
19  
    print("? " + q);
20  
    
21  
    new Map<S, S> map;
22  
    if (match4("how many plurals does $1 have?", q, map))
23  
      print(getPlurals(map.get("$1")).size());
24  
    else if (match4("what are the plurals of $1?", q, map))
25  
      print(structure(getPlurals(map.get("$1"))));
26  
    else
27  
      print("dunno.");
28  
  }
29  
  
30  
  static L<S> getPlurals(S x) {
31  
    S c = "$2 is the plural of " + x;
32  
    print("[" + c + "]");
33  
    MultiMap<S, S> matches = matchAll(c);
34  
    ret matches.get("$2");
35  
  }
36  
37  
  // result of matchStatements ordered by variable
38  
  static MultiMap<S, S> matchAll(S pat) {
39  
    new MultiMap<S, S> map;
40  
    L<Map<S, S>> l = matchStatements(pat);
41  
    print("[" + l.size() + " matches for " + quote(pat) + "]");
42  
    for (Map<S, S> m : l) {
43  
      for (Map.Entry<S, S> e : m.entrySet())
44  
        map.put(e.getKey(), e.getValue());
45  
    }
46  
    ret map;
47  
  }
48  
  
49  
  static void process() {
50  
    for (S rule : rules) {
51  
      new Matches m;
52  
      if (match3("* <=> *", rule, m)) {
53  
        S l = m.unq(0), r = m.unq(1);
54  
        for (Map<S, S> map : matchStatements(l))
55  
          addStatement(convert(r, map));
56  
      }
57  
    }
58  
  }
59  
  
60  
  static L<Map<S, S>> matchStatements(S pat) {
61  
    L<Map<S, S>> l = new ArrayList<Map<S, S>>();
62  
    for (S s : statements) {
63  
      Map<S, S> m = match4(pat, s);
64  
      if (m != null) l.add(m);
65  
    }
66  
    ret l;
67  
  }
68  
  
69  
  static Map<S, S> match4(S pat, S s) {
70  
    new Map<S, S> m;
71  
    ret match4(pat, s, m) ? m : null;
72  
  }
73  
  
74  
  static boolean match4(S pat, S s, Map<S, S> m) {
75  
    L<S> tok1 = parse3(pat), tok2 = parse3(s);
76  
    if (tok1.size() != tok2.size()) ret false;
77  
    m.clear();
78  
    for (int i = 1; i < tok1.size(); i += 2) {
79  
      S t1 = tok1.get(i), t2 = tok2.get(i);
80  
      if (isVar(t1))
81  
        m.put(t1, t2);
82  
      else if (!tokEq(t1, t2))
83  
        ret false;
84  
    }
85  
    ret true;
86  
  }
87  
  
88  
  static boolean tokEq(S a, S b) {
89  
    a = unquote(a);
90  
    b = unquote(b);
91  
    ret a.equalsIgnoreCase(b);
92  
  }
93  
  
94  
  static boolean isVar(S s) {
95  
    ret s.startsWith("$") && s.length() > 1;
96  
  }
97  
  
98  
  static S convert(S r, Map<S, S> matches) {
99  
    L<S> tok = javaTokPlusPeriod(r);
100  
    for (int i = 1; i < tok.size(); i += 2) {
101  
      if (tok.get(i).startsWith("$")) {
102  
        S m = matches.get(tok.get(i));
103  
        if (m != null)
104  
          tok.set(i, m);
105  
      }
106  
    }
107  
    ret join(tok);
108  
  }
109  
  
110  
  static void addStatement(S s) {
111  
    for (S st : statements)
112  
      if (match3(st, s))
113  
        ret;
114  
    statements.add(s);
115  
  }
116  
  
117  
  !include #2000515 // unquote
118  
  !include #1001497 // parseBoolean
119  
  !include #1000709 // formatSnippetID
120  
  
121  
  !include #1001296 // MultiMap
122  
}

Author comment

Began life as a copy of #1001507

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: #1001508
Snippet name: Singular & plural (v3)
Eternal ID of this version: #1001508/1
Text MD5: 1d8282b832873de5caae54bfa9a0ea20
Transpilation MD5: 76f59006902a61d828bc4a13907e0dd5
Author: stefan
Category:
Type: JavaX source code
Public (visible to everyone): Yes
Archived (hidden from active list): No
Created/modified: 2015-10-21 16:08:29
Source code size: 3091 bytes / 122 lines
Pitched / IR pitched: No / Yes
Views / Downloads: 580 / 615
Referenced in: [show references]