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/src/RandomWriter.cpp b/RandomWriter/src/RandomWriter.cpp index e591dac..75d6b87 100644 --- a/RandomWriter/src/RandomWriter.cpp +++ b/RandomWriter/src/RandomWriter.cpp @@ -6,9 +6,152 @@ #include "random.h" #include "strlib.h" #include "vector.h" +#include "filelib.h" +#include "simpio.h" +#include "tokenscanner.h" using namespace std; + +/* +The Function is to get the user level model in the form of an integer from 1 -10 +param modelLevel - the variable which will contain modelLevel +*/ +void getModelLevel(int &modelLevel){ + while(true){ + modelLevel = getInteger("Enter model level from 1-10: "); + if (modelLevel < 1 || modelLevel >10){ + cout<<"You enter the wrong model level!"<> &seedMap, ifstream &fileForAnalize, int &modelLevel){ + string seedKey; + char nextChar; + + TokenScanner scanner(fileForAnalize); + + readFirstSeed(scanner,modelLevel,seedKey,nextChar); + while(true){ + readNextSeeds(seedKey,nextChar,modelLevel); + nextChar = scanner.getChar(); + if(nextChar == EOF){ + break; + } + /*At each iteration adding to the map characters which is the current Sid*/ + seedMap[seedKey].add(nextChar); + } + fileForAnalize.close(); + +} + + +/*The Function of analyzing the map, and choose seed with the highest number of possible characters + param seedMap - map in which seed are the keys and values are vectors of characters which may be present after seed + return seedKey return found the most popular seed +*/ +string findMostPopularSeed(Map> &seedMap){ + Vector seedKeyCopy = seedMap.keys(); + string result = seedKeyCopy[0]; + for (string key: seedMap){ + if (seedMap[key].size() > seedMap[result].size()){ + result = key; + } + } + return result; +} + +/* + Function randomly selects the next character of the possible from prepared seed + param seedMap - map in which seed are the keys and values are vectors of characters which may be present after seed + param startSeed - the most popular seed + param modelLevel - the variable which will contain modelLevel + return character the most popular character from prepared seed +*/ +char getNextChar(Map> &seedMap,string &startSeed, int &modelLevel){ + char result; + Vector nextChar = seedMap[startSeed]; + result = nextChar[randomInteger(0, nextChar.size()-1)]; + readNextSeeds(startSeed,result,modelLevel); + return result; +} + +/* + Function generates a random text length of 2000 characters from the collected data in the map by Markov model + param seedMap - map in which seed are the keys and values are vectors of characters which may be present after seed +*/ +void printRandomText(Map> &seedMap,int &modelLevel){ + const int MAX_TEXT_SIZE = 2000; + string resultText = ""; + string startSeed = findMostPopularSeed(seedMap); + + resultText += startSeed; + while (true) { + + if (resultText.length() > MAX_TEXT_SIZE) break; + + resultText += getNextChar(seedMap, startSeed,modelLevel); + } + + cout<> seedMap; + + while (true) { + promptUserForFile(fileForAnalize, "Please enter the your file name: "); + if (fileForAnalize.is_open()){ + break; + } + } + + getModelLevel(modelLevel); + + readFileToBuildSeedsMap(seedMap,fileForAnalize,modelLevel); + + printRandomText(seedMap,modelLevel); + + return 0; } 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..f92b1df 100644 --- a/WordLadder/src/WordLadder.cpp +++ b/WordLadder/src/WordLadder.cpp @@ -6,7 +6,111 @@ #include "vector.h" using namespace std; + +/* +Function selects all the possible words that are different from the input one character. +param - startWord word to start the search +param allValidWord - a list of all the words +param usedWords - list has previously used the words +return vector all found words +*/ +Vector findSimilarWord(string startWord, Lexicon &allValidWord, Lexicon &usedWords){ + Vector result; + if(!usedWords.contains(startWord)) + usedWords.add(startWord); + + for (int i = 0; i < startWord.length(); i++){ + string wordCopy = startWord; + for (char c = 'a'; c <= 'z'; c++){ + wordCopy.replace(i,1,1,c); + if (allValidWord.contains(wordCopy) & !usedWords.contains(wordCopy)) { + result.add(wordCopy); + usedWords.add(wordCopy); + } + } + } + + return result; +} + + +/* + method adds the word to the queue + param word - the word to be added + validLadders - queue all valid ladders +*/ +void addWordToQueue (string word, Queue> &validLadders){ + Vector shortestLadder(1, word); + validLadders.enqueue(shortestLadder); +} + + +/* +Function displays the shortest ladder +param shortestLadder - a vector that contains the shortest ladder +*/ +void printResultLadder(Vector &shortestLadder){ + for (int i = 0; i < shortestLadder.size(); i++){ + if ( i != shortestLadder.size()-1 ){ + cout<"; + } else{ + cout<> validLadders; + addWordToQueue(startWord, validLadders); + bool result = false; + + while (!validLadders.isEmpty()) { + Vector shortestLadder = validLadders.dequeue(); + // If the last word of the vector is equal to destWord way to stop searching + if (shortestLadder[shortestLadder.size() -1] == destWord) { + printResultLadder(shortestLadder); + result = true; + break; + } + //displace a search on the last word on the ladder + string lastLadderWord = shortestLadder[shortestLadder.size() -1]; + + Vector allSimilarWords = findSimilarWord(lastLadderWord, allValidWord, usedWords); + + for(int i=0; i < allSimilarWords.size(); i++){ + Vector copyShortestLadder = shortestLadder; + copyShortestLadder.add(allSimilarWords[i]); + validLadders.enqueue(copyShortestLadder); + } + //If the ladder is not built stop and display a message + if (shortestLadder.size() == 0) break; + + } + if (!result) cout<<"Any ladders could not be found"<