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

121
LINES

< > BotCompany Repo | #1001195 // Lisp/NL logic engine tester (developing)

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

Libraryless. Compilation Failed (1836L/12K).

1  
!747
2  
3  
m {
4  
  static S inputID = "#1001191";
5  
  static S input;
6  
  static S engineID = "#1001192";
7  
  static Class engine; // logic engine
8  
  static int score, total;
9  
    
10  
  !include #1001196 // Lisp engine
11  
  
12  
  p {
13  
    if (input == null)
14  
      input = loadSnippet(inputID); // Images & pixels
15  
    else
16  
      inputID = "direct";
17  
    
18  
    Lisp list = lisplist();
19  
    for (S line : toLinesFullTrim(input)) {
20  
      if (line.equals("**")) {
21  
        testCase(list);
22  
        list = lisplist();
23  
      } else if (line.startsWith("!")) {
24  
        line = line.substring(1).trim();
25  
        Lisp l;
26  
        if (line.indexOf('|') >= 0) {
27  
          l = or();
28  
          for (S x : line.split("\\|"))
29  
            l.add(x.trim());
30  
        } else
31  
          l = new Lisp(line);
32  
        list.add(answer(l));
33  
      } else {
34  
        if (line.startsWith("?"))
35  
          line = line.substring(1).trim();
36  
        list.add(enter(line));
37  
      }
38  
    }
39  
    
40  
    testCase(list);
41  
    
42  
    print("Score for " + engineID + " on " + inputID + ": " + score + " / " + total + ".");
43  
  }
44  
  
45  
  static void testCase(Lisp list) {
46  
    if (list.isEmpty()) return;
47  
    
48  
    if (engine == null) {
49  
      engine = hotwire(engineID);
50  
      callOpt(engine, "init");
51  
    }
52  
 
53  
    for (int i = 0; i < list.size(); i++) {
54  
      Lisp a = list.get(i);
55  
      if (!a.is("answer")) continue;
56  
      print("Expected: " + a);
57  
      S answer = checkEngine(list.subList(0, i));
58  
      boolean ok = isAnswerOK(a.get(0), answer);
59  
      ++total;
60  
      if (ok) ++score;
61  
      print(ok ? "OK!" : "Not OK...");
62  
    }
63  
  }
64  
  
65  
  static boolean isAnswerOK(Lisp a, S answer) {
66  
    if (answer == null) return false;
67  
    if (a.is("or")) {
68  
      for (Lisp s : a)
69  
        if (sentenceEq(s.head, answer))
70  
          return true;
71  
      return false;
72  
    } else
73  
      return sentenceEq(a.head, answer);
74  
  }
75  
  
76  
  static S checkEngine(Lisp list) {
77  
    O exportedList = export(list, engine);
78  
    //print("exportedList: " + structure(exportedList));
79  
    S answer = (S) call(engine, "chimeIn", exportedList);
80  
    print("Engine said: " + answer);
81  
    return answer;
82  
  }
83  
  
84  
  static O export(O o, Class dest) ctex {
85  
    if (o == null || o instanceof String || o instanceof Number) return o;
86  
    
87  
    if (o instanceof List) {
88  
      List l = cast o;
89  
      List destO = new ArrayList(l.size());
90  
      for (int i = 0; i < l.size(); i++)
91  
        destO.add(export(l.get(i), dest));
92  
      return destO;
93  
    }
94  
    
95  
    if (o instanceof Lisp) {
96  
      Class destClass = dest.getClassLoader().loadClass(o.getClass().getName());
97  
      //print(o.getClass() + " => " + destClass);
98  
      
99  
      if (o.getClass() == destClass)
100  
        return o; // no export necessary
101  
        
102  
      // actually make a new object, copy fields
103  
      
104  
      O destO = nuObject(destClass);
105  
      
106  
      // TODO: go to superclasses too
107  
      Field[] fields = o.getClass().getDeclaredFields();
108  
      for (Field field : fields) {
109  
        if ((field.getModifiers() & Modifier.STATIC) != 0)
110  
          continue;
111  
        field.setAccessible(true);
112  
        Object value = field.get(o);
113  
        setOpt(destO, field.getName(), export(value, dest));
114  
      }
115  
      
116  
      return destO;
117  
    }
118  
    
119  
    throw fail(o.getClass());
120  
  }
121  
}

download  show line numbers  debug dex  old transpilations   

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

No comments. add comment

Snippet ID: #1001195
Snippet name: Lisp/NL logic engine tester (developing)
Eternal ID of this version: #1001195/1
Text MD5: 38093e81c814ac9390a22dd75079425b
Transpilation MD5: 5c427601ea2aa87fb4b40563b512c273
Author: stefan
Category: logic code
Type: JavaX source code
Public (visible to everyone): Yes
Archived (hidden from active list): No
Created/modified: 2016-04-17 22:14:15
Source code size: 3308 bytes / 121 lines
Pitched / IR pitched: No / No
Views / Downloads: 596 / 631
Referenced in: [show references]