diff --git a/lab2/Makefile b/lab2/Makefile index 0b87009..48c8fe4 100644 --- a/lab2/Makefile +++ b/lab2/Makefile @@ -3,7 +3,6 @@ CXXFLAGS = -Wall -Wextra -Wpedantic -Wshadow -Wnon-virtual-dtor -Wold-style-cast #CXXFLAGS += -Werror SRC = $(wildcard *.cc) -HDR = $(wildcard *.h) OBJ = $(SRC:.cc=.o) all: spell edit $(OBJ) @@ -20,18 +19,7 @@ spell: spell.o word.o dictionary.o @echo "Building $@" @$(CXX) -c $(CXXFLAGS) $< -o $@ -lint: clang-tidy cppcheck clang-format - -clang-tidy: - clang-tidy $(SRC) -- $(CXXFLAGS) - -cppcheck: - cppcheck --enable=all --language=c++ --std=c++17 --suppress=missingIncludeSystem -I/usr/include $(SRC) $(HDR) - -clang-format: - clang-format -i $(SRC) $(HDR) - clean: rm -f *.o spell edit -.PHONY: clean all lint clang-tidy cppcheck clang-format +.PHONY: clean diff --git a/lab2/dictionary.cc b/lab2/dictionary.cc index dc97b04..6747a69 100644 --- a/lab2/dictionary.cc +++ b/lab2/dictionary.cc @@ -1,9 +1,9 @@ #include "dictionary.h" #include "word.h" #include +#include #include #include -#include #include #include @@ -13,7 +13,7 @@ using std::vector; Dictionary::Dictionary() {} bool Dictionary::contains(const string &word) const { - auto l = word.length(); + 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])) { @@ -22,77 +22,14 @@ bool Dictionary::contains(const string &word) const { return false; } -std::vector Dictionary::get_suggestions(const string &word) const { +vector Dictionary::get_suggestions(const string &word) const { vector suggestions; - add_trigram_suggestions(suggestions, word); - rank_suggestions(suggestions, word); - trim_suggestions(suggestions, word); + // add_trigram_suggestions(suggestions, word); + // rank_suggestions(suggestions, word); + // trim_suggestions(suggestions); return suggestions; } -void Dictionary::add_trigram_suggestions(std::vector &suggestions, - const std::string &word) const { - // Get trigrams of the input word - Word input_word(word); - const std::vector &input_trigrams = input_word.get_triagrams(); - - // Iterate through all words in the dictionary - for (int i = 0; i < MAXLEN; ++i) { - for (const Word &dict_word : words[i]) { - // Get the trigrams of the dictionary word - const std::vector &dict_word_trigrams = - dict_word.get_triagrams(); - - // Count how many trigrams match - unsigned int match_count = dict_word.get_matches(input_trigrams); - - // If there are any matches, add the word to suggestions - if (match_count > 0) { - suggestions.push_back(dict_word.get_word()); - } - } - } -} - -void Dictionary::rank_suggestions(std::vector &suggestions, - const std::string &word) const { - // Get trigrams of the input word - Word input_word(word); - const std::vector &input_trigrams = input_word.get_triagrams(); - - // Sort suggestions based on the number of matching trigrams - std::sort(suggestions.begin(), suggestions.end(), - [&](const std::string &a, const std::string &b) { - Word word_a(a); - Word word_b(b); - unsigned int match_a = word_a.get_matches(input_trigrams); - unsigned int match_b = word_b.get_matches(input_trigrams); - return match_a > - match_b; // Sort in descending order of match count - }); -} - -void Dictionary::trim_suggestions(std::vector &suggestions, - const std::string &word) const { - // Remove duplicates using a set - std::set unique_suggestions(suggestions.begin(), - suggestions.end()); - suggestions.assign(unique_suggestions.begin(), unique_suggestions.end()); - - // Remove the input word from the suggestions list (if present) - suggestions.erase(std::remove(suggestions.begin(), suggestions.end(), word), - suggestions.end()); - - // Example: Remove any suggestions that are too short - suggestions.erase( - std::remove_if(suggestions.begin(), suggestions.end(), - [](const std::string &s) { - return s.length() < - 3; // Remove words shorter than 3 characters - }), - suggestions.end()); -} - int Dictionary::spit(path p) { std::ofstream file(p); @@ -123,9 +60,9 @@ int Dictionary::slurp(path p) { std::string line; while (std::getline(file, line)) { - // Words larger than max gets placed in the topmost bucket - words[std::min(line.size(), static_cast(MAXLEN) - 1)].push_back( - Word(line)); + if (line.size() > MAXLEN) + continue; + words[line.size()].push_back(Word(line)); } file.close(); diff --git a/lab2/dictionary.h b/lab2/dictionary.h index 31eada4..a9c5519 100644 --- a/lab2/dictionary.h +++ b/lab2/dictionary.h @@ -1,4 +1,5 @@ -#pragma once +#ifndef DICTIONARY_H +#define DICTIONARY_H #include "word.h" #include @@ -7,23 +8,19 @@ #define MAXLEN 30 -// using std::vector; +using std::vector; using std::filesystem::path; class Dictionary { public: Dictionary(); - void add_trigram_suggestions(std::vector &suggestions, - const std::string &word) const; - void rank_suggestions(std::vector &suggestions, - const std::string &word) const; - void trim_suggestions(std::vector &suggestions, - const std::string &word) const; bool contains(const std::string &word) const; std::vector get_suggestions(const std::string &word) const; int slurp(path p); int spit(path p); private: - std::vector words[MAXLEN]; + vector words[MAXLEN]; }; + +#endif diff --git a/lab2/edit_distance.cc b/lab2/edit_distance.cc index 7f635c9..905aeb4 100644 --- a/lab2/edit_distance.cc +++ b/lab2/edit_distance.cc @@ -1,13 +1,14 @@ -#include -#include +#include #include +#include +#include -int edit_distance(const std::string &s1, const std::string &s2) { +int edit_distance(const std::string& s1, const std::string& s2) { size_t m = s1.size(); size_t n = s2.size(); // Create a 2D DP table - std::vector> dp(m + 1, std::vector(n + 1)); + std::vector> dp(m + 1, std::vector(n + 1)); // Fill the base cases for (size_t i = 0; i <= m; ++i) @@ -22,14 +23,13 @@ int edit_distance(const std::string &s1, const std::string &s2) { if (s1[i - 1] == s2[j - 1]) { dp[i][j] = dp[i - 1][j - 1]; // No operation needed } else { - dp[i][j] = 1 + std::min({ - dp[i - 1][j], // Deletion - dp[i][j - 1], // Insertion - dp[i - 1][j - 1] // Substitution - }); + dp[i][j] = 1 + std::min({dp[i - 1][j], // Deletion + dp[i][j - 1], // Insertion + dp[i - 1][j - 1] // Substitution + }); } } } - return static_cast(dp[m][n]); + return dp[m][n]; } diff --git a/lab2/edit_distance.h b/lab2/edit_distance.h index 6ddc48a..71765ad 100644 --- a/lab2/edit_distance.h +++ b/lab2/edit_distance.h @@ -1,17 +1,18 @@ +#include +#include #include +#include /** * @brief Computes the edit distance (Levenshtein distance) between two strings. * * The edit distance is defined as the minimum number of single-character edits - * (insertions, deletions, or substitutions) required to transform one string - * into the other. + * (insertions, deletions, or substitutions) required to transform one string into the other. * - * This implementation uses dynamic programming to compute the distance - * efficiently. + * This implementation uses dynamic programming to compute the distance efficiently. * * @param s1 The first string. * @param s2 The second string. * @return The edit distance between the two strings. */ -int edit_distance(const std::string &s1, const std::string &s2); +int edit_distance(const std::string& s1, const std::string& s2); diff --git a/lab2/test_edit_distance.cc b/lab2/test_edit_distance.cc index 982badf..1f44719 100644 --- a/lab2/test_edit_distance.cc +++ b/lab2/test_edit_distance.cc @@ -8,7 +8,8 @@ #include -bool do_test(const std::string &x, const std::string &y, int expected) { +bool do_test(const std::string& x, const std::string& y, int expected) +{ auto actual = edit_distance(x, y); if (actual != expected) { std::cout << "*** WRONG: distance(" << x << ", " << y << ") was " @@ -18,7 +19,8 @@ bool do_test(const std::string &x, const std::string &y, int expected) { return false; } -int main() { +int main() +{ int res = do_test("foobar", "foobar", 0); res += do_test("x", "x", 0); res += do_test("baz", "bar", 1); diff --git a/lab2/word.cc b/lab2/word.cc index c1f73f6..4e89ae4 100644 --- a/lab2/word.cc +++ b/lab2/word.cc @@ -32,10 +32,6 @@ Word::Word(const std::string &w) : word(w) { string Word::get_word() const { return string(); } -vector Word::get_triagrams() const { - return triagrams; -} - unsigned int Word::get_matches(const vector &t) const { unsigned int matches = 0; diff --git a/lab2/word.h b/lab2/word.h index eb927a2..babe27e 100644 --- a/lab2/word.h +++ b/lab2/word.h @@ -17,9 +17,6 @@ class Word { /** Returns the word */ std::string get_word() const; - /** Returns triagrams */ - std::vector get_triagrams() const; - /** Returns how many of the trigrams in t that are present in this word's trigram vector */ unsigned int get_matches(const std::vector &t) const;