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

172
LINES

< > BotCompany Repo | #1000768 // Understander of concepts (v1)

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

Libraryless. Click here for Pure Java version (577L/4K/13K).

1  
!747
2  
!multi-line strings
3  
4  
m {
5  
  static class Concept {
6  
    new L<S> stringExamples;
7  
    new L<S> descriptions;
8  
    boolean closed;
9  
    
10  
    void addStringExample(S s) {
11  
      if (!stringExamples.contains(s)) // yes it's inefficient... who cares right now :))))))
12  
        stringExamples.add(s);
13  
    }
14  
    
15  
    void addDescription(S s) {
16  
      if (!descriptions.contains(s))
17  
        descriptions.add(s);
18  
    }
19  
  }
20  
  
21  
  static class Concepts {
22  
    new Map<S, Concept> map;
23  
    
24  
    Concept get(S s) {
25  
      Concept c = map.get(s);
26  
      if (c == null) map.put(s, c = new Concept());
27  
      return c;
28  
    }
29  
  }
30  
  
31  
  static new Concepts concepts;
32  
  
33  
  p {
34  
    L<S> sentences = toLinesFullTrim([[
35  
"0" is a digit.
36  
"1" is a digit.
37  
"2" is a digit.
38  
"3" is a digit.
39  
"4" is a digit.
40  
"5" is a digit.
41  
"6" is a digit.
42  
"7" is a digit.
43  
"8" is a digit.
44  
"9" is a digit.
45  
These are all the digits that exist.
46  
A number is a concatenation of digits.
47  
]]);
48  
49  
    L<S> questions = toLinesFullTrim([[
50  
Is "5" a digit?
51  
Is "your mama" a digit?
52  
]]);
53  
    
54  
    for (S s : concat(sentences, questions)) {
55  
      L<S> tok = javaTok(s);
56  
      
57  
      S[] bla = match("* is a *.", tok);
58  
      if (bla != null && bla[0].startsWith("\"")) {
59  
        print(bla[0] + " is a " + bla[1] + "!");
60  
        concepts.get(bla[1]).addStringExample(unquote(bla[0]));
61  
        continue;
62  
      }
63  
      
64  
      bla = match("These are all the * that exist.", tok);
65  
      if (bla != null) {
66  
        S c = bla[0].replaceAll("s$", "");
67  
        concepts.get(c).closed = true;
68  
        print("Closed concept " + c + ".");
69  
        continue;
70  
      }
71  
      
72  
      bla = match2("A * is ... .", tok);
73  
      if (bla != null) {
74  
        S desc = joinrest(bla, 1);
75  
        concepts.get(bla[0]).addDescription(desc);
76  
        print(bla[0] + ": " + desc);
77  
        continue;
78  
      }
79  
      
80  
      // process questions on knowledge base
81  
      
82  
      bla = match2("Is * a *?", tok);
83  
      if (bla != null) {
84  
        print("? " + s);
85  
        S answer = "Dunno.";
86  
        Concept c = concepts.get(bla[1]);
87  
        if (bla[0].startsWith("\"") && c.closed) {
88  
          answer = c.stringExamples.contains(unquote(bla[0])) ? "Yes." : "No.";
89  
        }
90  
        print("! " + answer);
91  
        continue;
92  
      }
93  
      
94  
      print("Unknown line: " + s);
95  
    }
96  
    print("Number of concepts: " + concepts.map.size() + " (" + structure(concepts.map.keySet()) + ")");
97  
  }
98  
  
99  
  static S[] match(S pattern, L<S> tok) {
100  
    L<S> pat = javaTok(pattern);
101  
    return match(pat, tok);
102  
  }
103  
  
104  
  static S[] match(L<S> pat, L<S> tok) {
105  
    new L<S> result;
106  
    if (pat.size() != tok.size()) return null;
107  
    for (int i = 1; i < pat.size(); i += 2) {
108  
      S p = pat.get(i), t = tok.get(i);
109  
      if (p == "*")
110  
        result.add(t);
111  
      else if (!p.equals(t))
112  
        return null;
113  
    }
114  
    return result.toArray(new S[result.size()]);
115  
  }
116  
  
117  
  /*
118  
  static class LL {
119  
    S head;
120  
    LL rest;
121  
  }
122  
  
123  
  static LL match2(L<S> pat, L<S> inp, int i, int j) {
124  
    boolean debug = false;
125  
    if (i+j-1 >= inp.size()) return null; // fail
126  
    for (; j < pat.size(); j += 2) {
127  
      String p = pat.get(j);
128  
      if (debug)
129  
        System.out.println("step " + p + " " + inp.get(i+j-1));
130  
      if (p == "*") {
131  
        // ok, consume
132  
      } else if (p == "." && pat.get(j+2).equals(".") && pat.get(j+4).equals(".")) {
133  
        LL bla = match2(pat, inp, i, j+6); // end ...
134  
        if (bla != null) return bla;
135  
        return match2(pat, inp, i+2, j); // continue ...
136  
      } else {
137  
        String found = inp.get(i + j - 1);
138  
        if (!found.equals(p)
139  
          return null; // fail
140  
      }
141  
    }
142  
    return i+j-2;
143  
  }*/
144  
  
145  
  static S[] match2(S pattern, L<S> tok) {
146  
    L<S> pat = javaTokPlusPeriod(pattern);
147  
    
148  
    // standard case (no ...)
149  
    int i = pat.indexOf("...");
150  
    if (i < 0) return match(pat, tok);
151  
    
152  
    pat.set(i, "*");
153  
    int expand = 0;
154  
    while (pat.size() < tok.size()) {
155  
      ++expand;
156  
      pat.add(i, "*");
157  
      pat.add(i+1, ""); // doesn't matter
158  
    }
159  
    
160  
    return match(pat, tok);
161  
  }
162  
  
163  
  static S joinrest(S[] bla, int i) {
164  
    return join(" ", Arrays.asList(bla).subList(i, bla.length));
165  
  }
166  
  
167  
  static L<S> concat(L<S> a, L<S> b) {
168  
   L<S> l = new ArrayList<S>(a);
169  
    l.addAll(b);
170  
    return l;
171  
  }
172  
}

download  show line numbers  debug dex  old transpilations   

Travelled to 16 computer(s): aoiabmzegqzx, bhatertpkbcr, cbybwowwnfue, cfunsshuasjs, gwrvuhgaqvyk, ishqpsrjomds, jtubtzbbkimh, lpdgvwnxivlt, mqqgnosmbjvj, onxytkatvevr, pyentgdyhuwx, pzhvpgtvlbxg, teubizvjbppd, tslmcundralx, tvejysmllsmz, vouqrxazstgt

No comments. add comment

Snippet ID: #1000768
Snippet name: Understander of concepts (v1)
Eternal ID of this version: #1000768/1
Text MD5: 3cd35c7270d5207005390aac56a1cfaf
Transpilation MD5: 4a53d5e5ceeae4e4c01766de0883ebaf
Author: stefan
Category:
Type: JavaX source code
Public (visible to everyone): Yes
Archived (hidden from active list): No
Created/modified: 2015-08-26 20:30:28
Source code size: 4402 bytes / 172 lines
Pitched / IR pitched: No / Yes
Views / Downloads: 658 / 634
Referenced in: [show references]