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

113
LINES

< > BotCompany Repo | #1001242 // Understander of concepts ("digit", v2)

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

Libraryless. Click here for Pure Java version (614L/5K/14K).

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

Author comment

Began life as a copy of #1000768

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: #1001242
Snippet name: Understander of concepts ("digit", v2)
Eternal ID of this version: #1001242/1
Text MD5: e04198826afb776b7786cf93122af59d
Transpilation MD5: 114272343f17341d08422067c059314b
Author: stefan
Category:
Type: JavaX source code
Public (visible to everyone): Yes
Archived (hidden from active list): No
Created/modified: 2015-10-04 17:01:14
Source code size: 2801 bytes / 113 lines
Pitched / IR pitched: No / Yes
Views / Downloads: 664 / 667
Referenced in: [show references]