From b843fc98e0c3a503975783f8bbd0b0f9b7e407d6 Mon Sep 17 00:00:00 2001 From: Imbus <> Date: Thu, 21 Nov 2024 07:48:45 +0100 Subject: [PATCH 1/4] Refactor --- lab2/dictionary.cc | 26 ++------------------------ lab2/word.cc | 27 +++++++++++++++++++++++++++ lab2/word.h | 6 ++++++ 3 files changed, 35 insertions(+), 24 deletions(-) diff --git a/lab2/dictionary.cc b/lab2/dictionary.cc index 1ce7578..0245fe4 100644 --- a/lab2/dictionary.cc +++ b/lab2/dictionary.cc @@ -9,7 +9,6 @@ using std::string; using std::vector; -// using std::filesystem::path; Dictionary::Dictionary() {} @@ -23,21 +22,6 @@ vector Dictionary::get_suggestions(const string &word) const { return suggestions; } -// Function to generate trigrams from a string -std::vector get_trigrams(const std::string &text) { - std::vector trigrams; - if (text.size() < 3) { - return trigrams; // Return an empty vector if the input is too short - } - - for (size_t i = 0; i <= text.size() - 3; ++i) { - trigrams.push_back( - text.substr(i, 3)); // Extract a substring of length 3 - } - - return trigrams; -} - int Dictionary::spit(path p) { std::ofstream file(p); @@ -48,13 +32,7 @@ int Dictionary::spit(path p) { for (int a = 0; a < MAXLEN; a++) { for (auto &word : words[a]) { - std::vector trias = get_trigrams(word.get_word()); - file << word << " " << trias.size(); - - for (auto tria : trias) { - file << " " << tria; - } - + file << word; file << std::endl; } } @@ -76,7 +54,7 @@ int Dictionary::slurp(path p) { while (std::getline(file, line)) { if (line.size() > MAXLEN) continue; - words[line.size()].push_back(Word(line, get_trigrams(line))); + words[line.size()].push_back(Word(line)); } file.close(); diff --git a/lab2/word.cc b/lab2/word.cc index 870d6a2..4e89ae4 100644 --- a/lab2/word.cc +++ b/lab2/word.cc @@ -1,4 +1,5 @@ #include "word.h" +#include "dictionary.h" #include #include #include @@ -10,6 +11,25 @@ Word::Word(const string &w, const vector &t) : word(w), triagrams(t) { std::sort(triagrams.begin(), triagrams.end()); } +std::vector get_trigrams(const std::string &text) { + std::vector trigrams; + if (text.size() < 3) { + return trigrams; // Return an empty vector if the input is too short + } + + for (size_t i = 0; i <= text.size() - 3; ++i) { + trigrams.push_back( + text.substr(i, 3)); // Extract a substring of length 3 + } + + return trigrams; +} + +Word::Word(const std::string &w) : word(w) { + this->triagrams = get_trigrams(w); + std::sort(triagrams.begin(), triagrams.end()); +} + string Word::get_word() const { return string(); } unsigned int Word::get_matches(const vector &t) const { @@ -27,8 +47,15 @@ std::ostream &operator<<(std::ostream &out, const Word &w) { auto space = string(" "); out << w.word; out << space; + out << w.triagrams.size(); for (const auto &tria : w.triagrams) { out << space << tria; } return out; } + +bool operator==(const Word &lhs, const Word &rhs) { + return lhs.word == rhs.word && + std::equal(lhs.triagrams.begin(), lhs.triagrams.end(), + rhs.triagrams.begin()); +} diff --git a/lab2/word.h b/lab2/word.h index 7c1fd5b..babe27e 100644 --- a/lab2/word.h +++ b/lab2/word.h @@ -11,6 +11,9 @@ class Word { /** Creates a word w with the sorted trigrams t */ Word(const std::string &w, const std::vector &t); + /** Creates a word w and derives the triagrams internally */ + Word(const std::string &w); + /** Returns the word */ std::string get_word() const; @@ -22,4 +25,7 @@ class Word { const std::string word; std::vector triagrams; friend std::ostream &operator<<(std::ostream &out, const Word &o); + friend bool operator==(const Word &lhs, const Word &rhs); }; + +bool operator==(const Word &lhs, const Word &rhs); From 5b669caddbfac239ed9a8ffc7a1e3f893366f871 Mon Sep 17 00:00:00 2001 From: Imbus <> Date: Thu, 21 Nov 2024 07:48:49 +0100 Subject: [PATCH 2/4] Contains --- lab2/dictionary.cc | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) diff --git a/lab2/dictionary.cc b/lab2/dictionary.cc index 0245fe4..6747a69 100644 --- a/lab2/dictionary.cc +++ b/lab2/dictionary.cc @@ -12,7 +12,15 @@ using std::vector; Dictionary::Dictionary() {} -bool Dictionary::contains(const string &word) const { return true; } +bool Dictionary::contains(const string &word) const { + int l = word.length(); + Word w = Word(word); + if (std::find(this->words[l].begin(), this->words[l].end(), w) != + std::end(this->words[l])) { + return true; + } + return false; +} vector Dictionary::get_suggestions(const string &word) const { vector suggestions; From c6eb4cb6ae3b98b8276f67130aa45ee7100c6dd5 Mon Sep 17 00:00:00 2001 From: Imbus <> Date: Thu, 21 Nov 2024 07:48:56 +0100 Subject: [PATCH 3/4] More picky compiler flags --- lab2/Makefile | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/lab2/Makefile b/lab2/Makefile index 4e354d7..48c8fe4 100644 --- a/lab2/Makefile +++ b/lab2/Makefile @@ -1,5 +1,6 @@ CXX = g++ -CXXFLAGS = -g3 -Werror -Wall -Wpedantic -Wunused-variable -std=c++17 +CXXFLAGS = -Wall -Wextra -Wpedantic -Wshadow -Wnon-virtual-dtor -Wold-style-cast -Wcast-align -Wunused -Woverloaded-virtual -Wconversion -Wsign-conversion -Wnull-dereference -Wdouble-promotion -Wformat=2 -std=c++17 +#CXXFLAGS += -Werror SRC = $(wildcard *.cc) OBJ = $(SRC:.cc=.o) From fe00d47e0236108359a2c034fe9a26bdee088397 Mon Sep 17 00:00:00 2001 From: Imbus <> Date: Thu, 21 Nov 2024 07:49:28 +0100 Subject: [PATCH 4/4] Ignore --- .gitignore | 2 ++ 1 file changed, 2 insertions(+) diff --git a/.gitignore b/.gitignore index 694cf30..4f6422d 100644 --- a/.gitignore +++ b/.gitignore @@ -5,3 +5,5 @@ build .cache/ words.txt compile_commands.json +lab2/edit +lab2/spell