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

93
LINES

< > BotCompany Repo | #1004574 // Make Text & Learn (dev.)

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

Transpiled version (2654L) is out of date.

1  
!752
2  
3  
p {
4  
  L<S> texts = splitAtSpace("a b c d");
5  
  data = litorderedmap();
6  
  for (S s : texts)
7  
    data.put(renderTheText(s), s);
8  
  push("Dummy");
9  
  push("MD5Recognizer");
10  
  solveImage2Text();
11  
}
12  
13  
static class Dummy {
14  
  O get(BWImage img) { ret "dummy"; }
15  
}
16  
17  
static class MD5Recognizer {
18  
  new Map<S, S> map;
19  
  
20  
  O get(BWImage img) { ret map.get(md5OfBWImage(img)); }
21  
  
22  
  void train(Map<BWImage, S> data) {
23  
    for (BWImage img : keys(data)) {
24  
      S text = data.get(img);
25  
      map.put(md5OfBWImage(img), text);
26  
    }
27  
  }
28  
}
29  
30  
static Font renderTheText_font;
31  
32  
static BWImage renderTheText(S s) {
33  
  if (renderTheText_font == null)
34  
    renderTheText_font = loadFont("#1004569"); // "Ticketing"
35  
  ret new BWImage(renderText(renderTheText_font, 20f, s));
36  
}
37  
38  
static void push(S solver) {
39  
  newProducts.add(solver);
40  
}
41  
42  
// solvers are func(BWImage) -> S
43  
44  
static Map<BWImage, S> data;
45  
static new LinkedBlockingQueue<S> newProducts;
46  
static new Best best;
47  
48  
sclass Best {
49  
  S solver;
50  
  double score;
51  
  
52  
  void update(S solver, double score) {
53  
    if (score > this.score) {
54  
      print("New best score: " + score);
55  
      this.score = score;
56  
      this.solver = solver;
57  
    }
58  
  }
59  
}
60  
61  
static void solveImage2Text() {
62  
  while licensed {
63  
    testSolver(grabFromQueue(newProducts));
64  
  }
65  
}
66  
67  
static void testSolver(S solver) {
68  
  O realSolver = unstructure(solver);
69  
  train(realSolver, data);
70  
  double score = score(realSolver, data);
71  
  print(formatDouble(score, 1) + ": " + solver);
72  
  best.update(solver, score);
73  
}
74  
75  
static void train(O realSolver, Map<BWImage, S> data) {
76  
  callOpt(realSolver, "train", data);
77  
}
78  
79  
static double score(O realSolver, Map<BWImage, S> data) {
80  
  double score = 0;
81  
  int n = 0;
82  
  for (BWImage img : keys(data)) {
83  
    S text = data.get(img);
84  
    ++n;
85  
    try {
86  
      if (eq(callF(realSolver, img), text))
87  
        ++score;
88  
    } catch e {
89  
      silentException(e);
90  
    }
91  
  }
92  
  ret score*100/n;
93  
}

download  show line numbers  debug dex  old transpilations   

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

No comments. add comment

Snippet ID: #1004574
Snippet name: Make Text & Learn (dev.)
Eternal ID of this version: #1004574/1
Text MD5: 3fcf5fe9e65f09fb820473bbce4aa9fb
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-24 14:49:38
Source code size: 1993 bytes / 93 lines
Pitched / IR pitched: No / No
Views / Downloads: 658 / 745
Referenced in: [show references]