File: jsc_checker.cpp

package info (click to toggle)
js8call 2.2.0%2Bds-5
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, trixie
  • size: 22,416 kB
  • sloc: cpp: 563,285; f90: 9,265; ansic: 937; python: 132; sh: 93; makefile: 7
file content (240 lines) | stat: -rw-r--r-- 7,095 bytes parent folder | download | duplicates (3)
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
/**
 * This file is part of JS8Call.
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <https://www.gnu.org/licenses/>.
 *
 * (C) 2018 Jordan Sherer <kn4crd@gmail.com> - All Rights Reserved
 *
 **/

#include "jsc_checker.h"

#include <QTextEdit>
#include <QTextBlock>
#include <QTextCursor>
#include <QTextDocument>
#include <QTextLayout>
#include <QDebug>

#include "jsc.h"
#include "varicode.h"

const int CORRECT = QTextFormat::UserProperty + 10;
const QString ALPHABET = { "ABCDEFGHIJKLMNOPQRSTUVWXYZ" };

JSCChecker::JSCChecker(QObject *parent) :
    QObject(parent)
{
}

bool cursorHasProperty(const QTextCursor &cursor, int property){
    if(property < QTextFormat::UserProperty) {
        return false;
    }
    if(cursor.charFormat().intProperty(property) == 1) {
        return true;
    }
    const QList<QTextLayout::FormatRange>& formats = cursor.block().layout()->additionalFormats();
    int pos = cursor.positionInBlock();
    foreach(const QTextLayout::FormatRange& range, formats) {
        if(pos > range.start && pos <= range.start + range.length && range.format.intProperty(property) == 1) {
            return true;
        }
    }
    return false;
}

QString nextChar(QTextCursor c){
    QTextCursor cur(c);
    cur.movePosition(QTextCursor::NextCharacter, QTextCursor::KeepAnchor);
    return cur.selectedText().toUpper();
}

bool isNumeric(QString s){
    return s.indexOf(QRegExp("^\\d+$")) == 0;
}

bool isWordChar(QString ch){
    return ch.contains(QRegExp("^\\w$"));
}

void JSCChecker::checkRange(QTextEdit* edit, int start, int end)
{
    if(end == -1){
        QTextCursor tmpCursor(edit->textCursor());
        tmpCursor.movePosition(QTextCursor::End);
        end = tmpCursor.position();
    }

    // stop contentsChange signals from being emitted due to changed charFormats
    edit->document()->blockSignals(true);

    //qDebug() << "checking range " << start << " - " << end;

    QTextCharFormat errorFmt;
    errorFmt.setFontUnderline(true);
    errorFmt.setUnderlineColor(Qt::red);
    errorFmt.setUnderlineStyle(QTextCharFormat::WaveUnderline);
    QTextCharFormat defaultFormat = QTextCharFormat();

    auto cursor = edit->textCursor();

    cursor.beginEditBlock();
    {
        cursor.setPosition(start);
        while(cursor.position() < end) {
            bool correct = false;

            cursor.movePosition(QTextCursor::EndOfWord, QTextCursor::KeepAnchor);
            if(cursor.selectedText()/*.toUpper()*/ == "@"){
                cursor.movePosition(QTextCursor::NextCharacter, QTextCursor::KeepAnchor);
                cursor.movePosition(QTextCursor::EndOfWord, QTextCursor::KeepAnchor);
            }

            if(cursorHasProperty(cursor, CORRECT)){
                correct = true;
            } else {
                QString word = cursor.selectedText().toUpper();

                // three or less is always "correct"
                if(word.length() < 4 || isNumeric(word)){
                    correct = true;
                } else {
                    bool found = false;
                    quint32 index = JSC::lookup(word, &found);
                    if(found){
                        correct = JSC::map[index].size == word.length();
                    }

                    if(!correct){
                        correct = Varicode::isValidCallsign(word, nullptr);
                    }
                }

                //qDebug() << "word" << word << "correct" << correct;
            }

            if(correct){
                QTextCharFormat fmt = cursor.charFormat();
                fmt.setFontUnderline(defaultFormat.fontUnderline());
                fmt.setUnderlineColor(defaultFormat.underlineColor());
                fmt.setUnderlineStyle(defaultFormat.underlineStyle());
                cursor.setCharFormat(fmt);
            } else {
                cursor.mergeCharFormat(errorFmt);
            }

            // Go to next word start
            //while(cursor.position() < end && !isWordChar(nextChar(cursor))){
            //    cursor.movePosition(QTextCursor::NextCharacter);
            //}
            cursor.movePosition(QTextCursor::NextCharacter);
        }
    }
    cursor.endEditBlock();

    edit->document()->blockSignals(false);
}

QSet<QString> oneEdit(QString word, bool includeAdditions, bool includeDeletions){
    QSet<QString> all;

    // 1-edit distance words (i.e., prefixed/suffixed/edited characters)
    for(int i = 0; i < 26; i++){
        if(includeAdditions){
            auto prefixed = ALPHABET.mid(i, 1) + word;
            all.insert(prefixed);

            auto suffixed = word + ALPHABET.mid(i, 1);
            all.insert(suffixed);
        }

        for(int j = 0; j < word.length(); j++){
            auto edited = word.mid(0, j) + ALPHABET.mid(i, 1) + word.mid(j + 1, word.length() - j);
            all.insert(edited);
        }
    }

    // 1-edit distance words (i.e., removed characters)
    if(includeDeletions){
        for(int j = 0; j < word.length(); j++){
            auto deleted = word.mid(0, j) + word.mid(j + 1, word.length() - j);
            all.insert(deleted);
        }
    }

    return all;
}

QMap<quint32, QString> candidates(QString word, bool includeTwoEdits){
    // one edit
    QSet<QString> one = oneEdit(word, true, true);

    // two edits
    QSet<QString> two;
    if(includeTwoEdits){
        foreach(auto w, one){
            two |= oneEdit(w, false, false);
        }
    }

    // existence check
    QMap<quint32, QString> m;

    quint32 index;
    foreach(auto w, one | two){
        if(JSC::exists(w, &index)){
            m[index] = w;
        }
    }

    return m;
}

QStringList JSCChecker::suggestions(QString word, int n, bool *pFound){
    QStringList s;

    // qDebug() << "computing suggestions for word" << word;

    QMap<quint32, QString> m;

    bool prefixFound = false;

    // lookup actual word prefix that is not a single character
    quint32 index = JSC::lookup(word, &prefixFound);
    if(prefixFound){
        auto t = JSC::map[index];
        if(t.size > 1){
            m[index] = QString::fromLatin1(t.str, t.size);
        }
    }

    // compute suggestion candidates
    m.unite(candidates(word, false));

    // return in order of probability (i.e., index rank)
    int i = 0;
    foreach(auto key, m.uniqueKeys()){
        if(i >= n){
            break;
        }
        //qDebug() << "suggest" << m[key] << key;
        s.append(m[key]);
        i++;
    }

    if(pFound) *pFound = prefixFound;

    return s;
}