Compare commits
No commits in common. "744d0f7a3a121c98bc9325fc09db0e761cb890e8" and "e4e27d421a0ec8fb86c2e0e88d7538a1900cf99f" have entirely different histories.
744d0f7a3a
...
e4e27d421a
10 changed files with 53 additions and 229 deletions
3
.gitignore
vendored
3
.gitignore
vendored
|
@ -2,6 +2,3 @@
|
|||
*.d
|
||||
*a.out*
|
||||
build
|
||||
.cache/
|
||||
words.txt
|
||||
compile_commands.json
|
||||
|
|
2
Makefile
2
Makefile
|
@ -1,2 +0,0 @@
|
|||
format:
|
||||
find . -regex '.*\.\(c\|cpp\|cc\|cxx\|h\|hpp\|hh\|hxx\)' -exec clang-format {} +
|
|
@ -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
|
|
@ -1,82 +1,22 @@
|
|||
#include "dictionary.h"
|
||||
#include "word.h"
|
||||
#include <algorithm>
|
||||
#include <filesystem>
|
||||
#include <fstream>
|
||||
#include <iostream>
|
||||
#include <string>
|
||||
#include <vector>
|
||||
#include <fstream>
|
||||
#include <iostream>
|
||||
#include <algorithm>
|
||||
#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<string> Dictionary::get_suggestions(const string &word) const {
|
||||
vector<string> 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<std::string> get_trigrams(const std::string &text) {
|
||||
std::vector<std::string> 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<std::string> 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<string> Dictionary::get_suggestions(const string& word) const {
|
||||
vector<string> suggestions;
|
||||
return suggestions;
|
||||
}
|
||||
|
|
|
@ -1,24 +1,15 @@
|
|||
#ifndef DICTIONARY_H
|
||||
#define DICTIONARY_H
|
||||
|
||||
#include "word.h"
|
||||
#include <filesystem>
|
||||
#include <string>
|
||||
#include <vector>
|
||||
|
||||
using std::vector;
|
||||
using std::filesystem::path;
|
||||
|
||||
class Dictionary {
|
||||
public:
|
||||
Dictionary();
|
||||
bool contains(const std::string &word) const;
|
||||
std::vector<std::string> get_suggestions(const std::string &word) const;
|
||||
int slurp(path p);
|
||||
int spit(path p);
|
||||
|
||||
private:
|
||||
vector<Word> words[25];
|
||||
public:
|
||||
Dictionary();
|
||||
bool contains(const std::string& word) const;
|
||||
std::vector<std::string> get_suggestions(const std::string& word) const;
|
||||
private:
|
||||
};
|
||||
|
||||
#endif
|
||||
|
|
|
@ -1,35 +0,0 @@
|
|||
#include <iostream>
|
||||
#include <vector>
|
||||
#include <string>
|
||||
#include <algorithm>
|
||||
|
||||
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<std::vector<int>> dp(m + 1, std::vector<int>(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];
|
||||
}
|
|
@ -1,18 +0,0 @@
|
|||
#include <algorithm>
|
||||
#include <iostream>
|
||||
#include <string>
|
||||
#include <vector>
|
||||
|
||||
/**
|
||||
* @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);
|
|
@ -1,17 +1,18 @@
|
|||
#include "dictionary.h"
|
||||
#include <cctype>
|
||||
#include <filesystem>
|
||||
#include <iostream>
|
||||
#include <string>
|
||||
#include <algorithm>
|
||||
#include <vector>
|
||||
#include <cctype>
|
||||
#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;
|
||||
}
|
||||
|
|
34
lab2/word.cc
34
lab2/word.cc
|
@ -1,34 +1,16 @@
|
|||
#include "word.h"
|
||||
#include <algorithm>
|
||||
#include <string>
|
||||
#include <vector>
|
||||
#include "word.h"
|
||||
|
||||
using std::string;
|
||||
using std::vector;
|
||||
using std::string;
|
||||
|
||||
Word::Word(const string &w, const vector<string> &t) : word(w), triagrams(t) {
|
||||
std::sort(triagrams.begin(), triagrams.end());
|
||||
Word::Word(const string& w, const vector<string>& t) {}
|
||||
|
||||
string Word::get_word() const {
|
||||
return string();
|
||||
}
|
||||
|
||||
string Word::get_word() const { return string(); }
|
||||
|
||||
unsigned int Word::get_matches(const vector<string> &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<string>& t) const {
|
||||
return 0;
|
||||
}
|
||||
|
|
34
lab2/word.h
34
lab2/word.h
|
@ -1,25 +1,21 @@
|
|||
#pragma once
|
||||
#ifndef WORD_H
|
||||
#define WORD_H
|
||||
|
||||
#include <string>
|
||||
#include <vector>
|
||||
|
||||
/*
|
||||
* 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<std::string> &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<std::string> &t) const;
|
||||
|
||||
private:
|
||||
const std::string word;
|
||||
const std::vector<std::string> 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<std::string>& 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<std::string>& t) const;
|
||||
private:
|
||||
};
|
||||
|
||||
#endif
|
||||
|
|
Loading…
Reference in a new issue