From c99872f7b18f787490983ed5b62cfa1cb23e2038 Mon Sep 17 00:00:00 2001 From: canes20 <43278745+canes20@users.noreply.github.com> Date: Sat, 6 Oct 2018 02:06:10 -0400 Subject: [PATCH 1/4] Update matchTen.cpp --- src/c++/matchTen.cpp | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) diff --git a/src/c++/matchTen.cpp b/src/c++/matchTen.cpp index d941985..455eac5 100644 --- a/src/c++/matchTen.cpp +++ b/src/c++/matchTen.cpp @@ -106,10 +106,13 @@ float uiLevenshteinDistance( const std::string &s1, const std::string &s2) int main() { - string input; + string input, fileName; + // stores the given file in the variable myFile - std::ifstream myFile("somefile.txt", std::ifstream::in); + cout << "Please enter a file name to read:" ; + cin >> fileName; + std::ifstream myFile(fileName, std::ifstream::in); // input std::getline(std::cin, input); @@ -129,4 +132,4 @@ int main() cin >> input; return 0; -} \ No newline at end of file +} From 98e2eaf5e20746338ef35924c568b7f4a809db60 Mon Sep 17 00:00:00 2001 From: canes20 <43278745+canes20@users.noreply.github.com> Date: Thu, 25 Oct 2018 19:48:38 -0400 Subject: [PATCH 2/4] Update matchTen.cpp --- src/c++/matchTen.cpp | 336 +++++++++++++++++++++++++++++++------------ 1 file changed, 243 insertions(+), 93 deletions(-) diff --git a/src/c++/matchTen.cpp b/src/c++/matchTen.cpp index 455eac5..043cf1b 100644 --- a/src/c++/matchTen.cpp +++ b/src/c++/matchTen.cpp @@ -5,131 +5,281 @@ #include #include #include +#include +#include +#include using namespace std; // struct type that stores two data type variables, in this case the LevenshteinDistance and the string your input was compared with in the file +template struct Record { - float distance; - string value; + float distance; + ASYNC value; }; // prototype for levenshtein -float uiLevenshteinDistance( const std::string &s1, const std::string &s2); +float uiLevenshteinDistance(const std::string &s1, const std::string &s2); -// sort to use for vector that is filled with struct "Records", once sorted the lowest levenshtein distances should be easy to discern -bool sortByDistance (Record const & a, Record & b) + +// function for sorting the final vector +void finalSort(vector finalVector) +{ + Record result; + result.distance = finalVector[0].distance; + result.value = finalVector[0].value; + + for (int i = 1; i > finalVector.size(); i++) + { + if (result.distance < finalVector[i].distance) + { + result.distance = finalVector[i].distance; + result.value = finalVector[i].value; + } + + + } + cout << "The closest match is:" << result.value << " which has a levenshtein distance of " << result.distance <<"::::::"; + +} + +// function to get the number of lines in a file +int fileLines(ifstream& file) { - return a.distance < b.distance; + int number_of_Lines = 0; + string line; + + if (file.is_open()) { + while (std::getline(file, line)) + { + ++number_of_Lines; + } + file.close(); + } + + + + std::cout << "The number of lines in the file is: " << number_of_Lines << endl; + return number_of_Lines; } // reads the the given file and stores its values in a vector -vector readFile(ifstream& file) +vector readFile0(ifstream& file) +{ + string line; + vectorfileVector0; + + cout << "thread 0 is starting..." << endl; + + if (file.is_open()) + { + for (int i = 1; !file.eof(); i + 4) + { + getline(file, line); + fileVector0.push_back(line); + + } + file.close(); + + } + cout << "thread 0 is complete..." << endl; + return fileVector0; +} + +vector readFile1(ifstream& file) { - string line; - vectorfileVector; - - if(file.is_open()) - { - while ( getline (file, line) ) - { - fileVector.push_back(line); - } - file.close(); - } - - return fileVector; + string line; + vectorfileVector1; + + cout << "thread 1 is starting..." << endl; + + if (file.is_open()) + { + for(int i = 1; !file.eof(); i + 4) + { + getline(file, line); + fileVector1.push_back(line); + + } + file.close(); + + } + cout << "thread 1 is complete..." << endl; + return fileVector1; } -//** having trouble getting this to work but the idea is to pass a string input into the function and compare it with every string value in the file which is accessible through the vector it was copied to -vector compare(string input, vector file) +vector readFile2(ifstream& file) { - vector results; - Record result; - for(int i = 0; i < file.size(); i++) - { - float distance = uiLevenshteinDistance(input, file[i]); - if (distance > result.distance) - { - result.distance = distance; - result.value = file[i]; - } - } - results.push_back(result); - return results; + string line; + vectorfileVector2; + + cout << "thread 2 is starting..." << endl; + + if (file.is_open()) + { + for(int i = 2; !file.eof(); i + 4) + { + getline(file, line); + fileVector2.push_back(line); + + } + file.close(); + + } + cout << "thread 2 is complete..." << endl; + return fileVector2; +} + +vector readFile3(ifstream& file) +{ + string line; + vectorfileVector3; + + cout << "thread 3 is starting..." << endl; + + if (file.is_open()) + { + for(int i = 3; !file.eof(); i + 4) + { + getline(file, line); + fileVector3.push_back(line); + + } + file.close(); + + } + cout << "thread 3 is complete..." << endl; + return fileVector3; +} + +// compares the input given by the user to the files lines stored in the vector +template +vector compare(string input, ASYNC &file) +{ + vector results; + Record result; + for (int i = 0; i < file.size(); i++) + { + float distance = uiLevenshteinDistance(input, file[i]); + if (distance > result.distance) + { + result.distance = distance; + result.value = file[i]; + } + } + results.push_back(result); + return results; } // actual function for calculating levenshtein distance -float uiLevenshteinDistance( const std::string &s1, const std::string &s2) +float uiLevenshteinDistance(const std::string &s1, const std::string &s2) { - const size_t m(s1.size()); - const size_t n(s2.size()); - const size_t sum = m + n; - - if( m==0 ) return n; - if( n==0 ) return m; - - size_t *costs = new size_t[n + 1]; - - for( size_t k=0; k<=n; k++ ) costs[k] = k; - - size_t i = 0; - for ( std::string::const_iterator it1 = s1.begin(); it1 != s1.end(); ++it1, ++i ) - { - costs[0] = i+1; - size_t corner = i; - - size_t j = 0; - for ( std::string::const_iterator it2 = s2.begin(); it2 != s2.end(); ++it2, ++j ) - { - size_t upper = costs[j+1]; - if( *it1 == *it2 ) - { - costs[j+1] = corner; - } - else - { - size_t t(upper finalVector; + + // stores the given file in the variable myFile + cout << "Please enter a file name to read:" << endl; + std::getline(std::cin, fileName); + std::ifstream myFile(fileName, std::ifstream::in); + + // input for comparison with file results + cout << "Please enter a record name for comparison:" << endl; + std::getline(std::cin, input); + + /* fills the fileVector with the strings from the file with every fourth line of the file starting at 0, the function is run concurrently with it's likewise + *I had to use auto to simulate the vector type here because using async created a special type that can't be stored using by simply using a string vector + */ + auto t1 = async(launch::async, readFile0, ref(myFile)); + + + auto t2 = async(launch::async, readFile1, ref(myFile)); + + + auto t3 = async(launch::async, readFile2, ref(myFile)); + + + auto t4 = async(launch::async, readFile3, ref(myFile)) + + // produces levenshtein values by comparing the input with the first 1/4 of the file (here is where I'm having trouble because the Record vector can't store the new type. + // using auto here wouldn't work either because you need to create a struct to idenify the closest levenshtein distance and the string match associated with it. + vector sortedVector0 = compare(input, t1); + + // produces levenshtein values by comparing the input with the second 1/4 of the file + vector sortedVector1 = compare(input, t2); + + // produces levenshtein values by comparing the input with the third 1/4 of the file + vector sortedVector2 = compare(input, t3); + // produces levenshtein values by comparing the input with the final 1/4 of the file + vector sortedVector3 = compare(input, t4); - // stores the given file in the variable myFile - cout << "Please enter a file name to read:" ; - cin >> fileName; - std::ifstream myFile(fileName, std::ifstream::in); + // here I'm simply merging the values of the four vectors created from the four parts of the file into one + result0.value = sortedVector0[0].value; + result0.distance = sortedVector0[0].distance; + finalVector.push_back(result0); - // input - std::getline(std::cin, input); + result1.value = sortedVector1[0].value; + result1.distance = sortedVector1[0].distance; + finalVector.push_back(result1); - // fills the fileVector with the strings from the file - vector fileVector = readFile(myFile); + result2.value = sortedVector2[0].value; + result2.distance = sortedVector2[0].distance; + finalVector.push_back(result2); - //** function that insn't working - vector sortedVector = compare(input, fileVector); + result3.value = sortedVector3[0].value; + result3.distance = sortedVector3[0].distance; + finalVector.push_back(result3); - // prints the contents of the fileVector to show that the strings from the file are copied to the vector - for (int i = 0; i < sortedVector.size(); i++) { - std:cout << sortedVector[i].value << " : " << sortedVector[i].distance << "\n"; - } + // sorts the four values of the finalVector (assuming that we decide to print more values from the threads, a more efficient sort could be utilized here). + finalSort(finalVector); - cout << " End of program"; - cin >> input; + std::cout << " End of program"; - return 0; + return 0; } From ef7925d92072750710b41ef00d14ddfc8f507211 Mon Sep 17 00:00:00 2001 From: canes20 <43278745+canes20@users.noreply.github.com> Date: Thu, 25 Oct 2018 19:58:07 -0400 Subject: [PATCH 3/4] Update matchTen.cpp --- src/c++/matchTen.cpp | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/c++/matchTen.cpp b/src/c++/matchTen.cpp index 043cf1b..044310e 100644 --- a/src/c++/matchTen.cpp +++ b/src/c++/matchTen.cpp @@ -11,7 +11,8 @@ using namespace std; -// struct type that stores two data type variables, in this case the LevenshteinDistance and the string your input was compared with in the file +// this is supposed to be a struct type that stores the levenshtein distance and the string value associated with the line from the file it was compared to +// I attempted to change string to a template to accomodate the weird type that is producted by the future package later in the code. template struct Record { From 075fbe3054986d2926c21a8966af030ea876fff2 Mon Sep 17 00:00:00 2001 From: canes20 <43278745+canes20@users.noreply.github.com> Date: Thu, 25 Oct 2018 20:28:43 -0400 Subject: [PATCH 4/4] Update matchTen.cpp --- src/c++/matchTen.cpp | 35 ++++++++++++++--------------------- 1 file changed, 14 insertions(+), 21 deletions(-) diff --git a/src/c++/matchTen.cpp b/src/c++/matchTen.cpp index 044310e..401e2f3 100644 --- a/src/c++/matchTen.cpp +++ b/src/c++/matchTen.cpp @@ -11,13 +11,11 @@ using namespace std; -// this is supposed to be a struct type that stores the levenshtein distance and the string value associated with the line from the file it was compared to -// I attempted to change string to a template to accomodate the weird type that is producted by the future package later in the code. -template +// struct type that stores two data type variables, in this case the LevenshteinDistance and the string your input was compared with in the file struct Record { float distance; - ASYNC value; + string value; }; // prototype for levenshtein @@ -155,8 +153,7 @@ vector readFile3(ifstream& file) } // compares the input given by the user to the files lines stored in the vector -template -vector compare(string input, ASYNC &file) +vector compare(string input, vector file) { vector results; Record result; @@ -233,34 +230,28 @@ int main() cout << "Please enter a record name for comparison:" << endl; std::getline(std::cin, input); - /* fills the fileVector with the strings from the file with every fourth line of the file starting at 0, the function is run concurrently with it's likewise + /* fills the fileVector with the strings from the file with every fourth line of the file starting at 0, the function is run concurrently with it's likewise *I had to use auto to simulate the vector type here because using async created a special type that can't be stored using by simply using a string vector */ auto t1 = async(launch::async, readFile0, ref(myFile)); - + auto t2 = async(launch::async, readFile1, ref(myFile)); - + auto t3 = async(launch::async, readFile2, ref(myFile)); - - auto t4 = async(launch::async, readFile3, ref(myFile)) - // produces levenshtein values by comparing the input with the first 1/4 of the file (here is where I'm having trouble because the Record vector can't store the new type. - // using auto here wouldn't work either because you need to create a struct to idenify the closest levenshtein distance and the string match associated with it. - vector sortedVector0 = compare(input, t1); + auto t4 = async(launch::async, readFile3, ref(myFile)); + + vector sortedVector0 = compare(input, t1.get()); - // produces levenshtein values by comparing the input with the second 1/4 of the file - vector sortedVector1 = compare(input, t2); + vector sortedVector1 = compare(input, t2.get()); - // produces levenshtein values by comparing the input with the third 1/4 of the file - vector sortedVector2 = compare(input, t3); + vector sortedVector2 = compare(input, t3.get()); - // produces levenshtein values by comparing the input with the final 1/4 of the file - vector sortedVector3 = compare(input, t4); + vector sortedVector3 = compare(input, t4.get()); - // here I'm simply merging the values of the four vectors created from the four parts of the file into one result0.value = sortedVector0[0].value; result0.distance = sortedVector0[0].distance; finalVector.push_back(result0); @@ -284,3 +275,5 @@ int main() return 0; } + +