Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion RandomWriter/RandomWriter.pro
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,7 @@ win32 {
QMAKE_LFLAGS += -Wl,--stack,536870912
LIBS += -lDbghelp
LIBS += -lbfd
LIBS += -liberty
# LIBS += -liberty
LIBS += -limagehlp
}
macx {
Expand Down
1 change: 1 addition & 0 deletions RandomWriter/res/HED.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Returns true if queue1 and queue2 contain the same elements.R.
150 changes: 149 additions & 1 deletion RandomWriter/src/RandomWriter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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<string, Vector<char>> &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<string, Vector<char>> &seeds);
char nextChar(Map<string, Vector<char>> &seeds,string &firstSeed,int &modelLevel);
void generateText(Map<string, Vector<char>> &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<string, Vector<char>> 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!!!"<<endl;
}

}

/*The function analyzes the source file for the presence of seeds in it
, the size of which depends on the order of numbers entered by the user model.
Selected seeds in Map will be recorded to the keys and the character that follows them as the value*/
void analysisOfTheModelText(Map<string, Vector<char>> &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);i<modelLevel;i++){
nextChar = scanner.getChar();
if(nextChar == EOF ){
break;
}
seed += nextChar;
}
}

/*The function receives the seed source and symbol located after him.
Seed subtract one character from the beginning, and one character is added at the end.
The result is the next seed.*/
void generationNextSeed(string &seed,char &nextChar,int &modelLevel){
if(seed != ""){
if(modelLevel>1){
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<string, Vector<char>> &seeds,int &modelLevel){
Vector<string> 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<<result<<endl;
}

//Initial is the seed from which the largest size values. Since it is most common in text.
void findingTheInitialSeed(string &initialSeed,Map<string, Vector<char>> &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<string, Vector<char>> &seeds,string &initialSeed, int &modelLevel){
char ch;
Vector<char> 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;
}
2 changes: 1 addition & 1 deletion WordLadder/WordLadder.pro
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,7 @@ win32 {
QMAKE_LFLAGS += -Wl,--stack,536870912
LIBS += -lDbghelp
LIBS += -lbfd
LIBS += -liberty
# LIBS += -liberty
LIBS += -limagehlp
}
macx {
Expand Down
130 changes: 128 additions & 2 deletions WordLadder/src/WordLadder.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,136 @@
#include "lexicon.h"
#include "queue.h"
#include "simpio.h"
#include "vector.h"
#include "vector.h"\
//#include <string>
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<Vector<string> > 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<string> 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<string>) 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<string> 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<string> 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 "";
}