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).

!752

static bool compileLua = true;

static JTextArea ta1, ta2;

static bool needUpdate = true;
static O sucker;
static S desc;
static double score;

// abusing this for pre-making and caching a single instance
static TimedCache<Algorithm> trainedAlgo = new TimedCache(0); // no expire

p {
  sucker = run("#1004698");
  desc = (S) call(sucker, "highestDesc");
  score = (double) call(sucker, "highestScore");
  
  JFrame frame = showFrame("\*programTitle()*/ (Score: \*formatDouble(score, 1)*/%)",
    vgrid(ta1 = new JTextArea, ta2 = new JTextArea));
    
  addMenu(frame, "Stuff", "Show original text", r { showOriginalText() });
  
  onUpdate(ta1, r { needUpdate = true; });
  
  final new SingleThread st;
  
  installTimer(ta2, 100, r {
    if (needUpdate && !st.running()) {
      needUpdate = false;
      st.go(r {
        luaCompile(compileLua);
        Algorithm algo = getTrainedAlgo();
        trainedAlgo.clear();
        final S t = ta1.getText();
        final S text = autoPredict(t, algo, 100);
        awt {
          ta2.setText(t + text);
          ta2.setCaretPosition(0);
        }
        getTrainedAlgo(); // make new one for next time
      });
    }
  });
}

static void feedText(S text, Algorithm algo) {
  int n = l(text);

  for (int pos = 0; pos < n; ) {
    S s = algo.predict(n-pos);
    if (empty(s)) s = " "; // We're lenient
    int j = min(pos+l(s), n);
    algo.feed(substring(text, pos, j));
    pos = j;
  }
}

static Algorithm getTrainedAlgo() {
  ret trainedAlgo.get(func { makeTrainedAlgo() });
}

static S autoPredict(S text, Algorithm algo, int max) {
  feedText(text, algo);
  new StringBuilder buf;
  int pos = 0;
  S s = null;
  while (pos < max) {
    if (s != null)
      algo.feed(s);
    s = algo.predict(max-pos);
    if (empty(s)) s = " "; // We're lenient
    s = substring(s, 0, max-pos);
    buf.append(s);
    pos += l(s);
  }
  ret str(buf);
}

static Algorithm makeTrainedAlgo() {
  Algorithm algo = makeLuaTextPredictAlgo(desc);
  
  Class intrface = _getClass(sucker, "main$Algorithm");
  assertNotNull("main$Algorithm", intrface);
  O proxy = proxy(intrface, algo);
  call(sucker, "scoreAlgorithm", proxy); // train
  
  ret algo;
}

svoid showOriginalText {
  showText("Original Text", (S) getOpt(sucker, "text"));
}

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: 597 / 719
Referenced in: [show references]