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

!747
!multi-line strings

m {
  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 {
    new L<S> stringExamples;
    new L<S> descriptions;
    boolean closed;
    
    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());
      return c;
    }
  }
  
  static new Concepts concepts;
  
  p {
    for (S s : concatLists(sentences, questions)) {
      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]));
        continue;
      }
      
      bla = match("These are all the * that exist.", tok);
      if (bla != null) {
        S c = bla[0].replaceAll("s$", "");
        concepts.get(c).closed = true;
        print("Closed concept " + c + ".");
        continue;
      }
      
      bla = match("A * is ... .", tok);
      if (bla != null) {
        S desc = joinRest(bla, 1);
        concepts.get(bla[0]).addDescription(desc);
        print(bla[0] + ": " + desc);
        continue;
      }
      
      // process questions on knowledge base
      
      bla = match("Is * a *?", tok);
      if (bla != null) {
        print("? " + s);
        S answer = "Dunno.";
        Concept c = concepts.get(bla[1]);
        if (bla[0].startsWith("\"") && c.closed) {
          answer = c.stringExamples.contains(unquote(bla[0])) ? "Yes." : "No.";
        }
        print("! " + answer);
        continue;
      }
      
      print("Unknown line: " + s);
    }
    print("Number of concepts: " + concepts.map.size() + " (" + structure(concepts.map.keySet()) + ")");
  }
  
  static S joinRest(S[] bla, int i) {
    return join(" ", Arrays.asList(bla).subList(i, bla.length));
  }
  
  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 #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: 663 / 666
Referenced in: #1001244 - Digit Understander Bot
#3000189 - Answer for stefanreich(>> t bla)
#3000190 - Answer for stefanreich(>> t 20 questions)
#3000382 - Answer for ferdie (>> t = 1, f = 0)
#3000383 - Answer for funkoverflow (>> t=1, f=0 okay)