Download Jar. Uses 3874K of libraries. Click here for Pure Java version (24310L/165K).
1 | !7 |
2 | |
3 | !include once #1014750 // CSnippet |
4 | |
5 | sS inspiration = [[ |
6 | snippet titles |
7 | "[OK]" |
8 | ]]; |
9 | |
10 | static new LL<S> facts; // stores code token parses |
11 | static Map<S, O> phrasesWordTree = wordTree(); |
12 | static Clusters<S> clusters = ciClusters(); |
13 | static Prolog prolog; |
14 | |
15 | sS reasoning = [[ |
16 | // check inspiration :o) |
17 | assert(inspired "snippet titles") |
18 | assert(inspired "\"[OK]\"") |
19 | |
20 | // phrases |
21 | phrase "snippet titles" |
22 | phrase "a list of strings" |
23 | phrase "a quoted string" |
24 | |
25 | // synonyms |
26 | "the snippet titles" = "snippet titles" |
27 | |
28 | // grammar |
29 | singular_plural("snippet title", "snippet titles")
|
30 | |
31 | // test parsing |
32 | assert_parse("snippet titles are a list of strings", "snippet titles", "are", "a list of strings")
|
33 | |
34 | // facts |
35 | snippet titles are a list of strings |
36 | "[OK]" is a quoted string |
37 | |
38 | // rule to answer a question |
39 | fact_question_answer_upperCaseVars("X are Y", "what are X?", "Y")
|
40 | |
41 | // check the rule |
42 | assert_answer("what are snippet titles?", "a list of strings")
|
43 | ]]; |
44 | |
45 | p-experiment-tt { go(); }
|
46 | |
47 | svoid go {
|
48 | prolog = new Prolog; |
49 | |
50 | L<S> program = tlftj(reasoning); |
51 | pnl(program); |
52 | print(); |
53 | |
54 | //set translateUsingWordTree_debug; |
55 | //set translateUsingWordTreeC_printReplacements; |
56 | |
57 | for (S s : tlftj(inspiration)) |
58 | addFact("inspired " + quote(s));
|
59 | |
60 | for (S s : program) {
|
61 | Pair<S, L<S>> p = parseFunctionCall(s); |
62 | if (p != null) {
|
63 | //print("Function call: " + sfu(p));
|
64 | S f = p.a; |
65 | L<S> args = p.b; |
66 | if (eqic(f, "assert")) |
67 | assertFact(getSingleton(p.b)); |
68 | else if (swic(f, "assert_")) {
|
69 | if (eqic(f, "assert_answer")) |
70 | assertAnswer(unquote(first(args)), unquote(second(args))); |
71 | else if (eqic(f, "assert_parse")) |
72 | assertParse(args); |
73 | else |
74 | fail("Unknown assertion function: " + f);
|
75 | } else if (eqic(f, "fact_question_answer_upperCaseVars")) |
76 | prolog.code( |
77 | print("if [" + toProlog(unquote(first(args))) + "] and ["
|
78 | + toProlog(unquote(second(args))) + "] then [answer is [" |
79 | + toProlog(unquote(third(args))) + "]]")); |
80 | continue; |
81 | } |
82 | |
83 | L<S> tok = javaTokC(s); |
84 | if (l(tok) == 2 && isIdentifier(first(tok)) && isQuoted(second(tok))) |
85 | if (eq(first(tok), "phrase")) {
|
86 | addPhrase(unquote(second(tok))); |
87 | continue; |
88 | } |
89 | |
90 | if (l(tok) == 3 && eq(second(tok), "=")) {
|
91 | ciClusters_add(clusters, |
92 | addPhrase(unquote(first(tok))), |
93 | addPhrase(unquote(third(tok)))); |
94 | continue; |
95 | } |
96 | |
97 | addFact(s); |
98 | } |
99 | |
100 | printAsciiHeading("Facts");
|
101 | pnlStruct(facts); |
102 | print(); |
103 | |
104 | print("Clusters: " + sfu(clusters));
|
105 | print("Phrases: " + sfu(phrasesWordTree));
|
106 | //loadSnippets(); |
107 | } |
108 | |
109 | svoid loadSnippets {
|
110 | loadMainConceptsFrom_readOnly(#1009918); // get snippets |
111 | print("Have " + countConcepts(CSnippet) + " snippets");
|
112 | pnl(takeFirst10(collect(list(CSnippet), 'title)); |
113 | } |
114 | |
115 | static L<S> parseFact(S s) {
|
116 | L<S> tok = map toLowerIfIdentifier(javaTokWithAngleBracketsC(/*dropPunctuation*/(s))); |
117 | ret nempties(translateUsingWordTreeC(tok, phrasesWordTree)); |
118 | } |
119 | |
120 | svoid assertFact(S fact) {
|
121 | if (contains(facts, parseFact(fact))) |
122 | print("OK fact: " + fact);
|
123 | else |
124 | print("FAILED fact assert: " + fact);
|
125 | } |
126 | |
127 | svoid assertAnswer(S q, S a) {
|
128 | Prolog p = new(prolog); |
129 | //p.showStuff = true; |
130 | p.code(dontPrint("to prolog: ", squareBracket(toProlog(q))));
|
131 | L<Lisp> out = p.rewrite(); |
132 | //print("Got " + l(out) + " statement(s)");
|
133 | L<S> lines = map nlUnparse(out); |
134 | L<S> answers = startingWithEndingWith_drop(lines, "answer is [", "]"); |
135 | if (l(answers) == 1 && eqic(first(answers), a)) |
136 | print("OK answer: " + q + " -> " + a);
|
137 | else {
|
138 | pnl(map nlUnparse(out)); |
139 | print("FAIL answer: " + q + " -> " + first(answers) + " - expected: " + a);
|
140 | } |
141 | } |
142 | |
143 | sS addPhrase(S s) {
|
144 | wordTreeAdd(phrasesWordTree, javaTokC(s), s); |
145 | ret s; |
146 | } |
147 | |
148 | svoid addFact(S s) {
|
149 | L<S> tok = parseFact(s); |
150 | facts.add(tok); |
151 | prolog.code(dontPrint("to prolog: ", squareBracket(factToProlog(tok))));
|
152 | } |
153 | |
154 | svoid assertParse(L<S> args) {
|
155 | args = unquoteAll(args); |
156 | S s = first(args); |
157 | L<S> parsed = parseFact(s); |
158 | L<S> expected = dropFirst(args); |
159 | if (eq(parsed, expected)) |
160 | print("OK parse: " + s + " -> " + sfu(parsed));
|
161 | else |
162 | print("FAILED parse: " + s + " -> " + sfu(parsed) + " - expected: " + sfu(expected));
|
163 | } |
164 | |
165 | sS toProlog(S s) {
|
166 | L<S> tok = parseFact(upperCaseVarsToDollarVars(s)); |
167 | ret factToProlog(tok); |
168 | } |
169 | |
170 | sS factToProlog(L<S> tok) {
|
171 | ret joinWithSpace(map tokenToProlog(tok)); |
172 | } |
173 | |
174 | sS tokenToProlog(S t) {
|
175 | if (!isQuoted(t) && containsSpace(t)) ret squareBracket(t); |
176 | ret t; |
177 | } |
download show line numbers debug dex old transpilations
Travelled to 14 computer(s): aoiabmzegqzx, bhatertpkbcr, cbybwowwnfue, cfunsshuasjs, gwrvuhgaqvyk, ishqpsrjomds, lpdgvwnxivlt, mqqgnosmbjvj, onxytkatvevr, pyentgdyhuwx, pzhvpgtvlbxg, tslmcundralx, tvejysmllsmz, vouqrxazstgt
No comments. add comment
| Snippet ID: | #1014753 |
| Snippet name: | Snippet Title Reasoning v1 [OK] |
| Eternal ID of this version: | #1014753/64 |
| Text MD5: | fd884adc61ab7546331d8200c3518e11 |
| Transpilation MD5: | 2c24cc4dc2811f1de52d832e24fe3cd1 |
| Author: | stefan |
| Category: | javax / a.i. |
| Type: | JavaX source code (desktop) |
| Public (visible to everyone): | Yes |
| Archived (hidden from active list): | No |
| Created/modified: | 2018-05-01 22:26:48 |
| Source code size: | 4767 bytes / 177 lines |
| Pitched / IR pitched: | No / No |
| Views / Downloads: | 843 / 4017 |
| Version history: | 63 change(s) |
| Referenced in: | [show references] |