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

241
LINES

< > BotCompany Repo | #1008139 // Demo 2 (deutsch): Fibonacci-Zahlen - translation of #1007765

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

Download Jar. Uses 4489K of libraries. Click here for Pure Java version (13723L/99K).

!7

sS theFacts = [[

  Fibonacci-Zahl n ist Fibonacci-Zahl n-1 plus Fibonacci-Zahl n-2 falls n > 2.
  Es gibt keine Fibonacci-Zahl 0.
  Fibonacci-Zahl 1 ist 1.
  Fibonacci-Zahl 2 ist 1.

  // Technisches Zeug
  gmtzfidalveipfyx feyhbrhdeovmbvaw amcmxkgwhivbouqs
  pqnfhkzxoqkaoyqk sppxbuuqeetjmzuo
]];

sS theRiddle = [[
  Was ist Fibonacci-Zahl 0?
  Was ist Fibonacci-Zahl 1?
  Was ist Fibonacci-Zahl 2?
  Was ist Fibonacci-Zahl 3?
  Was ist Fibonacci-Zahl 4?
]];

static S concepts = [[
  ["feyhbrhdeovmbvaw", "Es gibt keine X."],
  ["mtsrzasaoxrmicch", "X ist Y falls Z."],
  ["fgvvrzypbkqomktd", "X ist Y."],
  ["sppxbuuqeetjmzuo", "X plus Y"],
  ["rgsamtbrhpcqowko", "Fibonacci-Zahl X"],
  ["urfkhwfasdaqqqse", "Was ist X?"],
  ["jxcubqjtqykhfvyo", "X - Y", "as in minus"],
  ["wdbphzfoxwlrhdyl", "X > Y"],
  ["pqnfhkzxoqkaoyqk", "Operation X should be expanded."],
  ["amcmxkgwhivbouqs", "Etwas nicht existentes"],
  ["yissyloatogavyuu", "Fibonacci-Zahl"],
  ["gmtzfidalveipfyx", "Marker X implies label Y."],
  ["wvuyakuvuelmxpwp", "X on this"],
]];

!include #1007689 // Web v2

static int saveInterval = 60000;
static volatile new Web web;
static L<Lisp> facts;
static long lastSave, sizeSaved;

extend Web {
  volatile transient bool cancelled, calculating;
}

p-tt {
  setConsoleTitle(programTitle());
  load("web"); lastSave = sysNow();
  setFrameHeight(consoleFrame(), 400);
  centerConsole();
  //magellan();
  //graphite();
  //mistAqua();
  //moderateTheme();
  sahara();
  swing {
    setConsoleInput("Was ist Fibonacci-Zahl 500?");
    consoleAddAskButton();
    focusConsole();
    final JLabel lblBrainSize = jRightAlignedLabel();
    addToConsole2(withMargin(centerAndEast(lblBrainSize,
      withLeftMargin(jbutton("Reset", "reset")))));
    awtEvery(lblBrainSize, 1000, r {
      lblBrainSize.setText("Brain Size: " + toM(fileSize(programFile("web.structure")), 1) + " MB");
    });
    awtCalcEvery(lblBrainSize, 1000, 1000, r {
      if (web.calculating) ret;
      int webSize = web.count();
      if (shouldSave(webSize, false)) saveWeb(webSize);
    });
  }

  aiEnhancements();
  aiConcepts_noAutoClear();
  useConceptsDump(concepts);
  set englishToConceptLanguage_useBrackets;
  facts = clParse(linesToCL(theFacts));
  print(theFacts);
  useFacts(theFacts);
  printNumberedLines(facts); // print parsed facts
  
  firstRiddle();
  
  makeBot();
  notDone();
}

svoid firstRiddle {
  print();
  for (S rid : toLinesFullTrim(theRiddle)) {
    print("? " + rid);
    print("  " + answer(rid));
  }
  print();
}

static int webSize() { ret web.count(); }

sbool shouldSave(int webSize, bool checkInterval) {
  ret webSize != sizeSaved &&
    (!checkInterval || sysNow() >= lastSave+saveInterval);
}

svoid saveWeb(int webSize) {
  save("web");
  sizeSaved = webSize;
  lastSave = sysNow();
}

static S answer(S rid) {
  web.calculating = true;
  try {
    ret answerImpl(rid);
  } finally {
    web.calculating = false;
  }
}

static S answerImpl(S rid) {
  if (match("web", rid)) { print(web); ret "OK"; }
  if (match("visualize", rid)) { webToCAL(web).show(); ret "OK"; }
  Web web = main.web;
  Lisp riddle = clParse(englishToConceptLanguage(rid));
  //print("# " + riddle);
  
  if (!riddle.is("urfkhwfasdaqqqse", "mrhggsmuykeceghd")) ret "huh?"; // Was ist X? // Was ist the decimal representation of X?
  Lisp mainConcept = riddle.get(0);

  // Make web
  web.node(mainConcept);
  web.relations(facts);
  
  int webSize = web.count();
  int cursor = 0;
  while licensed {
    if (web.cancelled) ret "cancelled";
    //print("Web size: " + webSize);
    if (shouldSave(webSize, true)) {
      printWithoutNL("S");
      saveWeb(webSize);
    } else
      printWithoutNL(".");
    if ((++cursor % 60) == 0) print();
    
    CriticalAction action = beginCriticalAction("Web Update");
    try {
      web_expandMarkers2(web);
      web_expandOperations2(web);

      for (LispStatement ls : lispStatementsByHead("mtsrzasaoxrmicch")) {
        if (web.cancelled) ret "cancelled";
        Lisp rule = ls.term, x = rule.get(0);
        //print("Pot size: " + l(web.getPot(ls.globalID)));
        for (WebNode node : web.clearPot(ls.globalID)) {
          for (Lisp lbl : cloneList(node.labels)) {
            //print("Matching " + x + " and " + lbl);
            Map<S, Lisp> matches = lispMatch(x, lbl, ll("n"));
            if (matches != null) {
              //print("Rule match: " + struct(matches));
              Lisp bla = lispReplaceVars(rule.get(2), matches);
              if (checkACondition(bla)) {
                bla = lispReplaceVars(rule.get(1), matches);
                bla = lispCalculateDeep(bla);
                bla = web_insertNumbers(web, bla);
                //print("bla: " + bla);
                bla = lispCalculateDeep(bla);
                if (lispIsInt(bla)) {
                  node.addLabel(bla); // Found!
                  //pot.remove(node);
                } else {
                  //print("Condition OK! Evaluating: " + bla);
                  web.node(bla);
                  // Place into pot again
                  web.getPot(ls.globalID).add(node);
                }
              }
            }
          }
        }
        //print("Node: " + node + ", condition: " + condition);
      }
    } finally {
      action.done();
    }
    
    int n = web.count();
    if (n == webSize) break;
    webSize = n;
  }
  print();
  
  // print question again
  if (cursor / 60 >= 10)
    print("? " + rid);
    
  // Answer question
  L<Lisp> l = listWithout(web.findNode(mainConcept).labels, mainConcept);
  //print("Labels: " + l);
  S s = chooseALabelToReturn(l);
  s = or2(conceptLanguageToEnglish(unquote(s)), "I don't know");
  ret s;
}

sS chooseALabelToReturn(L<Lisp> ll) {
  L<S> l = map(f clUnparse, ll);
  //print("  Labels: " + l);
  S s = findInteger(l);
  if (s == null) s = firstGlobalID(l);
  //if (s == null) s = random(l);
  if (s == null) ret s = first(l);
  ret s;
}

sbool checkACondition(Lisp l) false on exception {
  ret lispCalculateDeep(l).is("true");
}

svoid cleanMeUp {
  while (criticalActionsInFlight()) sleep(5);
  if (sizeSaved != web.count())
    save("web");
}

svoid reset {
  if (!confirmOKCancel("Really clear the Fibonacci brain?")) ret;
  web.cancelled = true;
  CriticalAction action = beginCriticalAction("Resetting Brain");
  try {
    web = new Web;
    sizeSaved = 0;
    save("web");
  } finally {
    action.done();
  }
  clearConsole();
  firstRiddle();
}

Author comment

Began life as a copy of #1007765

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, unoaxrwscvea, vouqrxazstgt

No comments. add comment

-
Snippet ID: #1008139
Snippet name: Demo 2 (deutsch): Fibonacci-Zahlen - translation of #1007765
Eternal ID of this version: #1008139/9
Text MD5: 1a40a03c1f6de29664b886616b7e086e
Transpilation MD5: 450757b3a7b99ed1129e840e9a910934
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-26 12:25:02
Source code size: 6713 bytes / 241 lines
Pitched / IR pitched: No / No
Views / Downloads: 569 / 1146
Version history: 8 change(s)
Referenced in: