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

137
LINES

< > BotCompany Repo | #1005043 // Marking Stuff [save works, working on guessing]

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

Uses 3874K of libraries. Click here for Pure Java version (14479L/103K/343K).

1  
!752
2  
3  
sS text = [[
4  
Yo Spain! I said:
5  
6  
"Let's do that."
7  
8  
And he said
9  
10  
"Yeah sure"
11  
12  
And they said
13  
14  
"Yeah nice"
15  
16  
And we said
17  
18  
"That's cool!"
19  
]];
20  
21  
static Color markedColor = Color.green;
22  
static Color unmarkedColor = Color.white;
23  
24  
concepts.
25  
26  
concept Marking {
27  
  S text;
28  
  new BitSet marking;
29  
}
30  
31  
static JTextPane pane, guessPane;
32  
static Marking marking;
33  
static Guesser guesser = new MarkLineByFirstChar;
34  
35  
sinterface Guesser {
36  
  BitSet guess(S text, BitSet initial);
37  
}
38  
39  
p-substance {
40  
  loadAndAutoSaveConcepts();
41  
  text = text.trim();
42  
  marking = uniqueConcept(Marking.class, "text", text);
43  
  pane = new JTextPane;
44  
  guessPane = new JTextPane;
45  
  showFrame(hgrid(
46  
    withTitle("Your marking", pane),
47  
    withTitle("My guess", guessPane)));
48  
  update();
49  
  addButtonsToWindow(pane,
50  
    textComponentDependButton(pane, jbutton("Mark quote", "mark")),
51  
    textComponentDependButton(pane, jbutton("Unmark quote", "unmark")));
52  
}
53  
54  
svoid mark { mark(true); }
55  
svoid unmark { mark(false); }
56  
57  
svoid mark(bool b) {
58  
  int from = pane.getSelectionStart(), to = pane.getSelectionEnd();
59  
  if (from < 0) ret;
60  
  for (int i = from; i < to; i++)
61  
    marking.marking.set(i, b);
62  
  change();
63  
  update();
64  
}
65  
66  
svoid update {
67  
  showMarkedText(pane, marking.marking);
68  
  BitSet guess = guess();
69  
  showMarkedText(guessPane, guess);
70  
}
71  
72  
static BitSet guess() {
73  
  try {
74  
    if (guesser != null)
75  
      ret unnull(guesser.guess(text, marking.marking));
76  
  } catch e {
77  
    print(e);
78  
  }
79  
  ret new BitSet;
80  
}
81  
82  
svoid showMarkedText(JTextPane pane, BitSet marking) {
83  
  new L mtext;
84  
  Color c = unmarkedColor;
85  
  new StringBuilder buf;
86  
  for i over text: {
87  
    Color cc = marking.get(i) ? markedColor : unmarkedColor;
88  
    if (neq(cc, c)) {
89  
      if (nempty(buf))
90  
        mtext.addAll(ll(c, str(buf)));
91  
      buf.setLength(0);
92  
      c = cc;
93  
    }
94  
    buf.append(text.charAt(i));
95  
  }
96  
  if (nempty(buf))
97  
    mtext.addAll(ll(c, str(buf)));
98  
    
99  
  // Update JTextPane with new colors, preserve selection
100  
  int from = pane.getSelectionStart(), to = pane.getSelectionEnd();
101  
  showColoredText(pane, mtext);
102  
  pane.setSelectionStart(from);
103  
  pane.setSelectionEnd(to);
104  
}
105  
106  
sclass DummyGuesser implements Guesser {
107  
  public BitSet guess(S text, BitSet initial) {
108  
    BitSet bs = cloneBitSet(initial);
109  
    for (int i = 0; i < l(text); i += 2)
110  
      bs.set(i);
111  
    ret bs;
112  
  }
113  
}
114  
115  
sclass LineStartsTest implements Guesser {
116  
  public BitSet guess(S text, BitSet initial) {
117  
    L<Int> l = lineStartIndexes(text);
118  
    ret bitSet(l);
119  
  }
120  
}
121  
122  
sclass MarkLineByFirstChar implements Guesser {
123  
  public BitSet guess(S text, BitSet initial) {
124  
    L<S> lines = filterNempty(extractMarkedLines(text, initial));
125  
    S pref = commonPrefixMulti(lines);
126  
    if (empty(pref)) ret null;
127  
    pref = substring(pref, 0, 1);
128  
    print("Prefix: " + pref);
129  
    new BitSet bs;
130  
    for (int i : lineStartIndexes(text)) {
131  
      S line = extractLine(text, i);
132  
      if (startsWith(line, pref))
133  
        setBitRange(bs, i, min(l(text), smartIndexOf(text, "\n", i)+1));
134  
    }
135  
    ret bs;
136  
  }
137  
}

download  show line numbers  debug dex  old transpilations   

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

No comments. add comment

Snippet ID: #1005043
Snippet name: Marking Stuff [save works, working on guessing]
Eternal ID of this version: #1005043/1
Text MD5: d233f4a7b20944a435659cf7adc3566e
Transpilation MD5: 0270212e92142a9572da0b27f83268f5
Author: stefan
Category: javax / a.i.
Type: JavaX source code
Public (visible to everyone): Yes
Archived (hidden from active list): No
Created/modified: 2016-10-03 20:56:17
Source code size: 3132 bytes / 137 lines
Pitched / IR pitched: No / No
Views / Downloads: 586 / 732
Referenced in: [show references]