Compare commits
	
		
			4 commits
		
	
	
		
			fdae90ad9f
			...
			fe00d47e02
		
	
	| Author | SHA1 | Date | |
|---|---|---|---|
|   | fe00d47e02 | ||
|   | c6eb4cb6ae | ||
|   | 5b669caddb | ||
|   | b843fc98e0 | 
					 5 changed files with 48 additions and 26 deletions
				
			
		
							
								
								
									
										2
									
								
								.gitignore
									
										
									
									
										vendored
									
									
								
							
							
						
						
									
										2
									
								
								.gitignore
									
										
									
									
										vendored
									
									
								
							|  | @ -5,3 +5,5 @@ build | ||||||
| .cache/ | .cache/ | ||||||
| words.txt | words.txt | ||||||
| compile_commands.json | compile_commands.json | ||||||
|  | lab2/edit | ||||||
|  | lab2/spell | ||||||
|  |  | ||||||
|  | @ -1,5 +1,6 @@ | ||||||
| CXX = g++ | 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) | SRC = $(wildcard *.cc) | ||||||
| OBJ = $(SRC:.cc=.o) | OBJ = $(SRC:.cc=.o) | ||||||
|  |  | ||||||
|  | @ -9,11 +9,18 @@ | ||||||
| 
 | 
 | ||||||
| using std::string; | using std::string; | ||||||
| using std::vector; | using std::vector; | ||||||
| // using std::filesystem::path;
 |  | ||||||
| 
 | 
 | ||||||
| Dictionary::Dictionary() {} | 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<string> Dictionary::get_suggestions(const string &word) const { | vector<string> Dictionary::get_suggestions(const string &word) const { | ||||||
|     vector<string> suggestions; |     vector<string> suggestions; | ||||||
|  | @ -23,21 +30,6 @@ vector<string> Dictionary::get_suggestions(const string &word) const { | ||||||
|     return suggestions; |     return suggestions; | ||||||
| } | } | ||||||
| 
 | 
 | ||||||
| // 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; |  | ||||||
| } |  | ||||||
| 
 |  | ||||||
| int Dictionary::spit(path p) { | int Dictionary::spit(path p) { | ||||||
|     std::ofstream file(p); |     std::ofstream file(p); | ||||||
| 
 | 
 | ||||||
|  | @ -48,13 +40,7 @@ int Dictionary::spit(path p) { | ||||||
| 
 | 
 | ||||||
|     for (int a = 0; a < MAXLEN; a++) { |     for (int a = 0; a < MAXLEN; a++) { | ||||||
|         for (auto &word : words[a]) { |         for (auto &word : words[a]) { | ||||||
|             std::vector<std::string> trias = get_trigrams(word.get_word()); |             file << word; | ||||||
|             file << word << " " << trias.size(); |  | ||||||
| 
 |  | ||||||
|             for (auto tria : trias) { |  | ||||||
|                 file << " " << tria; |  | ||||||
|             } |  | ||||||
| 
 |  | ||||||
|             file << std::endl; |             file << std::endl; | ||||||
|         } |         } | ||||||
|     } |     } | ||||||
|  | @ -76,7 +62,7 @@ int Dictionary::slurp(path p) { | ||||||
|     while (std::getline(file, line)) { |     while (std::getline(file, line)) { | ||||||
|         if (line.size() > MAXLEN) |         if (line.size() > MAXLEN) | ||||||
|             continue; |             continue; | ||||||
|         words[line.size()].push_back(Word(line, get_trigrams(line))); |         words[line.size()].push_back(Word(line)); | ||||||
|     } |     } | ||||||
| 
 | 
 | ||||||
|     file.close(); |     file.close(); | ||||||
|  |  | ||||||
							
								
								
									
										27
									
								
								lab2/word.cc
									
										
									
									
									
								
							
							
						
						
									
										27
									
								
								lab2/word.cc
									
										
									
									
									
								
							|  | @ -1,4 +1,5 @@ | ||||||
| #include "word.h" | #include "word.h" | ||||||
|  | #include "dictionary.h" | ||||||
| #include <algorithm> | #include <algorithm> | ||||||
| #include <string> | #include <string> | ||||||
| #include <vector> | #include <vector> | ||||||
|  | @ -10,6 +11,25 @@ Word::Word(const string &w, const vector<string> &t) : word(w), triagrams(t) { | ||||||
|     std::sort(triagrams.begin(), triagrams.end()); |     std::sort(triagrams.begin(), triagrams.end()); | ||||||
| } | } | ||||||
| 
 | 
 | ||||||
|  | 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; | ||||||
|  | } | ||||||
|  | 
 | ||||||
|  | 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(); } | string Word::get_word() const { return string(); } | ||||||
| 
 | 
 | ||||||
| unsigned int Word::get_matches(const vector<string> &t) const { | unsigned int Word::get_matches(const vector<string> &t) const { | ||||||
|  | @ -27,8 +47,15 @@ std::ostream &operator<<(std::ostream &out, const Word &w) { | ||||||
|     auto space = string(" "); |     auto space = string(" "); | ||||||
|     out << w.word; |     out << w.word; | ||||||
|     out << space; |     out << space; | ||||||
|  |     out << w.triagrams.size(); | ||||||
|     for (const auto &tria : w.triagrams) { |     for (const auto &tria : w.triagrams) { | ||||||
|         out << space << tria; |         out << space << tria; | ||||||
|     } |     } | ||||||
|     return out; |     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()); | ||||||
|  | } | ||||||
|  |  | ||||||
|  | @ -11,6 +11,9 @@ class Word { | ||||||
|     /** Creates a word w with the sorted trigrams t */ |     /** Creates a word w with the sorted trigrams t */ | ||||||
|     Word(const std::string &w, const std::vector<std::string> &t); |     Word(const std::string &w, const std::vector<std::string> &t); | ||||||
| 
 | 
 | ||||||
|  |     /** Creates a word w and derives the triagrams internally */ | ||||||
|  |     Word(const std::string &w); | ||||||
|  | 
 | ||||||
|     /** Returns the word */ |     /** Returns the word */ | ||||||
|     std::string get_word() const; |     std::string get_word() const; | ||||||
| 
 | 
 | ||||||
|  | @ -22,4 +25,7 @@ class Word { | ||||||
|     const std::string word; |     const std::string word; | ||||||
|     std::vector<std::string> triagrams; |     std::vector<std::string> triagrams; | ||||||
|     friend std::ostream &operator<<(std::ostream &out, const Word &o); |     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); | ||||||
|  |  | ||||||
		Loading…
	
	Add table
		Add a link
		
	
		Reference in a new issue