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

184
LINES

< > BotCompany Repo | #1007674 // Demo 2: Fibonacci numbers [WORKS]

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

Download Jar. Uses 3874K of libraries. Click here for Pure Java version (11189L/78K).

1  
!7
2  
3  
sS theFacts = [[
4  
5  
  Fibonacci number n is Fibonacci number n-1 plus Fibonacci number n-2 if n > 2.
6  
  There is no Fibonacci number with a negative index.
7  
  There is no Fibonacci number 0.
8  
  Fibonacci number 1 is 1.
9  
  Fibonacci number 2 is 1.
10  
11  
  // Some technical stuff
12  
  gmtzfidalveipfyx feyhbrhdeovmbvaw amcmxkgwhivbouqs
13  
  pqnfhkzxoqkaoyqk sppxbuuqeetjmzuo
14  
]];
15  
16  
sS theRiddle = [[
17  
  What is Fibonacci number 0?
18  
  What is Fibonacci number 1?
19  
  What is Fibonacci number 2?
20  
  What is Fibonacci number 3?
21  
  What is Fibonacci number 4?
22  
]];
23  
24  
static S concepts = [[
25  
  ["feyhbrhdeovmbvaw", "There is no X."],
26  
  ["mtsrzasaoxrmicch", "X is Y if Z."],
27  
  ["fgvvrzypbkqomktd", "X is Y."],
28  
  ["sppxbuuqeetjmzuo", "X plus Y"],
29  
  ["devwldetfsfylnod", "X with a negative index"],
30  
  ["rgsamtbrhpcqowko", "Fibonacci number X"],
31  
  ["jxcubqjtqykhfvyo", "X - Y", "as in minus"],
32  
  ["wdbphzfoxwlrhdyl", "X > Y"],
33  
  ["urfkhwfasdaqqqse", "What is X?"],
34  
  ["pqnfhkzxoqkaoyqk", "Operation X should be expanded."],
35  
  ["amcmxkgwhivbouqs", "Something that doesn't exist"],
36  
  ["yissyloatogavyuu", "Fibonacci number"],
37  
  ["gmtzfidalveipfyx", "Marker X implies label Y."]
38  
]];
39  
40  
!include #1007689 // Web v2
41  
42  
static int saveInterval = 60000;
43  
static new Web web;
44  
static L<Lisp> facts;
45  
static long lastSave, sizeSaved;
46  
47  
p-tt {
48  
  setConsoleTitle(programTitle());
49  
  load("web"); lastSave = sysNow();
50  
  setFrameHeight(consoleFrame(), 400);
51  
  centerConsole();
52  
  //magellan();
53  
  //graphite();
54  
  //mistAqua();
55  
  //moderateTheme();
56  
  sahara();
57  
  swing {
58  
    setConsoleInput("What is Fibonacci number 500?");
59  
    consoleAddAskButton();
60  
    focusConsole();
61  
    final JLabel lblBrainSize = jRightAlignedLabel();
62  
    addToConsole(lblBrainSize);
63  
    awtEvery(lblBrainSize, 1000, r {
64  
      lblBrainSize.setText("Brain Size: " + toM(fileSize(programFile("web.structure")), 1) + "MB");
65  
    });
66  
  }
67  
68  
  aiEnhancements();
69  
  aiConcepts_noAutoClear();
70  
  useConceptsDump(concepts);
71  
  set englishToConceptLanguage_useBrackets;
72  
  facts = clParse(linesToCL(theFacts));
73  
  print(theFacts);
74  
  useFacts(theFacts);
75  
  //printNumberedLines(facts);
76  
  print();
77  
  
78  
  for (S rid : toLinesFullTrim(theRiddle)) {
79  
    print("? " + rid);
80  
    print("  " + answer(rid));
81  
  }
82  
    
83  
  makeBot();
84  
  notDone();
85  
}
86  
87  
static S answer(S rid) {
88  
  if (eq(rid, "web")) { print(web); ret "OK"; }
89  
  Lisp riddle = clParse(englishToConceptLanguage(rid));
90  
  //print("# " + riddle);
91  
  
92  
  if (!riddle.is("urfkhwfasdaqqqse", "mrhggsmuykeceghd")) ret "huh?"; // What is X? // What is the decimal representation of X?
93  
  Lisp mainConcept = riddle.get(0);
94  
95  
  // Make web
96  
  web.node(mainConcept);
97  
  web.relations(facts);
98  
  
99  
  int webSize = web.count();
100  
  int cursor = 0;
101  
  while licensed {
102  
    //print("Web size: " + webSize);
103  
    if (webSize != sizeSaved && sysNow() >= lastSave+saveInterval) {
104  
      printWithoutNL("S");
105  
      save("web");
106  
      sizeSaved = webSize;
107  
      lastSave = sysNow();
108  
    } else
109  
      printWithoutNL(".");
110  
    if ((++cursor % 60) == 0) print();
111  
    
112  
    CriticalAction action = beginCriticalAction("Web Update");
113  
    try {
114  
      web_expandMarkers(web);
115  
      web_expandOperations(web);
116  
  
117  
      for (Lisp rule : trueStatementsByHead("mtsrzasaoxrmicch")) {
118  
        Lisp x = rule.get(0);
119  
        for (WebNode node : cloneList(web.nodes)) {
120  
          for (Lisp lbl : cloneList(node.labels)) {
121  
            //print("Matching " + x + " and " + lbl);
122  
            Map<S, Lisp> matches = lispMatch(x, lbl, ll("n"));
123  
            if (matches != null) {
124  
              //print("Rule match: " + struct(matches));
125  
              Lisp bla = lispReplaceVars(rule.get(2), matches);
126  
              if (checkACondition(bla)) {
127  
                bla = lispReplaceVars(rule.get(1), matches);
128  
                bla = lispCalculateDeep(bla);
129  
                bla = web_insertNumbers(web, bla);
130  
                //print("bla: " + bla);
131  
                bla = lispCalculateDeep(bla);
132  
                if (lispIsInt(bla))
133  
                  node.addLabel(bla); // Found!
134  
                else {
135  
                  //print("Condition OK! Evaluating: " + bla);
136  
                  web.node(bla);
137  
                }
138  
              }
139  
            }
140  
          }
141  
        }
142  
        //print("Node: " + node + ", condition: " + condition);
143  
      }
144  
    } finally {
145  
      action.done();
146  
    }
147  
    
148  
    int n = web.count();
149  
    if (n == webSize) break;
150  
    webSize = n;
151  
  }
152  
  print();
153  
  
154  
  // print question again
155  
  if (cursor / 60 >= 10)
156  
    print("? " + rid);
157  
    
158  
  // Answer question
159  
  L<Lisp> l = listWithout(web.findNode(mainConcept).labels, mainConcept);
160  
  //print("Labels: " + l);
161  
  S s = chooseALabelToReturn(l);
162  
  s = or2(conceptLanguageToEnglish(unquote(s)), "I don't know");
163  
  ret s;
164  
}
165  
166  
sS chooseALabelToReturn(L<Lisp> ll) {
167  
  L<S> l = map(f clUnparse, ll);
168  
  //print("  Labels: " + l);
169  
  S s = findInteger(l);
170  
  if (s == null) s = firstGlobalID(l);
171  
  //if (s == null) s = random(l);
172  
  if (s == null) ret s = first(l);
173  
  ret s;
174  
}
175  
176  
sbool checkACondition(Lisp l) false on exception {
177  
  ret lispCalculateDeep(l).is("true");
178  
}
179  
180  
svoid cleanMeUp {
181  
  while (criticalActionsInFlight()) sleep(5);
182  
  if (sizeSaved != web.count())
183  
    save("web");
184  
}

Author comment

Began life as a copy of #1007649

download  show line numbers  debug dex  old transpilations   

Travelled to 14 computer(s): aoiabmzegqzx, bhatertpkbcr, cbybwowwnfue, cfunsshuasjs, gwrvuhgaqvyk, ishqpsrjomds, lpdgvwnxivlt, mqqgnosmbjvj, pyentgdyhuwx, pzhvpgtvlbxg, tslmcundralx, tvejysmllsmz, vouqrxazstgt, wtqryiryparv

No comments. add comment

Snippet ID: #1007674
Snippet name: Demo 2: Fibonacci numbers [WORKS]
Eternal ID of this version: #1007674/157
Text MD5: 652d6023411e2b99dbf051357a5947c6
Transpilation MD5: 1ce2a023e4d5b2213f3a42e5f0352c18
Author: stefan
Category: javax / a.i.
Type: JavaX source code (desktop)
Public (visible to everyone): Yes
Archived (hidden from active list): No
Created/modified: 2017-04-05 14:35:04
Source code size: 5302 bytes / 184 lines
Pitched / IR pitched: No / No
Views / Downloads: 1169 / 2809
Version history: 156 change(s)
Referenced in: [show references]