summaryrefslogtreecommitdiff
path: root/inputmethods/handwriting/qimpenmatch.cpp
blob: a0448b68c54a80333ca6cc7b3d45358589f77a19 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
/**********************************************************************
** Copyright (C) 2000 Trolltech AS.  All rights reserved.
**
** This file is part of Qtopia Environment.
**
** This file may be distributed and/or modified under the terms of the
** GNU General Public License version 2 as published by the Free Software
** Foundation and appearing in the file LICENSE.GPL included in the
** packaging of this file.
**
** This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
** WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
**
** See http://www.trolltech.com/gpl/ for GPL licensing information.
**
** Contact info@trolltech.com if any conditions of this licensing are
** not clear to you.
**
**********************************************************************/

#include "qimpenmatch.h"

#include <qpe/qdawg.h>
#include <qpe/global.h>

#include <qapplication.h>
#include <qtimer.h>
#include <opie2/odebug.h>

#include <limits.h>

#define ERROR_THRESHOLD	    200000
#define LOOKAHEAD_ERROR	    2500
//#define DEBUG_QIMPEN

QIMPenMatch::QIMPenMatch( QObject *parent, const char *name )
    : QObject( parent, name )
{
    strokes.setAutoDelete( TRUE );
    wordChars.setAutoDelete( TRUE );
    wordMatches.setAutoDelete( TRUE );

    multiTimer = new QTimer( this );
    connect( multiTimer, SIGNAL(timeout()), this, SLOT(endMulti()) );

    prevMatchChar = 0;
    prevMatchError = INT_MAX;
    charSet = 0;
    multiCharSet = 0;
    multiTimeout = 500;
    canErase = FALSE;
    doWordMatching = true;
}

QIMPenMatch::~QIMPenMatch()
{
}

void QIMPenMatch::setCharSet( QIMPenCharSet *cs )
{
    charSet = cs;
}

void QIMPenMatch::beginStroke()
{
    multiTimer->stop();
}

void QIMPenMatch::strokeEntered( QIMPenStroke *st )
{
#ifdef DEBUG_QIMPEN
    odebug << "---------- new stroke -------------" << oendl;
#endif
    strokes.append( new QIMPenStroke( *st ) );

    QIMPenChar testChar;
    QIMPenStrokeIterator it(strokes);
    for ( ; it.current(); ++it ) {
	testChar.addStroke( it.current() );
    }

    QIMPenCharMatchList ml;
    if ( strokes.count() > 1 && multiCharSet ) {
#ifdef DEBUG_QIMPEN
	odebug << "Matching against multi set" << oendl;
#endif
	ml = multiCharSet->match( &testChar );
    } else {
#ifdef DEBUG_QIMPEN
	odebug << "Matching against single set" << oendl;
#endif
	ml = charSet->match( &testChar );
    }

    processMatches( ml );
}

void QIMPenMatch::processMatches( QIMPenCharMatchList &ml )
{
#ifdef DEBUG_QIMPEN
    odebug << "Entering strokes.count() = " <<  strokes.count() << oendl;
#endif
    QIMPenCharMatch candidate1 = { INT_MAX, 0 };
    QIMPenCharMatch candidate2 = { INT_MAX, 0 };
    QIMPenCharMatchList ml2;

    if ( ml.count() ) {//&&
//	 ml.first().penChar->penStrokes().count() == strokes.count() ) {
	candidate1 = ml.first();
#ifdef DEBUG_QIMPEN
	odebug << "Candidate1 = " << candidate1.penChar->character() << oendl;
#endif
    }

    if ( strokes.count() > 1 ) {
	// See if the last stroke can match a new character
	QIMPenChar testChar;
	QIMPenStroke *st = strokes.at(strokes.count()-1);
	testChar.addStroke( st );
	ml2 = charSet->match( &testChar );
	if ( ml2.count() ) {
	    candidate2 = ml2.first();
#ifdef DEBUG_QIMPEN
	    odebug << "Candidate2 = " << candidate2.penChar->character() << oendl;
#endif
	}
    }

    bool eraseLast = FALSE;
    bool output = TRUE;

    if ( candidate1.penChar && candidate2.penChar ) {
	// Hmmm, a multi-stroke or a new character are both possible.
	// Bias the multi-stroke case.
	if ( QMAX(candidate2.error, prevMatchError)*3 < candidate1.error ) {
	    int i = strokes.count()-1;
	    while ( i-- ) {
		strokes.removeFirst();
		emit removeStroke();
	    }
	    prevMatchChar = candidate2.penChar;
	    prevMatchError = candidate2.error;
	    multiCharSet = charSet;
	    ml = ml2;
#ifdef DEBUG_QIMPEN
	    odebug << "** Using Candidate2" << oendl;
#endif
	} else {
	    if ( (prevMatchChar->character() >> 16) != Qt::Key_Backspace &&
		 (prevMatchChar->character() >> 16) < QIMPenChar::ModeBase )
		eraseLast = TRUE;
	    prevMatchChar = candidate1.penChar;
	    prevMatchError = candidate1.error;
#ifdef DEBUG_QIMPEN
	    odebug << "** Using Candidate1, with erase" << oendl;
#endif
	}
    } else if ( candidate1.penChar ) {
	if ( strokes.count() != 1 )
	    eraseLast = TRUE;
	else
	    multiCharSet = charSet;
	prevMatchChar = candidate1.penChar;
	prevMatchError = candidate1.error;
#ifdef DEBUG_QIMPEN
	odebug << "** Using Candidate1" << oendl;
#endif
    } else if ( candidate2.penChar ) {
	int i = strokes.count()-1;
	while ( i-- ) {
	    strokes.removeFirst();
	    emit removeStroke();
	}
	prevMatchChar = candidate2.penChar;
	prevMatchError = candidate2.error;
	multiCharSet = charSet;
	ml = ml2;
#ifdef DEBUG_QIMPEN
	odebug << "** Using Candidate2" << oendl;
#endif
    } else {
	if ( !ml.count() ) {
#ifdef DEBUG_QIMPEN
	    odebug << "** Failed" << oendl; 
#endif
	    canErase = FALSE;
	} else {
#ifdef DEBUG_QIMPEN
	    odebug << "Need more strokes" << oendl;
#endif
	    if ( strokes.count() == 1 )
		canErase = FALSE;
	    multiCharSet = charSet;
	}
	output = FALSE;
	emit noMatch();
    }

    if ( eraseLast && canErase ) {
#ifdef DEBUG_QIMPEN
	odebug << "deleting last" << oendl;
#endif
	emit erase();
	wordChars.removeLast();
	wordEntered.truncate( wordEntered.length() - 1 );
    }

    if ( output ) {
	emit matchedCharacters( ml );
	uint code = prevMatchChar->character() >> 16;
	if ( code < QIMPenChar::ModeBase ) {
	    updateWordMatch( ml );
	    emit keypress( prevMatchChar->character() );
	}
	canErase = TRUE;
    }

    if ( strokes.count() )
	multiTimer->start( multiTimeout, TRUE );
}

void QIMPenMatch::updateWordMatch( QIMPenCharMatchList &ml )
{
    if ( !ml.count() || !doWordMatching )
	return;
    int ch = ml.first().penChar->character();
    QChar qch( ch );
    int code = ch >> 16;
    if ( qch.isPunct() || qch.isSpace() ||
	 code == Qt::Key_Enter || code == Qt::Key_Return ||
	 code == Qt::Key_Tab || code == Qt::Key_Escape ) {
//	odebug << "Word Matching: Clearing word" << oendl;
	wordChars.clear();
	wordMatches.clear();
	wordEntered = QString();
    } else if ( code == Qt::Key_Backspace ) {
	//odebug << "Word Matching: Handle backspace" << oendl;
	wordChars.removeLast();
	wordEntered.truncate( wordEntered.length() - 1 );
	matchWords();
    } else {
	QIMPenChar *matchCh;

	wordChars.append( new QIMPenCharMatchList() );
	wordEntered += ml.first().penChar->character();

	QIMPenCharMatchList::Iterator it;
	for ( it = ml.begin(); it != ml.end(); ++it ) {
	    matchCh = (*it).penChar;

	    if ( matchCh->penStrokes().count() == strokes.count() ) {
		QChar ch(matchCh->character());
		if ( !ch.isPunct() && !ch.isSpace() ) {
		    wordChars.last()->append( QIMPenCharMatch( (*it) ) );
		}
	    }
	}
	matchWords();
    }
    if ( !wordMatches.count() || wordMatches.getFirst()->word != wordEntered )
	wordMatches.prepend( new MatchWord( wordEntered, 0 ) );
    emit matchedWords( wordMatches );
}

void QIMPenMatch::matchWords()
{
    if ( wordEntered.length() > 0 ) {
	// more leaniency if we don't have many matches
	if ( badMatches < 200 )
	    errorThreshold += (200 - badMatches) * 100;
    } else
	errorThreshold = ERROR_THRESHOLD;
    wordMatches.clear();
    goodMatches = 0;
    badMatches = 0;
    if ( wordChars.count() > 0 ) {
	maxGuess = (int)wordChars.count() * 2;
	if ( maxGuess < 3 )
	    maxGuess = 3;
	QString str;
	scanDict( Global::fixedDawg().root(), 0, str, 0 );
/*
	QListIterator<MatchWord> it( wordMatches);
	for ( ; it.current(); ++it ) {
	    odebug << "Match word: " << it.current()->word << oendl;
	}
*/
    }
    //odebug << "Possibles: Good " << goodMatches << ", total " << wordMatches.count() << oendl;
    wordMatches.sort();
}

void QIMPenMatch::scanDict( const QDawg::Node* n, int ipos, const QString& str, int error )
{
    if ( !n )
	return;
    if ( error / (ipos+1) > errorThreshold )
	return;

    while (n) {
	if ( goodMatches > 20 )
	    break;
	if ( ipos < (int)wordChars.count() ) {
	    int i;
	    QChar testCh = QChar(n->letter());
	    QIMPenCharMatchList::Iterator it;
	    for ( i = 0, it = wordChars.at(ipos)->begin();
		  it != wordChars.at(ipos)->end() && i < 8; ++it, i++ ) {
		QChar ch( (*it).penChar->character() );
		if ( ch == testCh || ( !ipos && ch.lower() == testCh.lower() ) ) {
		    int newerr =  error + (*it).error;
		    if ( testCh.category() == QChar::Letter_Uppercase )
			ch = testCh;
		    QString newstr( str + ch );
		    if ( n->isWord() && ipos == (int)wordChars.count() - 1 ) {
			wordMatches.append( new MatchWord( newstr, newerr ) );
			goodMatches++;
		    }
		    scanDict( n->jump(), ipos+1, newstr, newerr );
		}
	    }
	} else if ( badMatches < 200 && ipos < maxGuess ) {
	    int d = ipos - wordChars.count();
	    int newerr = error + ERROR_THRESHOLD + LOOKAHEAD_ERROR*d;
	    QString newstr( str + n->letter() );
	    if ( n->isWord() ) {
		wordMatches.append( new MatchWord( newstr, newerr ) );
		badMatches++;
	    }
	    scanDict( n->jump(), ipos+1, newstr, newerr );
	}
	n = n->next();
    }
}

void QIMPenMatch::backspace()
{
    wordChars.removeLast();
    wordEntered.truncate( wordEntered.length() - 1 );
    matchWords();
    if ( !wordMatches.count() || wordMatches.getFirst()->word != wordEntered )
	wordMatches.prepend( new MatchWord( wordEntered, 0 ) );
    emit matchedWords( wordMatches );
    if ( wordEntered.length() )
	canErase = TRUE;
}

void QIMPenMatch::endMulti()
{
    int i = strokes.count();
    while ( i-- )
	emit removeStroke();
    strokes.clear();
    multiCharSet = 0;
}

void QIMPenMatch::resetState()
{
    if ( !wordEntered.isEmpty() ) {
	wordChars.clear();
	wordMatches.clear();
	wordEntered = QString();
	emit matchedWords( wordMatches );
	canErase = FALSE;
    }
}