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

141
LINES

< > BotCompany Repo | #1010963 // Chat Theory Module

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

Download Jar. Libraryless. Click here for Pure Java version (16833L/122K).

1  
!7
2  
3  
static Map<S, Web> theoryForLineMap;
4  
static L<S> globalIDList; // msg index to global ID
5  
sbool listen = true;
6  
static O onUpdate;
7  
8  
static L<Web> peepHoleWebs;
9  
10  
p {
11  
  if (isMain()) autoRestart();
12  
  theoryForLineMap = persistentHashMap('theoryForLineMap);
13  
  globalIDList = persistentList('globalIDList);
14  
  if (empty(globalIDList)) loadFullHistory();
15  
  if (listen) {
16  
    print("Listening to chat.");
17  
    stefansChat_onLine(100, voidfunc(S line) {
18  
      ChatMsg msg = mapToObject(stefansChat_onLine_fullParams!, ChatMsg);
19  
      handleChatMsg(msg);
20  
      pcallF(onUpdate);
21  
    });
22  
  }
23  
  bot("Chat Theory.");
24  
  
25  
  onFileChangeAndNow(1, ai_chatAnalysis_peepHoleFile(), r {
26  
    peepHoleWebs = ai_chatAnalysis_peepHoleWebs();
27  
  });
28  
}
29  
30  
svoid loadFullHistory() {
31  
  int max = 10000;
32  
  L<ChatMsg> l = stefansChat_readArchive(max);
33  
  print("Got " + l(l) + "/" + max);
34  
  for (ChatMsg msg : l) pcall { handleChatMsg(msg); }
35  
}
36  
37  
svoid handleChatMsg(ChatMsg msg) {
38  
  lock dbLock();
39  
  //print("Handling " + msg.globalID);
40  
  listPut(globalIDList, msg.nr, msg.globalID);
41  
  
42  
  Web web = theoryForLineMap.get(msg.globalID);
43  
  if (web != null) ret; // No overwrite for now
44  
  
45  
  WebNode n = simpleWeb().newNode();
46  
  n.addLabel(/*"Text " +*/ quote(msg.text));
47  
  web_addString(n, msg.botMark ? "by bot" : "by user");
48  
  web_addStrings(n, ai_categoriesForChatLine(msg.text));
49  
  web_addString(n, "Time " + msg.time);
50  
  web_addString(n, "Should I react?");
51  
  n.addLabel("Msg " + msg.globalID);
52  
  
53  
  transformers(n);
54  
  
55  
  if (web_contentsDifferent(web, n.web)) {
56  
    if (web != null) print("Overwriting " + msg.globalID);
57  
    else print("Storing " + msg.globalID);
58  
    theoryForLineMap.put(msg.globalID, n.web);
59  
  }
60  
}
61  
62  
svoid transformers(WebNode n) {
63  
  for (S text : web_texts(n)) {
64  
    for (WebNode a : indexedNodes(quote(text))) {
65  
      WebNode b = web_operand(a, "with");
66  
      if (b == null || !web_hasLabel(n, web_unquote(b))) continue;
67  
      WebNode c = web_operand(b, "transforms to");
68  
      if (c != null)
69  
        web_replaceLabel(n, text, web_unquote(c));
70  
    }
71  
  }
72  
}
73  
74  
answer {
75  
  if "show *" 
76  
    ret showWeb(theoryForLineMap.get($1)) != null ? "OK" : "Not found";
77  
  if "load full history"
78  
    ret "OK" with loadFullHistory();
79  
    
80  
  if "size" ret lstr(theoryForLineMap);
81  
}
82  
83  
static Web lastWeb() {
84  
  ret theoryForLineMap.get(last(globalIDList));
85  
}
86  
87  
static WebNode lastNode() {
88  
  ret web_firstNode(lastWeb());
89  
}
90  
91  
sS html_thoughts() {
92  
  lock dbLock();
93  
  Web web = lastWeb();
94  
  if (web == null) ret "";
95  
  WebNode node = web_firstNode(web);
96  
  L<S> texts = web_texts(node);
97  
  S html = ul_htmlencode(texts);
98  
  
99  
  //new LinkedHashSet<S> expect;
100  
  final new Map<S, Int> expect;
101  
  for (Web pw : unnull(peepHoleWebs)) {
102  
    for (WebNode a : web_allNodesNamed(pw, texts)) {
103  
      WebNode b = web_operand(a, "then");
104  
      if (b != null)
105  
        for (S s : web_texts(b))
106  
          putHigherInt(expect, s, 100);
107  
        
108  
      b = web_operand(a, "then maybe");
109  
      if (b != null) {
110  
        int percent = 50;
111  
        pcall {
112  
          S ratio = web_string(web_getRelation(a, b), 1);
113  
          percent = ratioToIntPercent(parseFirstInt(ratio), parseSecondInt(ratio));
114  
        }
115  
        for (S s : web_texts(b))
116  
          putHigherInt(expect, s, percent);
117  
      }
118  
    }
119  
  }
120  
  
121  
  expect.remove("Should I react?");
122  
  if (nempty(expect))
123  
    html += "Expecting:" + ul_htmlencode(
124  
      map(keys(expect), func(S key) {
125  
        int percent = expect.get(key);
126  
        ret key + (percent >= 100 ? "" : " (" + percent + "%)");
127  
      }));
128  
    
129  
  ret html;
130  
}
131  
132  
svoid react { pcall {
133  
  lock dbLock();
134  
  WebNode n = lastNode();
135  
  if (n == null) ret;
136  
  if (web_hasLabel(n, "I should react") && web_hasLabel(n, "greeting")) {
137  
    web_replaceLabel(n, "I should react", "I reacted");
138  
    change();
139  
    postToStefansChat("Hello Bozo");
140  
  }
141  
}}

download  show line numbers  debug dex  old transpilations   

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

No comments. add comment

Snippet ID: #1010963
Snippet name: Chat Theory Module
Eternal ID of this version: #1010963/40
Text MD5: b17c038fd944f0c27d207971cc159b75
Transpilation MD5: 82af1a3860ed83f24d8cac16fdf3aa44
Author: stefan
Category: javax / chat
Type: JavaX source code (desktop)
Public (visible to everyone): Yes
Archived (hidden from active list): No
Created/modified: 2017-10-25 20:31:29
Source code size: 3929 bytes / 141 lines
Pitched / IR pitched: No / No
Views / Downloads: 485 / 3590
Version history: 39 change(s)
Referenced in: [show references]