diff --git a/.gitignore b/.gitignore index 694cf30..903ddae 100644 --- a/.gitignore +++ b/.gitignore @@ -2,6 +2,3 @@ *.d *a.out* build -.cache/ -words.txt -compile_commands.json diff --git a/Makefile b/Makefile deleted file mode 100644 index 51257f5..0000000 --- a/Makefile +++ /dev/null @@ -1,2 +0,0 @@ -format: - find . -regex '.*\.\(c\|cpp\|cc\|cxx\|h\|hpp\|hh\|hxx\)' -exec clang-format {} + diff --git a/lab2/Makefile b/lab2/Makefile deleted file mode 100644 index 4e354d7..0000000 --- a/lab2/Makefile +++ /dev/null @@ -1,24 +0,0 @@ -CXX = g++ -CXXFLAGS = -g3 -Werror -Wall -Wpedantic -Wunused-variable -std=c++17 - -SRC = $(wildcard *.cc) -OBJ = $(SRC:.cc=.o) - -all: spell edit $(OBJ) - -edit: test_edit_distance.o edit_distance.o - @echo "Building & linking $@" - @$(CXX) $(CXXFLAGS) $^ -o $@ - -spell: spell.o word.o dictionary.o - @echo "Building & linking $@" - @$(CXX) $(CXXFLAGS) $^ -o $@ - -%.o:%.cc - @echo "Building $@" - @$(CXX) -c $(CXXFLAGS) $< -o $@ - -clean: - rm -f *.o spell edit - -.PHONY: clean diff --git a/lab2/dictionary.cc b/lab2/dictionary.cc index d4658e8..79bb69e 100644 --- a/lab2/dictionary.cc +++ b/lab2/dictionary.cc @@ -1,82 +1,22 @@ -#include "dictionary.h" -#include "word.h" -#include -#include -#include -#include #include #include +#include +#include +#include +#include "word.h" +#include "dictionary.h" using std::string; using std::vector; -// using std::filesystem::path; -Dictionary::Dictionary() {} - -bool Dictionary::contains(const string &word) const { return true; } - -vector Dictionary::get_suggestions(const string &word) const { - vector suggestions; - // add_trigram_suggestions(suggestions, word); - // rank_suggestions(suggestions, word); - // trim_suggestions(suggestions); - return suggestions; +Dictionary::Dictionary() { } -// 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; +bool Dictionary::contains(const string& word) const { + return true; } -int Dictionary::spit(path p) { - std::ofstream file(p); - - if (!file.is_open()) { - std::cerr << "Error opening file! " << std::endl; - return 1; - } - - for (int a = 0; a < 25; 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 << std::endl; - } - } - - file.flush(); - file.close(); - return 0; -} - -int Dictionary::slurp(path p) { - std::ifstream file(p.string()); - - if (!file.is_open()) { - std::cerr << "Error opening file! " << std::endl; - return 1; - } - - std::string line; - while (std::getline(file, line)) { - words[line.size()].push_back(Word(line, get_trigrams(line))); - } - - file.close(); - return 0; +vector Dictionary::get_suggestions(const string& word) const { + vector suggestions; + return suggestions; } diff --git a/lab2/dictionary.h b/lab2/dictionary.h index e543011..b56f143 100644 --- a/lab2/dictionary.h +++ b/lab2/dictionary.h @@ -1,24 +1,15 @@ #ifndef DICTIONARY_H #define DICTIONARY_H -#include "word.h" -#include #include #include -using std::vector; -using std::filesystem::path; - class Dictionary { - public: - Dictionary(); - 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: - vector words[25]; +public: + Dictionary(); + bool contains(const std::string& word) const; + std::vector get_suggestions(const std::string& word) const; +private: }; #endif diff --git a/lab2/edit_distance.cc b/lab2/edit_distance.cc deleted file mode 100644 index 905aeb4..0000000 --- a/lab2/edit_distance.cc +++ /dev/null @@ -1,35 +0,0 @@ -#include -#include -#include -#include - -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)); - - // Fill the base cases - for (size_t i = 0; i <= m; ++i) - dp[i][0] = i; // Deletion cost - - for (size_t j = 0; j <= n; ++j) - dp[0][j] = j; // Insertion cost - - // Fill the DP table - for (size_t i = 1; i <= m; ++i) { - for (size_t j = 1; j <= n; ++j) { - 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 - }); - } - } - } - - return dp[m][n]; -} diff --git a/lab2/edit_distance.h b/lab2/edit_distance.h deleted file mode 100644 index 71765ad..0000000 --- a/lab2/edit_distance.h +++ /dev/null @@ -1,18 +0,0 @@ -#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. - * - * 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); diff --git a/lab2/spell.cc b/lab2/spell.cc index 04c3d39..dfb820b 100644 --- a/lab2/spell.cc +++ b/lab2/spell.cc @@ -1,17 +1,18 @@ -#include "dictionary.h" -#include -#include #include #include +#include #include +#include +#include "dictionary.h" +using std::string; +using std::vector; using std::cin; using std::cout; using std::endl; -using std::string; -using std::vector; -void check_word(const string &word, const Dictionary &dict) { +void check_word(const string& word, const Dictionary& dict) +{ if (dict.contains(word)) { cout << "Correct." << endl; } else { @@ -20,22 +21,18 @@ void check_word(const string &word, const Dictionary &dict) { cout << "Wrong, no suggestions." << endl; } else { cout << "Wrong. Suggestions:" << endl; - for (const auto &w : suggestions) { + for (const auto& w : suggestions) { cout << " " << w << endl; } } } } - int main() { - Dictionary dict; - string word; - dict.slurp(std::filesystem::path("/usr/share/dict/words")); - // dict.spit(std::filesystem::path("words.txt")); - - // while (cin >> word) { - // transform(word.begin(), word.end(), word.begin(), ::tolower); - // check_word(word, dict); - // } + Dictionary dict; + string word; + while (cin >> word) { + transform(word.begin(), word.end(), word.begin(), ::tolower); + check_word(word, dict); + } return 0; } diff --git a/lab2/word.cc b/lab2/word.cc index 870d6a2..20b9417 100644 --- a/lab2/word.cc +++ b/lab2/word.cc @@ -1,34 +1,16 @@ -#include "word.h" -#include #include #include +#include "word.h" -using std::string; using std::vector; +using std::string; -Word::Word(const string &w, const vector &t) : word(w), triagrams(t) { - std::sort(triagrams.begin(), triagrams.end()); +Word::Word(const string& w, const vector& t) {} + +string Word::get_word() const { + return string(); } -string Word::get_word() const { return string(); } - -unsigned int Word::get_matches(const vector &t) const { - unsigned int matches = 0; - - for (const auto &triagram : t) { - if (std::binary_search(triagrams.begin(), triagrams.end(), triagram)) { - ++matches; - } - } - return matches; -} - -std::ostream &operator<<(std::ostream &out, const Word &w) { - auto space = string(" "); - out << w.word; - out << space; - for (const auto &tria : w.triagrams) { - out << space << tria; - } - return out; +unsigned int Word::get_matches(const vector& t) const { + return 0; } diff --git a/lab2/word.h b/lab2/word.h index 7d07c8b..9fb0716 100644 --- a/lab2/word.h +++ b/lab2/word.h @@ -1,25 +1,21 @@ -#pragma once +#ifndef WORD_H +#define WORD_H #include #include -/* - * Contains a word and its triagrams - */ class Word { - public: - /** Creates a word w with the sorted trigrams t */ - Word(const std::string &w, const std::vector &t); - - /** Returns the word */ - std::string get_word() 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; - - private: - const std::string word; - const std::vector triagrams; - friend std::ostream &operator<<(std::ostream &out, const Word &o); +public: + /* Creates a word w with the sorted trigrams t */ + Word(const std::string& w, const std::vector& t); + + /* Returns the word */ + std::string get_word() 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; +private: }; + +#endif