Libraryless. Click here for Pure Java version (563L/4K/14K).
!747 m { !include #1001196 // Lisp static new L<Lisp> rules; static new L<S> statements; p { rules.add(equ("$1 is the singular of $2.", "$2 is the plural of $1.")); statements.add("house is the singular of houses."); process(); print(fromLines(statements)); } static void process() { for (Lisp rule : rules) { if (!rule.is("equ")) continue; S l = rule.getString(0), r = rule.getString(1); for (Map<S, S> m : matchStatements(l)) { addStatement(convert(r, m)); } } } static L<Map<S, S>> matchStatements(S pat) { L<Map<S, S>> l = new ArrayList<Map<S, S>>(); for (S s : statements) { Map<S, S> m = match4(pat, s); if (m != null) l.add(m); } ret l; } static Map<S, S> match4(S pat, S s) { L<S> tok1 = parse3(pat), tok2 = parse3(s); if (tok1.size() != tok2.size()) ret null; new Map<S, S> m; for (int i = 1; i < tok1.size(); i += 2) { S t1 = tok1.get(i), t2 = tok2.get(i); if (isVar(t1)) m.put(t1, t2); else if (!t1.equalsIgnoreCase(t2)) ret null; } ret m; } static boolean isVar(S s) { ret s.startsWith("$") && s.length() > 1; } static S convert(S r, Map<S, S> matches) { L<S> tok = javaTokPlusPeriod(r); for (int i = 1; i < tok.size(); i += 2) { if (tok.get(i).startsWith("$")) { S m = matches.get(tok.get(i)); if (m != null) tok.set(i, m); } } ret join(tok); } static void addStatement(S s) { for (S st : statements) if (match3(st, s)) ret; statements.add(s); } static Lisp equ(O... args) { ret lisp("equ", args); } !include #2000515 // unquote !include #1001497 // parseBoolean !include #1000709 // formatSnippetID }
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: | 604 / 629 |
Referenced in: | [show references] |