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

125
LINES

< > BotCompany Repo | #1001244 // Digit Understander Bot

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

Libraryless. Click here for Pure Java version (3546L/25K/79K).

1  
!7
2  
3  
static L<S> sentences = toLinesFullTrim([[
4  
5  
  "0" is a digit.
6  
  "1" is a digit.
7  
  "2" is a digit.
8  
  "3" is a digit.
9  
  "4" is a digit.
10  
  "5" is a digit.
11  
  "6" is a digit.
12  
  "7" is a digit.
13  
  "8" is a digit.
14  
  "9" is a digit.
15  
  These are all the digits that exist.
16  
  A number is a concatenation of digits.
17  
18  
]]);
19  
20  
static L<S> questions = toLinesFullTrim([[
21  
22  
  Is "5" a digit?
23  
  Is "your mama" a digit?
24  
  
25  
]]);
26  
  
27  
28  
static class Concept {
29  
  S name;
30  
  new L<S> stringExamples;
31  
  new L<S> descriptions;
32  
  boolean closed;
33  
  
34  
  *(S *name) {}
35  
  *() {}
36  
  
37  
  void addStringExample(S s) {
38  
    if (!stringExamples.contains(s)) // yes it's inefficient... who cares right now :))))))
39  
      stringExamples.add(s);
40  
  }
41  
  
42  
  void addDescription(S s) {
43  
    if (!descriptions.contains(s))
44  
      descriptions.add(s);
45  
  }
46  
}
47  
48  
static class Concepts {
49  
  new Map<S, Concept> map;
50  
  
51  
  Concept get(S s) {
52  
    Concept c = map.get(s);
53  
    if (c == null) map.put(s, c = new Concept(s));
54  
    return c;
55  
  }
56  
}
57  
58  
static new Concepts concepts;
59  
60  
p {
61  
  for (S s : concatLists(sentences, questions)) {
62  
    print("> " + s);
63  
    print("< " + answer(s));
64  
  }
65  
  
66  
  print("Number of concepts: " + concepts.map.size() + " (" + structure(concepts.map.keySet()) + ")");
67  
  
68  
  makeAndroid("Digit Understander Bot.");
69  
}
70  
71  
static synchronized S answer(S s) {
72  
  L<S> tok = javaTok(s);
73  
  
74  
  S[] bla = match("* is a *.", tok);
75  
  if (bla != null && bla[0].startsWith("\"")) {
76  
    print(bla[0] + " is a " + bla[1] + "!");
77  
    concepts.get(bla[1]).addStringExample(unquote(bla[0]));
78  
    return "ok";
79  
  }
80  
  
81  
  bla = match("These are all the * that exist.", tok);
82  
  if (bla != null) {
83  
    S c = bla[0].replaceAll("s$", "");
84  
    concepts.get(c).closed = true;
85  
    return "ok, closed concept " + c + ".";
86  
  }
87  
  
88  
  bla = match("A * is ... .", tok);
89  
  if (bla != null) {
90  
    S desc = joinRest(bla, 1);
91  
    concepts.get(bla[0]).addDescription(desc);
92  
    print(bla[0] + ": " + desc);
93  
    return "ok";
94  
  }
95  
  
96  
  // process questions on knowledge base
97  
  
98  
  bla = match("Is * a *?", tok);
99  
  if (bla != null) {
100  
    S answer = null /*"Dunno."*/;
101  
    Concept c = concepts.get(bla[1]);
102  
    if (bla[0].startsWith("\"") && c.closed) {
103  
      answer = c.stringExamples.contains(unquote(bla[0])) ? "Yes." : "No.";
104  
    }
105  
    return answer;
106  
  }
107  
  
108  
  bla = match("what is a *?", tok);
109  
  if (bla != null) {
110  
    S answer = null /*"Dunno."*/;
111  
    Concept c = concepts.get(unquote(bla[0]));
112  
    if (!c.descriptions.isEmpty())
113  
      answer = "A " + c.name + " is " + c.descriptions.get(0) + ".";
114  
    else if (c.closed)
115  
      answer = "A " + c.name + " is one of " + c.stringExamples.size() + " globally defined strings.";
116  
    return answer;
117  
  }
118  
    
119  
  return null;
120  
}
121  
122  
static S[] match(S pat, L<S> tok) {
123  
  new Matches m;
124  
  return match3(pat, join(tok), m) ? m.m : null;
125  
}

Author comment

Began life as a copy of #1001242

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: #1001244
Snippet name: Digit Understander Bot
Eternal ID of this version: #1001244/2
Text MD5: d30028ba1f60c7874f451fa323d1674f
Transpilation MD5: 8a42324befcec1e57e4fbd62f1bf6c42
Author: stefan
Category:
Type: JavaX source code
Public (visible to everyone): Yes
Archived (hidden from active list): No
Created/modified: 2017-08-07 16:02:39
Source code size: 2911 bytes / 125 lines
Pitched / IR pitched: No / No
Views / Downloads: 801 / 1494
Version history: 1 change(s)
Referenced in: [show references]