From 5b7ee6a75e60989585a2337293e263d6148544bb Mon Sep 17 00:00:00 2001 From: vhrebenko Date: Fri, 4 Sep 2015 22:57:23 +0300 Subject: [PATCH 1/2] [Assignment2.1 Done] --- WordLadder/WordLadder.pro | 2 +- WordLadder/src/WordLadder.cpp | 130 +++++++++++++++++++++++++++++++++- 2 files changed, 129 insertions(+), 3 deletions(-) diff --git a/WordLadder/WordLadder.pro b/WordLadder/WordLadder.pro index 85bdbce..4570e8d 100644 --- a/WordLadder/WordLadder.pro +++ b/WordLadder/WordLadder.pro @@ -110,7 +110,7 @@ win32 { QMAKE_LFLAGS += -Wl,--stack,536870912 LIBS += -lDbghelp LIBS += -lbfd - LIBS += -liberty +# LIBS += -liberty LIBS += -limagehlp } macx { diff --git a/WordLadder/src/WordLadder.cpp b/WordLadder/src/WordLadder.cpp index 01d35d7..b8f8ba3 100644 --- a/WordLadder/src/WordLadder.cpp +++ b/WordLadder/src/WordLadder.cpp @@ -3,10 +3,136 @@ #include "lexicon.h" #include "queue.h" #include "simpio.h" -#include "vector.h" +#include "vector.h"\ +//#include using namespace std; +string findNextWord(string word, Lexicon &english, Lexicon &alreadyUsed); +void wordLadder(Lexicon &english, string startWord, string endWord); + + int main() { - // TODO: fill in the code + //Create a vocabulary for checking the validity of the newly created words + Lexicon english("EnglishWords.txt"); + + while(true){ + //Setting the initial word + string startWord = getLine("Enter source word (RETURN to quit): "); + + //Setting the final word + string endWord = getLine("Enter destination word: "); + + //The search function of the shortest ladder dictionary + wordLadder(english,startWord,endWord); + } return 0; } + + + +/*The search function of the shortest ladder dictionary. +Takes to verify the validity of the lexicon, the word of the initial and final*/ +void wordLadder(Lexicon &english, string startWord, string endWord) +{ + // Creating a queue to place ladders dictionary + Queue > queueWordLadders; + + //Lexicon to have used words + Lexicon alreadyUsed; + + //The line for the newly generated words + string nextWord; + + //Clear the queue + queueWordLadders.clear(); + + //Create a vector and move it to the initial word. We put the vector at the end of the queue. + Vector addToQueue; + addToQueue.add(startWord); + queueWordLadders.enqueue(addToQueue); + + // We put the initial word in the lexicon already using the word. + alreadyUsed.add(startWord); + + + //*************Cycle find the shortest ladder dictionary************************ + bool start = true; + while (start) + { + + /* build up a new list(Vector) of items to search, or stop if empty*/ + if (queueWordLadders.size() == 0) + { + cout << "Nope nothing found! no ladder"<< endl; + break; + } + + /*Create a list and we set to the first element in the queue. + In turn, this element is then removed.*/ + Vector firstLadderToQueue = queueWordLadders.dequeue(); + + + /*The cycle of adding all the vocabulary of stairs to the difference in + the words of one letter (one-step, two-step ladder and so on + until you find the shortest ladder final of elements that will be the final word).*/ + + /***********The cycle of creation of the dictionary of stairs and find the shortest**********/ + while (true) + { + + // find the next word to use + nextWord = findNextWord(firstLadderToQueue[firstLadderToQueue.size()-1], english, alreadyUsed); + // the end of the search + if (nextWord == endWord) + { + cout << "FOUND ladder !!" << endl; + for (string list: firstLadderToQueue) + { + cout << list << " -> "; + } + cout << endWord << endl << endl; + start = false; + break; + } else if (nextWord != "") + { + /*if there is another word to search with add to the end of the list. + Create a copy of the first list in the queue + added to his word and put a list of the queue.*/ + Vector addMoreLadder = firstLadderToQueue; + addMoreLadder.add(nextWord); + queueWordLadders.enqueue(addMoreLadder); + } + // else if nothing left to search for stop!! + else if (nextWord =="") + break; + //Add a word to the list of already used + alreadyUsed.add(nextWord); + + } + } +} + + + +/*Creating a word that differs from the input one letter and a test of its validity +The function takes an input word, the vocabulary of English words, the lexicon has used words. +The function returns a word which is different from the input one letter.*/ +string findNextWord(string word, Lexicon &english, Lexicon &alreadyUsed) +{ + string newWord; + for (int i =0; i < word.length(); i++) + { + newWord = word; + for (char ch = 'a'; ch <= 'z'; ch++) + { + // create a new word + newWord = newWord.replace(i,1,1, ch); + // make sure it is a valid word and also not already used + if (english.contains(newWord)) + if (!alreadyUsed.contains(newWord)) + return newWord; + } + } + //If the word is not valid and is already used returns an empty string. + return ""; +} From fde3238f2ec553ee714ea1d57fd9c1dde25378e4 Mon Sep 17 00:00:00 2001 From: vhrebenko Date: Sun, 6 Sep 2015 23:48:07 +0300 Subject: [PATCH 2/2] [Assignment2.2 Done] --- RandomWriter/RandomWriter.pro | 2 +- RandomWriter/res/HED.txt | 1 + RandomWriter/src/RandomWriter.cpp | 150 +++++++++++++++++++++++++++++- 3 files changed, 151 insertions(+), 2 deletions(-) create mode 100644 RandomWriter/res/HED.txt diff --git a/RandomWriter/RandomWriter.pro b/RandomWriter/RandomWriter.pro index 85bdbce..4570e8d 100644 --- a/RandomWriter/RandomWriter.pro +++ b/RandomWriter/RandomWriter.pro @@ -110,7 +110,7 @@ win32 { QMAKE_LFLAGS += -Wl,--stack,536870912 LIBS += -lDbghelp LIBS += -lbfd - LIBS += -liberty +# LIBS += -liberty LIBS += -limagehlp } macx { diff --git a/RandomWriter/res/HED.txt b/RandomWriter/res/HED.txt new file mode 100644 index 0000000..57da395 --- /dev/null +++ b/RandomWriter/res/HED.txt @@ -0,0 +1 @@ +Returns true if queue1 and queue2 contain the same elements.R. diff --git a/RandomWriter/src/RandomWriter.cpp b/RandomWriter/src/RandomWriter.cpp index e591dac..ad180dc 100644 --- a/RandomWriter/src/RandomWriter.cpp +++ b/RandomWriter/src/RandomWriter.cpp @@ -6,9 +6,157 @@ #include "random.h" #include "strlib.h" #include "vector.h" +#include "filelib.h" +#include "simpio.h" +#include "tokenscanner.h" using namespace std; +void inputModelLevel(int &modelLevel); +void analysisOfTheModelText(Map> &seeds, ifstream &infile, int &modelLevel); +void findingFirstSeed(TokenScanner &scanner,int &modelLevel,string &seed,char &nextChar); +void generationNextSeed(string &seed,char &nextChar,int &modelLevel); +void findingTheInitialSeed(string &initialSeed,Map> &seeds); +char nextChar(Map> &seeds,string &firstSeed,int &modelLevel); +void generateText(Map> &seeds,int &modelLevel); + + + int main() { - // TODO: fill in the code + /*Variable storage: + feed stream to generate a random text, + line number generation model, + collections storage seeds.*/ + ifstream infile; + int modelLevel; + Map> seeds; + + //Prompts the user for the name of the source to generate random text generation and model number. + promptUserForFile(infile, "Enter the file name: "); + inputModelLevel(modelLevel); + + /*The function takes the parameters: source file to generate random text, + line number generation models, clothing collection for storing the results + of analysis of the model text.*/ + analysisOfTheModelText(seeds,infile,modelLevel); + + /*Function based on the model obtained from the analysis of the source code + and a user-specified number of order model generates random text*/ + generateText(seeds,modelLevel); + return 0; } + + +//***Determining the validity of a user-entered numbers in a row models generate a random text.*** +void inputModelLevel(int &modelLevel){ + while(true){ + modelLevel = getInteger("Enter model level (1-10): "); + if(modelLevel && (modelLevel>=1 && modelLevel <= 10)){ + break; + } + cout<<"Enter the wrong number!!!"<> &seeds, ifstream &infile, int &modelLevel){ + string seed; + char nextChar; + + TokenScanner scanner(infile); + findingFirstSeed(scanner,modelLevel,seed,nextChar); + while(true){ + generationNextSeed(seed,nextChar,modelLevel); + if((nextChar = scanner.getChar()) == EOF){ + break; + } + + seeds[seed].add(nextChar); + } + infile.close(); + +} + +/*An additional feature that is used in the analysis of the model text. +Finding the first seed in the text. Depending on the model number of +the order entered by the user to generate the file to read the same +number of characters is equal to the number of the order of the model. +A string of characters is the first seed.*/ +void findingFirstSeed(TokenScanner &scanner,int &modelLevel,string &seed,char &nextChar){ + + for(int i(0);i1){ + seed = seed.substr(1); + seed += nextChar; + } + else{ + seed = ""; + seed += nextChar; + } + } + } + + +/*The function that generates a random text based on the model obtained +from the analysis of the text (which the Collection MAP keys are seeds +and the values that are characters after them with a certain probability).*/ +void generateText(Map> &seeds,int &modelLevel){ + Vector keys = seeds.keys(); + string result = ""; + string initialSeed = keys[0]; + + //Finding the initial seed to start generating random text + findingTheInitialSeed(initialSeed,seeds); + result += initialSeed; + + while (true) { + result += nextChar(seeds, initialSeed,modelLevel); + if (result.length() > 2000){ + break; + } + } + + cout<> &seeds){ + for (string key : seeds){ + if (seeds[key].size() > seeds[initialSeed].size()){ + initialSeed = key; + } + } +} + +/*Randomly searching for a symbol, which will be located after the seed. +Search occurs in the probability vector values of the characters that are after the seeds.*/ +char nextChar(Map> &seeds,string &initialSeed, int &modelLevel){ + char ch; + Vector value = seeds[initialSeed]; + int n; + if(value.size()>0){ + n = randomInteger(0, value.size()-1); + } + else{ + n = 0; + } + ch = value[n]; + generationNextSeed(initialSeed,ch,modelLevel); + return ch; +}