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

74
LINES

< > BotCompany Repo | #1025613 // Philosophy Bot 2 - match questions and answers [v3 with soc, dev.]

JavaX source code (Dynamic Module) [tags: use-pretranspiled] - run with: Stefan's OS

Uses 911K of libraries. Click here for Pure Java version (6601L/34K).

1  
!7
2  
3  
cmodule PhilosophyBot2 extends DynPrintLog {
4  
  transient L unmappedLines = ll("good", "early morning");
5  
  transient L unmappedQuestions = ll("what is the time", "how do you feel");
6  
  transient SS mappings = ciMap();
7  
  
8  
  transient S program = [[
9  
    start
10  
    
11  
    start => proc {
12  
      run (proc {
13  
        while (there are unmapped questions):
14  
          find (an unmapped question) x
15  
          find (an unmapped line) y
16  
          map x to y
17  
      }
) with backtracking
18  
    }
19  
  ]];
20  
  
21  
  transient new PhilosophyBot1 bot;
22  
23  
  start-thread {
24  
    bot.program = program;
25  
    bot.addNativePredicate("there are unmapped questions",
26  
      () -> nempty(unmappedQuestions));
27  
28  
    bot.addNativePredicate("find (an unmapped question) x",
29  
      (map, env) -> {
30  
        print("find unmapped question x");
31  
        ret randomChoiceFromChangingList('x, unmappedQuestions, env);
32  
      });
33  
34  
    bot.addNativePredicate("find (an unmapped line) z",
35  
      (map, env) -> {
36  
        print("find unmapped line z");
37  
        ret randomChoiceFromChangingList('z, unmappedLines, env);
38  
      });
39  
40  
    bot.addNativePredicate("map x to y",
41  
      (map, env) -> {
42  
        S x = map.get("x"), y = map.get("y");
43  
        unmappedQuestions.remove(x);
44  
        unmappedLines.remove(y);
45  
        print("Remaining unmapped questions: " + l(unmappedQuestions));
46  
        mappings.put(x, y);
47  
        print("Mapped question " + quote(x) + " to answer " + quote(y));
48  
        ret !env.wantAlternatives() ? true : new PhilosophyBot1.WithAlternative(() -> {
49  
          print("Undoing mapping of " + quote(x) + " to answer " + quote(y));
50  
          unmappedQuestions.add(x);
51  
          unmappedLines.add(y);
52  
          false;
53  
        }, true);
54  
      });
55  
56  
   bot.addNativePredicate("run x with backtracking",
57  
     (SS map) -> {
58  
        S x = map.get("x");
59  
        print("Run x with backtracking: " + x);
60  
        bot.runParsedProcedure(bot.parseProcedure(x)); // TODO
61  
        print("Ran x with backtracking: " + x);
62  
        true;
63  
     });
64  
   
65  
    bot.run();
66  
  }
67  
68  
  O randomChoiceFromChangingList(S var, Cl<S> list, PhilosophyBot1.Env env) {
69  
    if (empty(unmappedLines)) false;
70  
    if (!env.wantAlternatives())
71  
      ret litcimap(var, random(list));
72  
    ret lazyMap(cloneList(list), z -> litcimap(var, z));
73  
  }
74  
}

download  show line numbers  debug dex  old transpilations   

Travelled to 6 computer(s): bhatertpkbcr, mqqgnosmbjvj, pyentgdyhuwx, pzhvpgtvlbxg, tvejysmllsmz, vouqrxazstgt

No comments. add comment

Snippet ID: #1025613
Snippet name: Philosophy Bot 2 - match questions and answers [v3 with soc, dev.]
Eternal ID of this version: #1025613/8
Text MD5: f9848b135a5ea6ee3c31e7397c1e94fe
Transpilation MD5: 77b4b289bbf6c621635afada90fc880b
Author: stefan
Category:
Type: JavaX source code (Dynamic Module)
Public (visible to everyone): Yes
Archived (hidden from active list): No
Created/modified: 2019-10-08 20:34:38
Source code size: 2281 bytes / 74 lines
Pitched / IR pitched: No / No
Views / Downloads: 149 / 415
Version history: 7 change(s)
Referenced in: [show references]