Permalink
Apr 22, 2016
May 5, 2016
May 17, 2017
Apr 22, 2016
Apr 22, 2016
May 5, 2016
May 5, 2016
May 5, 2016
May 5, 2016
May 5, 2016
May 17, 2017
Apr 22, 2016
May 17, 2017
Apr 22, 2016
May 17, 2017
May 17, 2017
May 17, 2017
May 17, 2017
May 17, 2017
May 17, 2017
May 17, 2017
Newer
100644
232 lines (218 sloc)
7.06 KB
1
function() {
2
this.label = 'qw';
3
this.onEnter = function(p) { window.isWritingToTextSketch = true; }
4
this.onExit = function(p) { window.isWritingToTextSketch = false; }
5
this.onDelete = function(p) { window.isWritingToTextSketch = false; }
6
this.mouseMove = function(x, y, z) {
7
if (this._isRespondingToMouseMove)
8
this.qw.trackXY(this.inverseTransform([x, y, def(z)]));
9
}
10
this._isRespondingToMouseMove = true;
11
this._isStartOfSentence = true;
12
this._counter = 0;
13
this.mouseDown = function(x, y, z) {
14
if (! this.isChoosingWord) {
15
this._isRespondingToMouseMove = false;
16
this._stroke = [ this.inverseTransform([x, y, def(z)]) ];
17
}
18
}
19
this.mouseDrag = function(x, y, z) {
20
if (! this.isChoosingWord) {
21
this._stroke.push(this.inverseTransform([x, y, def(z)]));
22
if (this._counter++ % 10 == 0)
23
this._words = qwLookupWord(this._stroke);
24
}
25
}
26
this.mouseUp = function(x, y, z) {
27
if (this.isChoosingWord) {
28
this.isChoosingWord = false;
29
var p = this.inverseTransform([x, y, def(z)]);
30
this.outputWord(this._words[p[1] > 0 ? 0 : 1]);
31
this._words = null;
32
}
33
else if (this._stroke.length > 5) {
34
this._words = qwLookupWord(this._stroke);
35
if (this._words.length == 1) {
36
this.outputWord(this._words[0]);
37
this._words = null;
38
}
39
else
40
this.isChoosingWord = true;
41
this._stroke = null;
42
}
43
}
44
this.outputWord = function(word) {
45
if (word.indexOf('DEL') == 0)
46
sketchPage.handleTextSketchChar('del');
47
else
48
for (let i = 0 ; i < word.length ; i++) {
49
let ch = word.substring(i, i+1);
50
if (ch.indexOf('.') == 0)
51
this._isStartOfSentence = true;
52
else if (i == 1 && this._isStartOfSentence) {
53
ch = ch.toUpperCase();
54
this._isStartOfSentence = false;
55
}
56
if (word === ' i' && ch === 'i')
57
ch = 'I';
58
sketchPage.handleTextSketchChar(ch);
59
}
60
}
61
this.qw = new QW2();
62
this.render = function() {
63
this.duringSketch(function() {
66
});
67
this.afterSketch(function() {
68
var faded = backgroundColor == 'white' ? .3 : .1;
69
color(palette.color[this.colorId]);
70
for (var i = 0 ; i < this.qw.zones.length ; i++) {
71
var z = this.qw.zones[i];
73
color(fadedColor(faded, this.colorId));
74
mFillDisk(z, z[2]);
75
color(palette.color[this.colorId]);
77
lineWidth(this.mScale(i==0 && this.qw.sequence.length == 0 ? .03 : .01));
78
mDrawDisk(z, z[2]);
84
var s = this.qw.toVisibleChar(this.qw.A(row, col));
85
var fh = s.length == 1 ? .18 : .1;
88
var xy = this.qw.rowAndColToXY(row, col);
89
mText(s, xy, .5, .5);
90
if (col == this.qw.col && row == this.qw.row) {
91
mDrawDisk(xy, .1);
100
101
if (this._stroke) {
102
color('red');
103
lineWidth(4);
104
mCurve(this._stroke);
105
}
106
});
107
if (this._words)
108
if (this._words.length == 1)
109
mText(this._words[0], [0,0], .5, .5);
110
else {
111
mText(this._words[0], [0,0], .5, 1.5);
112
mText(this._words[1], [0,0], .5,-0.5);
113
}
114
}
115
}
117
var qwDictionary = (function() {
118
function addToDictionary(word, stroke) {
119
dictionary.push({
120
word : word,
121
stroke: resampleCurve(stroke, 100)
122
});
124
var dictionary = [];
125
var qw = new QW2();
126
function zoneToXY(zone) {
127
return [ .85 * Math.cos(zone * Math.PI / 4),
128
.85 * Math.sin(zone * Math.PI / 4) ];
130
for (let n = 0 ; n < 7697 ; n++) {
131
let word = wordList[n];
132
133
var stroke = [], prevZones;
134
for (let i = 0 ; i < word.length ; i++) {
135
let zones = qw.letterToZones(word.substring(i, i+1));
136
137
if (i > 0 && zones[0] == prevZones[prevZones.length-1]) {
138
let xy = zoneToXY(zones[0]);
139
stroke.push([xy[0]/2, xy[1]/2]);
140
if (i == word.length - 1 && zones.length == 1)
141
continue;
142
}
143
prevZones = zones;
151
addToDictionary('DEL', [[0,0],[-.85, 0 ]]);
152
addToDictionary('\n' , [[0,0],[ 0,-.85]]);
153
addToDictionary('.' , [[0,0],[ .6,-.6 ]]);
154
addToDictionary(',' , [[0,0],[-.6,-.6 ]]);
155
return dictionary;
156
})();
157
158
function qwLookupWord(stroke) {
159
stroke = resampleCurve(stroke, 100);
160
var lowScore = [1000000, 1000000], I = [0,0];
161
for (let i = 0 ; i < qwDictionary.length ; i++) {
162
let s = qwDictionary[i].stroke, score = 0;
163
for (let j = 0 ; j < 100 ; j++) {
164
let x = stroke[j][0] - s[j][0];
165
let y = stroke[j][1] - s[j][1];
166
score += x * x + y * y;
167
}
168
if (score < lowScore[0]) {
169
lowScore[1] = lowScore[0];
170
I[1] = I[0];
171
lowScore[0] = score;
172
I[0] = i;
173
}
174
else if (score < lowScore[1]) {
175
lowScore[1] = score;
176
I[1] = i;
179
if (lowScore[1] >= lowScore[0] * 1.2)
180
return [ qwDictionary[I[0]].word ];
181
else
182
return [ qwDictionary[I[0]].word, qwDictionary[I[1]].word ];
183
}
185
function computeBigramConflicts() {
186
var N = 5000;
187
var conflicts = [];
188
for (let b = 0 ; b < bigramCount.length ; b++) {
189
190
let c = [];
191
for (let l = 0 ; l < 26 ; l++)
192
c.push(0);
193
conflicts.push(c);
194
195
let bigram = bigramCount[b][0];
196
197
for (let l = 0 ; l < 26 ; l++) {
198
let ch = String.fromCharCode(97 + l);
199
if (bigram.indexOf(ch) < 0)
200
for (let w1 = 0 ; w1 < N ; w1++) {
201
let word1 = wordList[w1];
202
let i = word1.indexOf(bigram);
203
if (i >= 0) {
204
let word2 = word1.substring(0, i) + ch + word1.substring(i+2, word1.length);
205
for (let w2 = 0 ; w2 < N ; w2++)
206
if (wordList[w2] === word2)
207
c[l]++;
208
}
209
}
210
}
212
return conflicts;
213
}
214
/*
215
var conflicts = computeBigramConflicts();
216
217
var str = 'var conflicts = [\n';
218
for (let n = 0 ; n < bigramCount.length ; n++) {
219
str += ' [\'' + bigramCount[n][0] + '\',[';
220
for (let l = 0 ; l < 26 ; l++) {
221
let s = conflicts[n][l] + ',';
222
while (s.length < 3)
223
s = ' ' + s;
224
str += s;