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

77
LINES

< > BotCompany Repo | #1001506 // Singular & plural

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

Libraryless. Click here for Pure Java version (563L/4K/14K).

1  
!747
2  
3  
m {
4  
  !include #1001196 // Lisp
5  
  
6  
  static new L<Lisp> rules;
7  
  static new L<S> statements;
8  
  
9  
  p {
10  
    rules.add(equ("$1 is the singular of $2.", "$2 is the plural of $1."));
11  
    statements.add("house is the singular of houses.");
12  
    process();
13  
    print(fromLines(statements));
14  
  }
15  
  
16  
  static void process() {
17  
    for (Lisp rule : rules) {
18  
      if (!rule.is("equ")) continue;
19  
      S l = rule.getString(0), r = rule.getString(1);
20  
      for (Map<S, S> m : matchStatements(l)) {
21  
        addStatement(convert(r, m));
22  
      }
23  
    }
24  
  }
25  
  
26  
  static L<Map<S, S>> matchStatements(S pat) {
27  
    L<Map<S, S>> l = new ArrayList<Map<S, S>>();
28  
    for (S s : statements) {
29  
      Map<S, S> m = match4(pat, s);
30  
      if (m != null) l.add(m);
31  
    }
32  
    ret l;
33  
  }
34  
  
35  
  static Map<S, S> match4(S pat, S s) {
36  
    L<S> tok1 = parse3(pat), tok2 = parse3(s);
37  
    if (tok1.size() != tok2.size()) ret null;
38  
    new Map<S, S> m;
39  
    for (int i = 1; i < tok1.size(); i += 2) {
40  
      S t1 = tok1.get(i), t2 = tok2.get(i);
41  
      if (isVar(t1))
42  
        m.put(t1, t2);
43  
      else if (!t1.equalsIgnoreCase(t2))
44  
        ret null;
45  
    }
46  
    ret m;
47  
  }
48  
  
49  
  static boolean isVar(S s) {
50  
    ret s.startsWith("$") && s.length() > 1;
51  
  }
52  
  
53  
  static S convert(S r, Map<S, S> matches) {
54  
    L<S> tok = javaTokPlusPeriod(r);
55  
    for (int i = 1; i < tok.size(); i += 2) {
56  
      if (tok.get(i).startsWith("$")) {
57  
        S m = matches.get(tok.get(i));
58  
        if (m != null)
59  
          tok.set(i, m);
60  
      }
61  
    }
62  
    ret join(tok);
63  
  }
64  
  
65  
  static void addStatement(S s) {
66  
    for (S st : statements)
67  
      if (match3(st, s))
68  
        ret;
69  
    statements.add(s);
70  
  }
71  
  
72  
  static Lisp equ(O... args) { ret lisp("equ", args); }
73  
  
74  
  !include #2000515 // unquote
75  
  !include #1001497 // parseBoolean
76  
  !include #1000709 // formatSnippetID
77  
}

Author comment

Began life as a copy of #1001505

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: #1001506
Snippet name: Singular & plural
Eternal ID of this version: #1001506/1
Text MD5: ff6b64aee0a5f78c6364042b3ff78118
Transpilation MD5: 185920f4d7635ccdec16faa41c2e1db3
Author: stefan
Category:
Type: JavaX source code
Public (visible to everyone): Yes
Archived (hidden from active list): No
Created/modified: 2015-10-21 15:46:34
Source code size: 1885 bytes / 77 lines
Pitched / IR pitched: No / Yes
Views / Downloads: 606 / 632
Referenced in: [show references]