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

94
LINES

< > BotCompany Repo | #1004704 // Swing Text Predict

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

Uses 5262K of libraries. Click here for Pure Java version (2958L/20K/72K).

1  
!752
2  
3  
static bool compileLua = true;
4  
5  
static JTextArea ta1, ta2;
6  
7  
static bool needUpdate = true;
8  
static O sucker;
9  
static S desc;
10  
static double score;
11  
12  
// abusing this for pre-making and caching a single instance
13  
static TimedCache<Algorithm> trainedAlgo = new TimedCache(0); // no expire
14  
15  
p {
16  
  sucker = run("#1004698");
17  
  desc = (S) call(sucker, "highestDesc");
18  
  score = (double) call(sucker, "highestScore");
19  
  
20  
  JFrame frame = showFrame("\*programTitle()*/ (Score: \*formatDouble(score, 1)*/%)",
21  
    vgrid(ta1 = new JTextArea, ta2 = new JTextArea));
22  
    
23  
  addMenu(frame, "Stuff", "Show original text", r { showOriginalText() });
24  
  
25  
  onUpdate(ta1, r { needUpdate = true; });
26  
  
27  
  final new SingleThread st;
28  
  
29  
  installTimer(ta2, 100, r {
30  
    if (needUpdate && !st.running()) {
31  
      needUpdate = false;
32  
      st.go(r {
33  
        luaCompile(compileLua);
34  
        Algorithm algo = getTrainedAlgo();
35  
        trainedAlgo.clear();
36  
        final S t = ta1.getText();
37  
        final S text = autoPredict(t, algo, 100);
38  
        awt {
39  
          ta2.setText(t + text);
40  
          ta2.setCaretPosition(0);
41  
        }
42  
        getTrainedAlgo(); // make new one for next time
43  
      });
44  
    }
45  
  });
46  
}
47  
48  
static void feedText(S text, Algorithm algo) {
49  
  int n = l(text);
50  
51  
  for (int pos = 0; pos < n; ) {
52  
    S s = algo.predict(n-pos);
53  
    if (empty(s)) s = " "; // We're lenient
54  
    int j = min(pos+l(s), n);
55  
    algo.feed(substring(text, pos, j));
56  
    pos = j;
57  
  }
58  
}
59  
60  
static Algorithm getTrainedAlgo() {
61  
  ret trainedAlgo.get(func { makeTrainedAlgo() });
62  
}
63  
64  
static S autoPredict(S text, Algorithm algo, int max) {
65  
  feedText(text, algo);
66  
  new StringBuilder buf;
67  
  int pos = 0;
68  
  S s = null;
69  
  while (pos < max) {
70  
    if (s != null)
71  
      algo.feed(s);
72  
    s = algo.predict(max-pos);
73  
    if (empty(s)) s = " "; // We're lenient
74  
    s = substring(s, 0, max-pos);
75  
    buf.append(s);
76  
    pos += l(s);
77  
  }
78  
  ret str(buf);
79  
}
80  
81  
static Algorithm makeTrainedAlgo() {
82  
  Algorithm algo = makeLuaTextPredictAlgo(desc);
83  
  
84  
  Class intrface = _getClass(sucker, "main$Algorithm");
85  
  assertNotNull("main$Algorithm", intrface);
86  
  O proxy = proxy(intrface, algo);
87  
  call(sucker, "scoreAlgorithm", proxy); // train
88  
  
89  
  ret algo;
90  
}
91  
92  
svoid showOriginalText {
93  
  showText("Original Text", (S) getOpt(sucker, "text"));
94  
}

download  show line numbers  debug dex  old transpilations   

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

No comments. add comment

Snippet ID: #1004704
Snippet name: Swing Text Predict
Eternal ID of this version: #1004704/1
Text MD5: bc814736a4dbc043b20702b58a1ecc09
Transpilation MD5: 31c98b1bd936d05ab02d480fcd5e9488
Author: stefan
Category: javax / a.i.
Type: JavaX source code
Public (visible to everyone): Yes
Archived (hidden from active list): No
Created/modified: 2016-08-27 16:21:46
Source code size: 2374 bytes / 94 lines
Pitched / IR pitched: No / No
Views / Downloads: 699 / 839
Referenced in: [show references]