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

88
LINES

< > BotCompany Repo | #1000991 // Token prediction with graphical display

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

Libraryless. Click here for Pure Java version (2134L/17K/51K).

!747

abstract class P {
  abstract void write(S tok);
  abstract S read();
}

m {
  static class Pairs extends P {
    new Map<S,S> map;
    S last;

    void write(S tok) {
      if (last != null)
        map.put(last, tok);
      last = tok;
    }

    S read() {
      return last == null ? null : map.get(last);
    }
  }

  p {
    P p = new Pairs;
    S corpus = makeCorpus(10);
    L<S> tok = javaTok(corpus);
    print("Tokens in corpus: " + tok.size());
    
    new L<Boolean> predicted;
    int points = 0;
    for (int i = 1; i < tok.size(); i += 2) {
      S t = tok.get(i);
      S x = p.read();
      boolean correct = t.equals(x);
      while (predicted.size() < i) predicted.add(false);
      predicted.add(correct);
      if (correct) ++points;
      p.write(t);
    }
    double score = points*100.0/(tok.size()/2);
    print(points + "/"+ tok.size()/2 + " = " + formatDouble(score, 2) + "%");
    
    showColoredText(tok, predicted);
  }
  
  static void showColoredText(L<S> tok, L<Boolean> predicted) ctex {
    JFrame jf = new JFrame("Predicted = green");
    Container cp = jf.getContentPane();

    JTextPane pane = new JTextPane();
    //pane.setFont(loadFont("#1000993", 24));
    Document doc = pane.getStyledDocument();

    for (int i = 0; i < tok.size(); i++) {
      if (tok.get(i).length() == 0) continue;
      boolean green = i < predicted.size() && predicted.get(i);
      SimpleAttributeSet set = new SimpleAttributeSet();
      StyleConstants.setForeground(set, green ? Color.green : Color.gray);
      doc.insertString(doc.getLength(), tok.get(i), set);
    }
    
    JScrollPane scrollPane = new JScrollPane(pane);
    cp.add(scrollPane, BorderLayout.CENTER);

    jf.setBounds(100, 100, 600, 600);
    jf.setVisible(true);
  }
  
  !include #1000989 // SnippetDB
  
  static S makeCorpus(int numSnippets) {
    new StringBuilder buf;
    SnippetDB db = new SnippetDB("#1000673");
    List<List<S>> rows = db.rowsOrderedBy("sn_created");
    for (int i = 0; i < Math.min(rows.size(), numSnippets); i++) {
      S id = db.getField(rows.get(i), "sn_id");
      S title = db.getField(rows.get(i), "sn_title");
      S text = db.getField(rows.get(i), "sn_text");
      buf.append("\n== ID: " + id);
      buf.append("\n== Title: " + title);
      buf.append("\n==\n");
      buf.append(text).append("\n");
      ++i;
    }
    return buf.toString();
  }
}

download  show line numbers  debug dex  old transpilations   

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

No comments. add comment

Snippet ID: #1000991
Snippet name: Token prediction with graphical display
Eternal ID of this version: #1000991/1
Text MD5: 7c65fe7ebea79b2f6d745411ac79759f
Transpilation MD5: 9aebadd15ce2d040cde1bcbbd035f2a3
Author: stefan
Category:
Type: JavaX source code
Public (visible to everyone): Yes
Archived (hidden from active list): No
Created/modified: 2015-09-14 18:23:50
Source code size: 2481 bytes / 88 lines
Pitched / IR pitched: No / Yes
Views / Downloads: 650 / 742
Referenced in: [show references]