feat: add option to load english words

This commit is contained in:
mandlm 2024-03-20 21:45:06 +01:00
parent 10b19749f7
commit dae5445efc
Signed by: mandlm
GPG Key ID: 4AA25D647AA54CC7
4 changed files with 370190 additions and 22 deletions

View File

@ -10,6 +10,7 @@
#include "tree_finder.h"
#include "word_list_generator.h"
#include <fstream>
#include <sstream>
MainWindow::MainWindow(QWidget *parent)
@ -17,10 +18,12 @@ MainWindow::MainWindow(QWidget *parent)
ui->setupUi(this);
ui->resultView->setModel(&result_model_);
ui->wordListSourceSelector->setModel(&word_list_sources_);
ui->searchAlgorithmSelector->setModel(&search_algorithms_);
setupAlgorithmSelector();
generateWordList();
setupWordListSourceSelector();
setupWordList();
}
MainWindow::~MainWindow() { delete ui; }
@ -33,6 +36,23 @@ void MainWindow::setupAlgorithmSelector() {
search_algorithms_.appendRow(new QStandardItem("Grouped search"));
}
void MainWindow::setupWordListSourceSelector() {
word_list_sources_.appendRow(new QStandardItem("All 4-caps words"));
word_list_sources_.appendRow(new QStandardItem("English words"));
}
void MainWindow::setupWordList() {
switch (ui->wordListSourceSelector->currentIndex()) {
case 0:
default:
generateWordList();
break;
case 1:
loadWordList("words.txt");
break;
}
}
void MainWindow::generateWordList() {
Timer timer;
word_list_ = WordListGenerator::generate(ui->wordListSizeSelector->value());
@ -43,6 +63,25 @@ void MainWindow::generateWordList() {
ui->mainStatusBar->showMessage(QString::fromStdString(status_message.str()));
}
void MainWindow::loadWordList(std::filesystem::path path) {
word_list_.clear();
Timer timer;
std::ifstream ifs(path);
std::string line;
while (std::getline(ifs, line)) {
for (auto i = 0; i < ui->wordListSizeSelector->value(); ++i) {
word_list_.emplace_back(line);
}
}
timer.stop();
std::stringstream status_message;
status_message << "loaded " << word_list_.size() << " words in " << timer;
ui->mainStatusBar->showMessage(QString::fromStdString(status_message.str()));
}
void MainWindow::search(const QString &search_term) {
if (search_term.isEmpty()) {
result_model_.setStringList(QStringList());
@ -108,9 +147,17 @@ void MainWindow::on_searchAlgorithmSelector_currentIndexChanged(int) {
QGuiApplication::restoreOverrideCursor();
}
void MainWindow::on_wordListSizeSelector_valueChanged(int) {
void MainWindow::on_wordListSourceSelector_currentIndexChanged(int) {
QGuiApplication::setOverrideCursor(Qt::WaitCursor);
generateWordList();
setupWordList();
createSelectedFinder();
search(ui->searchInput->displayText());
QGuiApplication::restoreOverrideCursor();
}
void MainWindow::on_wordListSizeSelector_valueChanged(int) {
QGuiApplication::setOverrideCursor(Qt::WaitCursor);
setupWordList();
createSelectedFinder();
search(ui->searchInput->displayText());
QGuiApplication::restoreOverrideCursor();

View File

@ -5,6 +5,7 @@
#include <QMainWindow>
#include <QStandardItemModel>
#include <QStringListModel>
#include <filesystem>
QT_BEGIN_NAMESPACE
namespace Ui {
@ -19,6 +20,7 @@ private:
std::vector<std::string> word_list_;
QStringListModel result_model_;
QStandardItemModel search_algorithms_;
QStandardItemModel word_list_sources_;
std::unique_ptr<Finder> finder_;
public:
@ -27,7 +29,10 @@ public:
private:
void setupAlgorithmSelector();
void setupWordListSourceSelector();
void setupWordList();
void generateWordList();
void loadWordList(std::filesystem::path path);
void search(const QString &search_term);
void createSelectedFinder();
void showResults(const std::forward_list<const std::string *> &results);
@ -35,7 +40,8 @@ private:
private slots:
void on_searchInput_textChanged(const QString &search_term);
void on_searchAlgorithmSelector_currentIndexChanged(int);
void on_wordListSizeSelector_valueChanged(int new_value);
void on_wordListSourceSelector_currentIndexChanged(int);
void on_wordListSizeSelector_valueChanged(int);
private:
Ui::MainWindow *ui;

View File

@ -35,20 +35,10 @@
<property name="sizeConstraint">
<enum>QLayout::SetDefaultConstraint</enum>
</property>
<item row="0" column="1">
<widget class="QLineEdit" name="searchInput">
<property name="sizePolicy">
<sizepolicy hsizetype="Preferred" vsizetype="Fixed">
<horstretch>0</horstretch>
<verstretch>0</verstretch>
</sizepolicy>
</property>
</widget>
</item>
<item row="1" column="0">
<widget class="QLabel" name="label_2">
<item row="3" column="0">
<widget class="QLabel" name="label_3">
<property name="text">
<string>Select search algorithm</string>
<string>&lt;html&gt;&lt;head/&gt;&lt;body&gt;&lt;p&gt;Select word-list size&lt;/p&gt;&lt;/body&gt;&lt;/html&gt;</string>
</property>
</widget>
</item>
@ -68,14 +58,17 @@
</property>
</widget>
</item>
<item row="2" column="0">
<widget class="QLabel" name="label_3">
<property name="text">
<string>&lt;html&gt;&lt;head/&gt;&lt;body&gt;&lt;p&gt;Select word-list size&lt;/p&gt;&lt;/body&gt;&lt;/html&gt;</string>
<item row="0" column="1">
<widget class="QLineEdit" name="searchInput">
<property name="sizePolicy">
<sizepolicy hsizetype="Preferred" vsizetype="Fixed">
<horstretch>0</horstretch>
<verstretch>0</verstretch>
</sizepolicy>
</property>
</widget>
</item>
<item row="2" column="1">
<item row="3" column="1">
<widget class="QSpinBox" name="wordListSizeSelector">
<property name="maximum">
<number>100</number>
@ -85,6 +78,23 @@
</property>
</widget>
</item>
<item row="1" column="0">
<widget class="QLabel" name="label_2">
<property name="text">
<string>Select search algorithm</string>
</property>
</widget>
</item>
<item row="2" column="0">
<widget class="QLabel" name="label_4">
<property name="text">
<string>Select word-list source</string>
</property>
</widget>
</item>
<item row="2" column="1">
<widget class="QComboBox" name="wordListSourceSelector"/>
</item>
</layout>
</item>
<item>

370105
words.txt Normal file

File diff suppressed because it is too large Load Diff