Download Jar. Uses 3874K of libraries. Click here for Pure Java version (10778L/75K).
1 | !7 |
2 | |
3 | static int perPage = 8; |
4 | static L<S> guessPrograms = ll(#1008721, #1008696, #1008704, #1008723); |
5 | |
6 | static SingleComponentPanel scp; |
7 | static JTextArea detailTextArea; |
8 | static int startIndex; |
9 | static new L guessModules; |
10 | static ReliableSingleThread relearnThread = new(f relearn); |
11 | static new TreeSet<S> keys; |
12 | |
13 | concept Sentence {
|
14 | S text; |
15 | new SS data; |
16 | |
17 | S getPart(IntRange r) { ret substring(text, r); }
|
18 | } |
19 | |
20 | p-substance {
|
21 | unswing {
|
22 | db(); |
23 | indexConceptField(Sentence, "text"); |
24 | gatherKeys(); |
25 | L<S> sentences = toLinesFullTrim(loadSnippet(#1008606)); |
26 | sentences = notStartingWith("\"", sentences);
|
27 | time { makeUniqueConcepts(Sentence, "text", sentences); }
|
28 | for (S id : guessPrograms) |
29 | pcall { guessModules.add(runDependent(id)); }
|
30 | } |
31 | JSplitPane split; |
32 | showFrame(centerAndSouth( |
33 | split = jvsplit( |
34 | scp = new SingleComponentPanel, |
35 | detailTextArea = jtypewritertextarea()), |
36 | withTopMargin(20, jcenteredline( |
37 | textActionButton("Mark as...", f mark),
|
38 | jbutton("Prev", r { makePanel(max(startIndex-perPage, 0)) }),
|
39 | jbutton("Next", f nextPage),
|
40 | jbutton("Random", r { makePanel(random(countConcepts(Sentence))/perPage*perPage) }),
|
41 | jbutton("All OK", r { clickButtons(findButtons(scp, "OK")); nextPage(); }),
|
42 | jbutton("Add sentence...", f addSentence),
|
43 | //jbutton("Re-learn", r { relearnThread.trigger() })
|
44 | )))); |
45 | addMenu(scp, "Actions", |
46 | "Rename key...", f renameKey, |
47 | "Load sentences from web bot...", f fromWebBot, |
48 | "Load text2tag snippet...", f loadText2Tag, |
49 | "Apply splittings", f applySplittings); |
50 | setFrameSize(scp, 1000, 600); |
51 | setSplitPaneLater(split, 0.8); // try it again |
52 | centerFrame(scp); |
53 | makePanel(0); |
54 | } |
55 | |
56 | svoid makePanel(int startIndex) {
|
57 | main.startIndex = startIndex; |
58 | makePanel(); |
59 | } |
60 | |
61 | svoid makePanel() {
|
62 | JPanel panel = gridLayoutPanel_trackWidth_evenlySpacedCols(2); |
63 | panel.add(jlabel("Sentence"));
|
64 | panel.add(jlabel("Analysis"));
|
65 | for (final Sentence s : takeFirst(perPage, dropFirst(startIndex, orderByFieldDesc("created", list(Sentence))))) {
|
66 | final JTextField tfText = jtextfield_caretLeft(s.text); |
67 | tfText.setCaret(new DefaultCaret {
|
68 | @Override |
69 | public void setSelectionVisible(boolean visible) {
|
70 | super.setSelectionVisible(true); |
71 | } |
72 | }); |
73 | panel.add(tfText); |
74 | |
75 | final JTextField tf = jtextfield_caretLeft(dataMapToString(s.data)); |
76 | onFocus(tf, r { updateDetail(tf) });
|
77 | final JTextField tfGuess = jtextfield_caretLeft(dataMapToString(makeGuess(s.text))); |
78 | onFocus(tfGuess, r { updateDetail(tfGuess) });
|
79 | |
80 | final new Var<JButton> reviseBtn, okBtn; |
81 | okBtn.set(jbutton("OK", r {
|
82 | pcall-messagebox {
|
83 | cset(s, data := dataMapFromString(getTextTrim(tf))); |
84 | disableTextField(tf); |
85 | replaceComponent(okBtn!, reviseBtn!); |
86 | relearnThread.trigger(); |
87 | } |
88 | })); |
89 | reviseBtn.set(jbutton("Revise", r {
|
90 | cset(s, data := new HashMap); |
91 | enableTextField(tf); |
92 | replaceComponent(reviseBtn!, okBtn!); |
93 | })); |
94 | onUpdateAndNow(tf, r { okBtn->setEnabled(nempty(getTextTrim(tf))) });
|
95 | |
96 | bool hasData = nempty(s.data); |
97 | if (hasData) disableTextField(tf); |
98 | panel.add(vstack( |
99 | centerAndEast(withLabel("Saved:", tf), hasData ? reviseBtn! : okBtn!),
|
100 | centerAndEast(withLabel("Guess:", tfGuess), jbutton("OK", r {
|
101 | pcall-messagebox {
|
102 | s.data.putAll(dataMapFromString(getTextTrim(tfGuess))); |
103 | s.change(); |
104 | disableTextField(tf); |
105 | replaceComponent(okBtn!, reviseBtn!); |
106 | relearnThread.trigger(); |
107 | } |
108 | })), |
109 | )); |
110 | } |
111 | scp.setComponent(jscroll(panel)); |
112 | } |
113 | |
114 | svoid relearn {
|
115 | for (O guessModule : guessModules) pcall { callMain(guessModule); }
|
116 | swing { makePanel(); }
|
117 | } |
118 | |
119 | svoid nextPage {
|
120 | if (startIndex+perPage < countConcepts(Sentence)) makePanel(startIndex+perPage); |
121 | } |
122 | |
123 | static S dataMapToString(SS data) {
|
124 | ret dataMapToString(data, ". "); |
125 | } |
126 | |
127 | static S dataMapToString(SS data, S glue) {
|
128 | ret join(glue, mapMapToList(withoutNulls(data), func(S a, S b) {
|
129 | a + "=" + b })); |
130 | } |
131 | |
132 | static SS dataMapFromString(S input) {
|
133 | new SS data; |
134 | for (S s : trimAll(splitByJavaToken(input, "."))) {
|
135 | L<S> tok = javaTok(s); |
136 | int i = tok.indexOf("=");
|
137 | if (i < 0) continue; |
138 | data.put(trimJoinSubList(tok, 0, i), trimJoinSubList(tok, i+1)); |
139 | } |
140 | ret data; |
141 | } |
142 | |
143 | static SS makeGuess(S sentence) {
|
144 | new SS data; |
145 | for (O guessModule : guessModules) |
146 | if (hasMethodNamed(guessModule, "guess")) |
147 | pcall(guessModule, "guess", sentence, data); // new way |
148 | else |
149 | pcall(guessModule, "callGuesser", get(guessModule, "best"), sentence, data); |
150 | ret data; |
151 | } |
152 | |
153 | svoid mark(JTextComponent c) {
|
154 | final IntRange r = textComponentSelection(c); |
155 | fS selection = c.getSelectedText(); |
156 | fS text = c.getText(); |
157 | //final JTextField tf = jtextfield("verb");
|
158 | final AutoComboBox cb = autoComboBox(keys); |
159 | showFormTitled("Mark sentence part",
|
160 | "Text", selection + " (" + r.start + "/" + r.length() + ")",
|
161 | "Mark as", cb, |
162 | func {
|
163 | pcall-infobox {
|
164 | S as = getTextTrim(cb); |
165 | Sentence s = findConcept(Sentence, +text); |
166 | s.data.put(as, ai_renderAction(text, r)); |
167 | s.change(); |
168 | gatherKeys(s); |
169 | relearnThread.trigger(); |
170 | null; |
171 | } |
172 | false; |
173 | }); |
174 | } |
175 | |
176 | svoid addSentence {
|
177 | final JTextField tf = jtextfield(); |
178 | showFormTitled("Add sentence", "Sentence", tf, r {
|
179 | uniq(Sentence, text := getTextTrim(tf)); |
180 | makePanel(0); |
181 | }); |
182 | } |
183 | |
184 | svoid renameKey {
|
185 | final AutoComboBox tf1 = autoComboBox(keys); |
186 | final JTextField tf2 = jtextfield(); |
187 | showFormTitled("Rename key",
|
188 | "Old key name", tf1, |
189 | "New key name", tf2, |
190 | r {
|
191 | S oldKey = getTextTrim(tf1); |
192 | S newKey = getTextTrim(tf2); |
193 | int n = 0; |
194 | for (Sentence s) {
|
195 | S value = s.data.get(oldKey); |
196 | if (value != null) {
|
197 | s.data.remove(oldKey); |
198 | s.data.put(newKey, value); |
199 | s.change(); |
200 | ++n; |
201 | } |
202 | } |
203 | infoBox(n(n, "changes") + " made"); |
204 | keys.clear(); gatherKeys(); |
205 | makePanel(); |
206 | }); |
207 | } |
208 | |
209 | svoid gatherKeys {
|
210 | keys.clear(); |
211 | for (Sentence s) gatherKeys(s); |
212 | } |
213 | |
214 | svoid gatherKeys(Sentence s) {
|
215 | keys.addAll(keys(s.data)); |
216 | } |
217 | |
218 | svoid fromWebBot {
|
219 | final JTextField tf = jtextfield(#1008316); |
220 | showFormTitled("Load sentences from web bot", "Chat Bot ID", tf, r {
|
221 | Collection<S> sentences = keys(getSentencesFromWebBotLog(getTextTrim(tf))); |
222 | makeUniqueConcepts(Sentence, "text", sentences); |
223 | makePanel(0); |
224 | }); |
225 | } |
226 | |
227 | svoid loadText2Tag {
|
228 | final JTextField tf = jtextfield(#1008363); |
229 | showFormTitled("Load text 2 tag snippet", "Snippet ID", tf, r {
|
230 | Pair<SS> p = tok_dropAsterisks_pair(parseText2Tag(#1008363)); |
231 | Collection<S> sentences = concatLists(keys(p.a), keys(p.b)); |
232 | makeUniqueConcepts(Sentence, "text", sentences); |
233 | for (S tag : values(p.a)) {
|
234 | Sentence s = uniq(Sentence, text := tag); |
235 | s.data.put("isTag", "t");
|
236 | s.change(); |
237 | } |
238 | makePanel(0); |
239 | }); |
240 | } |
241 | |
242 | svoid applySplittings {
|
243 | bool change = false; |
244 | for (Sentence s) {
|
245 | IntRange firstPart = ai_parseAction(s.data.get("firstPart"));
|
246 | if (firstPart != null) {
|
247 | if (makeUniqueConcepts(Sentence, "text", |
248 | ll(trim(s.getPart(firstPart)), trim(substring(s.text, firstPart.end)))) |
249 | != 0) |
250 | change = true; |
251 | } |
252 | } |
253 | if (change) makePanel(0); |
254 | } |
255 | |
256 | svoid updateDetail(JTextField tf) {
|
257 | pcall-short {
|
258 | SS data = dataMapFromString(getTextTrim(tf)); |
259 | detailTextArea.setText(dataMapToString(data, "\n")); |
260 | } |
261 | } |
Began life as a copy of #1008674
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: | #1008692 |
| Snippet name: | Sentence analysis with map [WORKS] |
| Eternal ID of this version: | #1008692/59 |
| Text MD5: | 69465da2a556da865a972f6bb09f655f |
| Transpilation MD5: | 27cfbabedcb5bd15765c9855f30f445a |
| Author: | stefan |
| Category: | javax / a.i. |
| Type: | JavaX source code (desktop) |
| Public (visible to everyone): | Yes |
| Archived (hidden from active list): | No |
| Created/modified: | 2017-06-01 17:05:15 |
| Source code size: | 7790 bytes / 261 lines |
| Pitched / IR pitched: | No / No |
| Views / Downloads: | 1058 / 2705 |
| Version history: | 58 change(s) |
| Referenced in: | [show references] |