Updated suggestions menu to be inline

This commit is contained in:
Jordan Sherer 2018-12-31 15:14:48 -05:00
parent 22e2700a04
commit 28e542e414
2 changed files with 64 additions and 34 deletions

View File

@ -145,6 +145,61 @@ void JSCChecker::checkRange(QTextEdit* edit, int start, int end)
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;
@ -163,38 +218,16 @@ QStringList JSCChecker::suggestions(QString word, int n, bool *pFound){
}
}
// 1-edit distance words (i.e., prefixed/suffixed/edited/removed characters)
for(int i = 0; i < 26; i++){
auto prefixed = ALPHABET.mid(i, 1) + word;
if(JSC::exists(prefixed, &index)){
m[index] = prefixed;
}
auto suffixed = word + ALPHABET.mid(i, 1);
if(JSC::exists(suffixed, &index)){
m[index] = 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);
if(JSC::exists(edited, &index)){
m[index] = edited;
}
}
}
for(int j = 0; j < word.length(); j++){
auto deleted = word.mid(0, j) + word.mid(j + 1, word.length() - j);
if(JSC::exists(deleted, &index)){
m[index] = deleted;
}
}
// compute suggestion candidates
m.unite(candidates(word, false));
// return in order of probability (i.e., index rank)
int i = 0;
foreach(auto key, m.keys()){
foreach(auto key, m.uniqueKeys()){
if(i >= n){
break;
}
qDebug() << "suggest" << m[key] << key;
s.append(m[key]);
i++;
}

View File

@ -1177,15 +1177,13 @@ MainWindow::MainWindow(QDir const& temp_directory, bool multiple,
connect(ui->extFreeTextMsgEdit, &QTableWidget::customContextMenuRequested, this, [this, clearAction2, clearActionAll, restoreAction](QPoint const &point){
QMenu * menu = new QMenu(ui->extFreeTextMsgEdit);
// spelling suggestions...
buildSuggestionsMenu(menu, ui->extFreeTextMsgEdit, point);
auto selectedCall = callsignSelected();
bool missingCallsign = selectedCall.isEmpty();
buildSuggestionsMenu(menu, ui->extFreeTextMsgEdit, point);
restoreAction->setDisabled(m_lastTxMessage.isEmpty());
menu->addAction(restoreAction);
menu->addSeparator();
auto savedMenu = menu->addMenu("Saved messages...");
buildSavedMessagesMenu(savedMenu);
@ -7455,18 +7453,17 @@ void MainWindow::buildSuggestionsMenu(QMenu *menu, QTextEdit *edit, const QPoint
return;
}
QStringList suggestions = JSCChecker::suggestions(word, 10, &found);
QStringList suggestions = JSCChecker::suggestions(word, 5, &found);
if(suggestions.isEmpty() && !found){
return;
}
auto suggestionsMenu = menu->addMenu("Suggestions...");
if(suggestions.isEmpty()){
auto a = suggestionsMenu->addAction("No suggestions");
auto a = menu->addAction("No Suggestions");
a->setDisabled(true);
} else {
foreach(auto suggestion, suggestions){
auto a = suggestionsMenu->addAction(suggestion);
auto a = menu->addAction(suggestion);
connect(a, &QAction::triggered, this, [this, edit, point, suggestion](){
auto c = edit->cursorForPosition(point);