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

207
LINES

< > BotCompany Repo | #1023240 // Ontology Bot ["Is" Bot, LIVE in @Monkey]

JavaX source code (Dynamic Module) [tags: use-pretranspiled] - run with: Stefan's OS

Uses 911K of libraries. Click here for Pure Java version (20686L/115K).

1  
!7
2  
3  
concept Comment {
4  
  Concept commentingOn;
5  
  S comment;
6  
  S source;
7  
}
8  
9  
concept Is {
10  
  S globalID = aGlobalID();
11  
  S a, b; // what is what?
12  
  S mod; // 'yes, 'no, 'sometime, ...
13  
  S linkWord; // "is", "are" (or null)
14  
  S source; // where is the info from
15  
  Bool useful;
16  
  
17  
  S render() {
18  
    ret a + " " + linkPhrase() + " " + b;
19  
  }
20  
  
21  
  toString { ret "[" + globalID + "] " + render(); }
22  
  
23  
  S linkPhrase() { ret makeLinkPhrase(this); }
24  
  
25  
  void setUseful(Bool useful) { cset(this, +useful); }
26  
}
27  
28  
cmodule IsBot > DynAttractorBot {
29  
  transient SimpleCRUD<Is> crud;
30  
   
31  
  // word -> language
32  
  transient SS linkWords = reversePseudoMultiMapAsCIMap(litmap(
33  
    "english" := splitAtSpaceToCISet(/*"is are am"*/"is are"),
34  
    "german" := splitAtSpaceToCISet(/*"ist sind bin bist seid"*/"ist sind")));
35  
  
36  
  // magic sentence splitter
37  
  PairS splitIt(S s) {
38  
    LS tok = wordTok(s);
39  
    int i = -1;
40  
    if (l(tok) == 2*2+1) // two words
41  
      i = 3;
42  
    else {
43  
      L<Int> determiners = indicesOfAny(tok, combinedMechSetsCI("Determiners", "German | Determiners"), 3);
44  
      if (l(determiners) == 1)
45  
        i = last(determiners);
46  
    }
47  
    
48  
    ret i < 0 ? null : pair(joinSubList(tok, 1, i-1), joinSubList(tok, i, l(tok)-1));
49  
  }
50  
  
51  
  void storeAnswer(PairS p, S mod, S linkWord, S huh) {
52  
    if (p == null) {
53  
      // emitAnswer(huh);
54  
    } else {
55  
      Collection<S> mods = lookup(p.a, p.b);
56  
      if (containsIC(mods, mod))
57  
        emitAnswer("I know");
58  
      else {
59  
        Is entry = uniqCI_sync Is(a := p.a, b := p.b, +linkWord, +mod);
60  
        emitAnswer("Stored: " + p.a + " " + makeLinkPhrase(entry) + " " + p.b);
61  
      }
62  
    }
63  
  }
64  
  
65  
  // returns mods
66  
  Collection<S> lookup(S a, S b) {
67  
    ret collect mod(findConceptsWhereCI(Is, +a, +b));
68  
  }
69  
70  
  AttractorBot makeBot() {
71  
    ret testBot(new SingleAttractorBot {
72  
      PairS lastPair; // persistent
73  
      
74  
      void process(S s) { // MAIN ANSWER FUNCTION
75  
        new Matches m;
76  
        
77  
        s = dropTrailingPunctuation(s);
78  
        
79  
        temp ai_setLanguages("english", "german");
80  
        
81  
        /*s = ai_userSpaceToObjective(s);
82  
        print("Got objective space input: " + s);*/
83  
        
84  
        if (matchX_any(mL("Is Bot | Yes Phrases"), s))
85  
          storeAnswer(lastPair, 'yes, null, "Yes what?");
86  
        
87  
        if (matchX_any(mL("Is Bot | No Phrases"), s))
88  
          storeAnswer(lastPair, 'no, null, "No what?");
89  
        
90  
        LS tok = wordTok(s);
91  
        S linkWord = second(tok);
92  
        S language = linkWords.get(linkWord);
93  
        if (language == null) {
94  
          L<Int> linkIndices = indicesOfCodeTokensContainedInMap(tok, linkWords);
95  
          if (l(linkIndices) != 1) ret with lastPair = null;
96  
          int idx = first(linkIndices);
97  
          linkWord = tok.get(idx);
98  
          language = linkWords.get(linkWord);
99  
          PairS p = pair(joinSubList(tok, 0, idx-1), joinSubList(tok, idx+2));
100  
          
101  
          if (eqicOneOf(p.a, "what", "who", "was", "wer")) {
102  
            lastPair = null;
103  
            ret with emitAnswer(whatIs(p.b, language));
104  
          }
105  
          
106  
          if (!ai_ontologyBot_tester1(p)) ret with print("Skipping: " + p);
107  
          
108  
          lastPair = p;
109  
          storeAnswer(p, 'yes, linkWord, null);
110  
          ret;
111  
        }
112  
        bool g = eq(language, 'german);
113  
        ai_setLanguages(language);
114  
        
115  
        PairS p;
116  
        
117  
        if (flexMatchIC("is it true that * is *", s, m))
118  
          p = pair($1, $2);
119  
        else {
120  
          // tautological case
121  
          
122  
          S s2 = joinSubList(tok, 3);
123  
          if (matchDoubleRest("", s2))
124  
            ret with emitAnswer(g ? "Ja, natürlich" : "yes, obviously");
125  
            
126  
          p = splitIt(s2);
127  
          if (p == null) ret/* with emitAnswer(g
128  
            ? "Ich verstehe diesen Satz nicht"
129  
            : [[I can't parse that sentence. Please try rephrasing with "Is it true that..."]])*/;
130  
        }
131  
        
132  
        lastPair = p;
133  
        S a = p.a, b = p.b;
134  
        
135  
        Collection<S> mods = lookup(a, b);
136  
        if (nempty(mods))
137  
          emitAnswer(join(" and ", mods));
138  
        else
139  
          emitAnswer(ai_objectiveSpaceToBot(g
140  
            ? "Ich weiß nicht, ob " + a + " wohl " + b + " " + linkWord
141  
            : "I don't know if " + a + " " + linkWord + " " + b));
142  
      }
143  
    });
144  
  }
145  
  
146  
  // test for the empty bot
147  
  AttractorBot testBot(AttractorBot bot) {
148  
    testAttractorBotResponses_noFail(bot,
149  
      "is green green?", "yes, obviously",
150  
      "is a man a child?", "I don't know if a man is a child",
151  
      "is sleep pleasant", "I don't know if sleep is pleasant");
152  
    ret bot;
153  
  }
154  
  
155  
  start {
156  
    dbIndexingCI(Is, 'a, Is, 'b);
157  
    dm_localMechListCopies();
158  
    persistBot();
159  
    crud = SimpleCRUD<Is>(db_mainConcepts(), Is);
160  
  }
161  
  
162  
  afterVisualize {
163  
    addTab(tabs, "CRUD" := crud.make_dontStartBots());
164  
    addAndValidate(crud.buttons,
165  
      tableDependentButton_extraCondition(crud.table,
166  
        "Useful", r { cset(crud.selected(), useful := true) },
167  
        func -> bool { neq(crud.selected().useful, true) } ));
168  
    addAndValidate(crud.buttons,
169  
      tableDependentButton_extraCondition(crud.table,
170  
        "Not useful", r { cset(crud.selected(), useful := false) },
171  
        func -> bool { neq(crud.selected().useful, false) } ));
172  
    replaceAWTComponent(crud.buttons, func(Component c) -> JComponent {
173  
      centerAndSouthWithMargin(c,
174  
        jrightalignedlabel("Note: 'Useful' means grammatically correct and context-free, not necessarily 'true statement'"))
175  
    });
176  
  }
177  
  
178  
  S whatIs(S thing, S language) {
179  
    L<Is> l = conceptsWhereIC(Is, a := thing);
180  
    L<Is> l2 = conceptsWhereIC(Is, b := thing);
181  
    if (empty(l) && empty(l2)) ret isGerman(language) ? "I don't know" : "Keine Ahnung";
182  
    if (randomlyChooseFirstList(l, l2))
183  
      ret random(l).b;
184  
    else {
185  
      Is entry = random(l2);
186  
      ret entry.a + " " + makeLinkPhrase(entry) + " " + thing;
187  
    }
188  
  }
189  
  
190  
  // API
191  
  
192  
  L<Is> concepts() { ret list(Is); }
193  
  
194  
  L<Is> searchForA(S entity) { ret conceptsWhereIC(Is, a := entity); }
195  
  L<Is> searchForB(S entity) { ret conceptsWhereIC(Is, b := entity); }
196  
  L<Is> searchForAny(S entity) { ret mergeConceptLists(searchForA(entity), searchForB(entity)); }
197  
  
198  
  L<Is> unclassifiedConcepts() { ret conceptsWhere(Is, useful := null); }
199  
  L<Is> conceptsWithUsefulness(Bool usefulness) { ret conceptsWhere(Is, useful := usefulness); }
200  
}
201  
202  
sS makeLinkPhrase(Is entry) {
203  
  S linkWord = or2(entry.linkWord, "is"), mod = entry.mod;
204  
  if (eqic(mod, 'yes)) ret linkWord;
205  
  if (eqic(mod, 'no)) ret linkWord + " not";
206  
  ret linkWord + " " + mod;
207  
}

Author comment

Began life as a copy of #1023179

download  show line numbers  debug dex  old transpilations   

Travelled to 7 computer(s): bhatertpkbcr, cfunsshuasjs, mqqgnosmbjvj, pyentgdyhuwx, pzhvpgtvlbxg, tvejysmllsmz, vouqrxazstgt

No comments. add comment

Snippet ID: #1023240
Snippet name: Ontology Bot ["Is" Bot, LIVE in @Monkey]
Eternal ID of this version: #1023240/123
Text MD5: c2d837ef4cf301d0fa90ab5392fd1663
Transpilation MD5: 1842ae2b3602e41520c1722af5628006
Author: stefan
Category: javax / a.i.
Type: JavaX source code (Dynamic Module)
Public (visible to everyone): Yes
Archived (hidden from active list): No
Created/modified: 2019-06-26 01:32:47
Source code size: 6777 bytes / 207 lines
Pitched / IR pitched: No / No
Views / Downloads: 473 / 10511
Version history: 122 change(s)
Referenced in: [show references]