Storing and Translating Words

Dictionary.cpp #include "Dictionary.h" Dictionary::Dictionary() : wordHashTable(wordHash),posHashTable(posHash),EnglishWordHashTable(englishHash) { } void Dictionary::addDictionaryWord(string word, string pos, string english) { wordHashTable.add(word, pos, english); posHashTable.add(word, pos, english); EnglishWordHashTable.add(word, pos, english); } DictionaryWord Dictionary::findWord(string key) { if ((int)key.find(" ") != -1) { key = key.substr(key.find(" ") + 1); } return wordHashTable.find(key); } DictionaryWord Dictionary::findPos(string key) { if ((int)key.find("(") != -1) { key= key.substr(0, key.find("(")); } return posHashTable.find(key); } DictionaryWord Dictionary::findEnglishWord(string key) { return EnglishWordHashTable.find(key); } vector Dictionary::getAll() { DictionaryWord temp; vectorresult; temp = findPos("noun"); while (temp.next != NULL) { if (temp.partOfSpeech[0] == 'n') result.push_back(temp); temp = *temp.next; } if (temp.partOfSpeech[0] == 'n') result.push_back(temp); temp = findPos("verb"); while (temp.next != NULL) { if (temp.partOfSpeech[0] == 'v') result.push_back(temp); temp = *temp.next; } if (temp.partOfSpeech[0] == 'v') result.push_back(temp); temp = findPos("preposition"); while (temp.next != NULL) { if (temp.partOfSpeech[0] == 'p') result.push_back(temp); temp = *temp.next; } if (temp.partOfSpeech[0] == 'p') result.push_back(temp); temp = findPos("adjective"); while (temp.next != NULL) { if (temp.partOfSpeech[2] == 'j') result.push_back(temp); temp = *temp.next; } if (temp.partOfSpeech[2] == 'j') result.push_back(temp); temp = findPos("adverb"); while (temp.next != NULL) { if (temp.partOfSpeech[2] == 'v') result.push_back(temp); temp = *temp.next; } if (temp.partOfSpeech[2] == 'v') result.push_back(temp); temp = findPos("cardinal number"); while (temp.next != NULL) { if (temp.partOfSpeech[0] == 'c') result.push_back(temp); temp = *temp.next; } if (temp.partOfSpeech[0] == 'c') result.push_back(temp); return result; } Dictionary.h #pragma once #include"HashTable.h" #include class Dictionary { HashTable wordHashTable; HashTable EnglishWordHashTable; HashTable posHashTable; public: Dictionary(); void addDictionaryWord(string word,string pos,string english); DictionaryWord findWord(string key); DictionaryWord findPos(string key); DictionaryWord findEnglishWord(string key); vector getAll(); }; HashTable.cpp #include "HashTable.h" HashTable::HashTable(HashType t) { this->type = t; } void HashTable::add(string word, string pos, string english) { string tempWord = word; string tempPos = pos; string tempEnglish = english; int index = -1; switch (type) { case wordHash: if ((int)word.find(" ") != -1) { word = word.substr(word.find(" ")+1); } index = hashIndex(word); break; case posHash: if ((int)pos.find("(") != -1) { pos = pos.substr(0,pos.find("(")); } index = hashIndex(pos); break; case englishHash: index = hashIndex(english); break; } list[index].add(tempWord,tempPos,tempEnglish); } void HashTable::print() { for (int i = 0; i < 33; i++) { cout << "linkedlist of hash " + to_string(i) << endl; list[i].print(); cout << "---------------------"<type); } int HashTable::hashIndex(string key) { unsigned long hash = 5381; for (int i = 0; i < key.size(); i++) { hash = ((hash << 5) + hash)+key[i]; } return hash % 33; } HashTable.h #pragma once #include"LinkedList.h" /* Hash table with separate chaining. */ class HashTable { private: LinkedList list[33]; HashType type; int hashIndex(string key); public: HashTable(HashType t); void add(string word, string pos, string english); void print(); DictionaryWord find(string key); }; LinkedList.cpp #include "LinkedList.h" LinkedList::LinkedList() { head = NULL; } void LinkedList::add(string word,string pos,string english) { if (head == NULL) { head = new DictionaryWord(word,pos,english); return; } DictionaryWord* cur = head; while (cur->next != NULL) { cur = cur->next; } cur->next = new DictionaryWord(word, pos, english); } DictionaryWord LinkedList::find(string key,HashType type) { if (head == NULL) { return DictionaryWord("", "", ""); } DictionaryWord* cur = head; while (cur->next != NULL) { DictionaryWord temp(cur->word,cur->partOfSpeech,cur->englishMeaning); if ((int)temp.word.find(" ") != -1) { temp.word = temp.word.substr(temp.word.find(" ") + 1); } if ((int)temp.partOfSpeech.find("(") != -1) { temp.partOfSpeech = temp.partOfSpeech.substr(0, temp.partOfSpeech.find("(")); } if (type == wordHash && temp.word == key) { return *cur; } if (type == posHash && temp.partOfSpeech == key) { return *cur; } if (type == englishHash && temp.englishMeaning== key) { return *cur; } cur = cur->next; } DictionaryWord temp(cur->word, cur->partOfSpeech, cur->englishMeaning); if ((int)temp.word.find(" ") != -1) { temp.word = temp.word.substr(temp.word.find(" ") + 1); } if ((int)temp.partOfSpeech.find("(") != -1) { temp.partOfSpeech = temp.partOfSpeech.substr(0, temp.partOfSpeech.find("(")); } if (type == wordHash && temp.word == key) { return *cur; } if (type == posHash && temp.partOfSpeech == key) { return *cur; } if (type == englishHash && temp.englishMeaning == key) { return *cur; } return DictionaryWord("", "", ""); } void LinkedList::print() { if (head == NULL) return; DictionaryWord* cur = head; while (cur->next != NULL) { cout << cur->word<<"\t"<partOfSpeech<<"\t"<englishMeaning<next; } cout << cur->word << "\t" << cur->partOfSpeech << "\t" << cur->englishMeaning << endl; } void clean(DictionaryWord* node) { if (node == NULL) { return; } if (node->next == NULL) { return; } clean(node->next); delete node->next; } LinkedList::~LinkedList() { clean(head); delete head; } LinkedList.h #pragma once #include #include #include using namespace std; enum HashType { wordHash, posHash, englishHash }; struct DictionaryWord { string word; string partOfSpeech; string englishMeaning; DictionaryWord*next; DictionaryWord() {} DictionaryWord(string w,string p,string english) { next = NULL; word = w; partOfSpeech = p; englishMeaning = english; } void print() { printf("%-15s %-8s %-10s\n", word.c_str(), partOfSpeech.c_str(), englishMeaning.c_str()); } void print2() { printf("%-15s %-8s %-10s\n", englishMeaning.c_str(), partOfSpeech.c_str(), word.c_str()); } bool operator<(DictionaryWord &x) { string word1 = word; string word2 = x.word; if ((int)word.find(" ") != -1) { word1 = word.substr(word.find(" ") +1); } if ((int)x.word.find(" ") != -1) word2 = x.word.substr(x.word.find(" ") + 1); return word1 < word2 ; } }; class LinkedList { DictionaryWord *head; public: LinkedList(); void add(string word, string pos, string english); DictionaryWord find(string key,HashType type); void print(); ~LinkedList(); }; Source.cpp #include"Dictionary.h" #include #include #include void split(std::string& s, string &x, string &y,string &z) { if (s.empty()) return; int index = s.find(","); x = s.substr(0, index); int index2 = s.substr(index+1).find(","); y = s.substr(index+1, index2); z = s.substr(index + index2 + 2); } void initTables(Dictionary &german,Dictionary &french,Dictionary &latin,Dictionary &herbew) { ifstream f("input.txt"); string x, y, z; string line; for (int i = 0; i < 26; i++) { getline(f, line); split(line, x, y, z); german.addDictionaryWord(x, y, z); } for (int i = 0; i < 25; i++) { getline(f,line); split(line, x, y, z); french.addDictionaryWord(x, y, z); } for (int i = 0; i < 25; i++) { getline(f, line); split(line, x, y, z); latin.addDictionaryWord(x, y, z); } for (int i = 0; i < 25; i++) { getline(f, line); split(line, x, y, z); herbew.addDictionaryWord(x, y, z); } } void printMenu() { cout << "1- create the word list" << endl; cout << "2- German, French, Latin, Herbew to English nouns" << endl; cout << "3- German, French, Latin, Herbew to English verbs" << endl; cout << "4- German, French, Latin, Herbew to English prepositions" << endl; cout << "5- German, French, Latin, Herbew to English adjectives" << endl; cout << "6- German, French, Latin, Herbew to English adverbs" << endl; cout << "7- German, French, Latin, Herbew to English cardinal numbers" << endl; cout << "8- German, French, Latin, Herbew to English all" << endl; cout << "9- English to (German, French, Latin, Herbew) nouns" << endl; cout << "10- English to (German, French, Latin, Herbew) verbs" << endl; cout << "11- English to (German, French, Latin, Herbew) prepostions" << endl; cout << "12- English to (German, French, Latin, Herbew) adjectives" << endl; cout << "13- English to (German, French, Latin, Herbew) adverbs" << endl; cout << "14- English to (German, French, Latin, Herbew) cardinal numbers" << endl; cout << "15- English to (German, French, Latin, Herbew) all" << endl; cout << "16- Search for word in one language and return other language" << endl; cout << "17- Quit" << endl; cout << "Enter your choice: "; } void printVector(vectorvec) { sort(vec.begin(), vec.end()); for (int i = 0; i < vec.size(); i++) { vec[i].print(); } } void printVector2(vectorvec) { sort(vec.begin(), vec.end()); for (int i = 0; i < vec.size(); i++) { vec[i].print2(); } } int main() { Dictionary dicts[4];//german frech latin herbew string names[] = { "German" ,"French" ,"Latin" ,"Herbew" }; int action = -1; bool start = false; DictionaryWord temp; vectorresult; string word;//to search for bool found; while (action != 17) { result.clear(); printMenu(); cin >> action; if (!start && action == 16) { cout << "please create list first"<