[Psi-Devel] [Patches] [Patch] Spell checker doesn't handle abbreviated words
Michail Pishchagin
mblsha at gmail.com
Wed Jul 16 15:30:20 PDT 2008
On 14.07.2008, at 13:34, Norman Rasmussen wrote:
>> Could you please give some examples?
>
> discussed at http://conference.psi-im.org/psi@conference.psi-im.org/2008/06/28.html
> flysprayed at http://flyspray.psi-im.org/task/859
>
> words that incorrectly highlight: doesn't shouldn't
> words that incorrectly don't highlight: can'do good'spelling
> note: can't and won't don't highlight because can and won are valid
> words
Ahhar, we need to put a unit-test around our highlighter.
>> One other problem that you approach doesn't fix is that aspell
>> erroneously highlights numbers. And there's one more problem as
>> well: we shouldn't highlight words on top of which the cursor is
>> located.
>
>> I have a private prototype patch that fixes two problems I've
>> specified, and I wanted to clean it up for inclusion in 0.13.
>
> yegh, so you might need to use: "[A-Za-z][A-Za-z']*[A-Za-z]" as the
> regex instead. Ugly Note: that this probably excludes non-latin
> charecters, which isn't desired. QtRegex doesn't easily support
> matching only letter by the looks of it. I guess you might be able
> to work some magic with zero length lookaheads, but then the regex
> starts to get too magic.
>
> I guess you could just match on "\\w[\\w']*\\w", and then check that
> "\\d" fails too, that may be the 'best' technique to follow.
This solution works for me:
rehighlightTimer_->stop();
needRehighlight_ = previousBlockState() != -1;
int cursorPosition = cursorPositionInCurrentBlock();
// Underline
QTextCharFormat tcf;
tcf.setUnderlineColor(QBrush(QColor(255,0,0)));
tcf.setUnderlineStyle(QTextCharFormat::SpellCheckUnderline);
// Match words (minimally)
static QRegExp expression("\\b\\w+\\b");
static QRegExp number("^\\d+$");
// Iterate through all words
int index = text.indexOf(expression);
while (index >= 0) {
int length = expression.matchedLength();
if (expression.cap().indexOf(number) >= 0) {
// skipping numbers
}
else if (cursorPosition >= index && (cursorPosition <= index +
length)) {
needRehighlight_ = true;
}
else {
if (!SpellChecker::instance()->isCorrect(expression.cap()))
setFormat(index, length, tcf);
}
index = text.indexOf(expression, index + length);
}
setCurrentBlockState(needRehighlight_ ? 0 : -1);
Not shown is the code that re-highlights the text when cursor moves
around the text field ;-)
-Michail
More information about the Psi-Devel
mailing list