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).

!7

static L<S> sentences = toLinesFullTrim([[

  "0" is a digit.
  "1" is a digit.
  "2" is a digit.
  "3" is a digit.
  "4" is a digit.
  "5" is a digit.
  "6" is a digit.
  "7" is a digit.
  "8" is a digit.
  "9" is a digit.
  These are all the digits that exist.
  A number is a concatenation of digits.

]]);

static L<S> questions = toLinesFullTrim([[

  Is "5" a digit?
  Is "your mama" a digit?
  
]]);
  

static class Concept {
  S name;
  new L<S> stringExamples;
  new L<S> descriptions;
  boolean closed;
  
  *(S *name) {}
  *() {}
  
  void addStringExample(S s) {
    if (!stringExamples.contains(s)) // yes it's inefficient... who cares right now :))))))
      stringExamples.add(s);
  }
  
  void addDescription(S s) {
    if (!descriptions.contains(s))
      descriptions.add(s);
  }
}

static class Concepts {
  new Map<S, Concept> map;
  
  Concept get(S s) {
    Concept c = map.get(s);
    if (c == null) map.put(s, c = new Concept(s));
    return c;
  }
}

static new Concepts concepts;

p {
  for (S s : concatLists(sentences, questions)) {
    print("> " + s);
    print("< " + answer(s));
  }
  
  print("Number of concepts: " + concepts.map.size() + " (" + structure(concepts.map.keySet()) + ")");
  
  makeAndroid("Digit Understander Bot.");
}

static synchronized S answer(S s) {
  L<S> tok = javaTok(s);
  
  S[] bla = match("* is a *.", tok);
  if (bla != null && bla[0].startsWith("\"")) {
    print(bla[0] + " is a " + bla[1] + "!");
    concepts.get(bla[1]).addStringExample(unquote(bla[0]));
    return "ok";
  }
  
  bla = match("These are all the * that exist.", tok);
  if (bla != null) {
    S c = bla[0].replaceAll("s$", "");
    concepts.get(c).closed = true;
    return "ok, closed concept " + c + ".";
  }
  
  bla = match("A * is ... .", tok);
  if (bla != null) {
    S desc = joinRest(bla, 1);
    concepts.get(bla[0]).addDescription(desc);
    print(bla[0] + ": " + desc);
    return "ok";
  }
  
  // process questions on knowledge base
  
  bla = match("Is * a *?", tok);
  if (bla != null) {
    S answer = null /*"Dunno."*/;
    Concept c = concepts.get(bla[1]);
    if (bla[0].startsWith("\"") && c.closed) {
      answer = c.stringExamples.contains(unquote(bla[0])) ? "Yes." : "No.";
    }
    return answer;
  }
  
  bla = match("what is a *?", tok);
  if (bla != null) {
    S answer = null /*"Dunno."*/;
    Concept c = concepts.get(unquote(bla[0]));
    if (!c.descriptions.isEmpty())
      answer = "A " + c.name + " is " + c.descriptions.get(0) + ".";
    else if (c.closed)
      answer = "A " + c.name + " is one of " + c.stringExamples.size() + " globally defined strings.";
    return answer;
  }
    
  return null;
}

static S[] match(S pat, L<S> tok) {
  new Matches m;
  return match3(pat, join(tok), m) ? m.m : null;
}

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: 800 / 1491
Version history: 1 change(s)
Referenced in: #3000169 - Answer for stefanreich
#3000174 - Answer for stefanreich
#3000175 - Answer for stefanreich
#3000176 - Answer for stefanreich
#3000177 - Answer for stefanreich
#3000179 - Answer for stefanreich
#3000180 - Answer for stefanreich
#3000181 - Answer for stefanreich
#3000182 - Answer for stefanreich
#3000184 - Answer for stefanreich
#3000185 - Answer for stefanreich
#3000189 - Answer for stefanreich(>> t bla)
#3000190 - Answer for stefanreich(>> t 20 questions)
#3000202 - Answer for stefanreich (>> T conversion bot)
#3000203 - Answer for stefanreich (>> timing hello bot)
#3000208 - Answer for bgrgndz (>> list sub bots)
#3000209 - Answer for bgrgndz (>> list sub bots)
#3000238 - Answer for stefanreich (>> t power bot)
#3000369 - Answer for Stefan (>> list sub bots)
#3000376 - Answer for stefanreich (>> list sub bots)
#3000377 - Answer for stefanreich (>> timing eleu?)
#3000380 - Answer for stefanreich (>> list sub bots)
#3000381 - Answer for speedforce (>> list sub bots)
#3000382 - Answer for ferdie (>> t = 1, f = 0)
#3000383 - Answer for funkoverflow (>> t=1, f=0 okay)