diff --git a/Cards.h b/Cards.h index 76795bbe2579fc10599320b47c5e2157c524f4a1..50b46310fc8fdbe7dcd0b99c09c2788f38e9e786 100644 --- a/Cards.h +++ b/Cards.h @@ -2,6 +2,7 @@ // Created by Eray düzenli on 13.04.23. // #pragma once + #include <string> @@ -15,5 +16,4 @@ public: std::string cmc; std::string type; std::string count; - }; \ No newline at end of file diff --git a/aufgabe1.cpp b/aufgabe1.cpp index 49c3fa8f711574368dc137635eb2fe3c9f9de4c1..ce8b962d9f6b5d578403436da7f87339b2cf951f 100644 --- a/aufgabe1.cpp +++ b/aufgabe1.cpp @@ -1,7 +1,7 @@ #include <iostream> #include "aufgabe1.h" -//calculate minimum of three values +//Helper function, calculate minimum of three values int min(int x, int y, int z) { if (x < y && x < z) { return x; @@ -13,7 +13,7 @@ int min(int x, int y, int z) { } -int levenshteinDistance(const std::string& s, const std::string& t) { +int levenD(const std::string &s, const std::string &t) { //Length of first string int n = s.length(); //Length of second string @@ -35,23 +35,24 @@ int levenshteinDistance(const std::string& s, const std::string& t) { for (int i = 1; i <= n; i++) { for (int j = 1; j <= m; j++) { - if (s[i-1] == t[j-1]) + if (s[i - 1] == t[j - 1]) cost = 0; else cost = 1; //M[i][j] := min(rep, ins, del) - int replace = Matrix[i-1][j-1] + cost; - int insert = Matrix[i][j-1]+1; - int del = Matrix[i-1][j]+1; - Matrix[i][j] = min(replace, insert,del); - //Matrix[i][j] = min((Matrix[i-1][j-1] + cost), (Matrix[i][j-1]+1),(Matrix[i-1][j]+1)); + int replace = Matrix[i - 1][j - 1] + cost; + int insert = Matrix[i][j - 1] + 1; + int del = Matrix[i - 1][j] + 1; + Matrix[i][j] = min(replace, insert, del); - } } + } + //matrix entry at the bottom right which represents the levenshteinDistance int LevenDist = Matrix[n][m]; - std::cout << "The Levenshtein distance between " << s << " and " << t << " is " << LevenDist << std::endl; - return 0; + + //return the distance + return LevenDist; } diff --git a/aufgabe1.h b/aufgabe1.h index 1b1e5b1b74a3adf51a18988ea8a724f0b19ed50a..d01fa9f3895a666aed45643ffc5d923d9aa69184 100644 --- a/aufgabe1.h +++ b/aufgabe1.h @@ -5,4 +5,4 @@ #include <string> int min(int x, int y, int z); -int levenshteinDistance(const std::string& s, const std::string& t); +int levenD(const std::string& s, const std::string& t); diff --git a/functions.cpp b/functions.cpp index c5e5eded8234419aadd9271ae84bfeb120014376..0472e74166cc7e8cffb0a573c22a38b7827acdcc 100644 --- a/functions.cpp +++ b/functions.cpp @@ -7,11 +7,12 @@ #include <stdexcept> #include <sstream> #include <vector> +#include "aufgabe1.h" // Aufgabe 3 -std::list<Cards> getListOfCards(const std::string& filename) { +std::list<Cards> getListOfCards(const std::string &filename) { std::ifstream file(filename); - if(!file) { + if (!file) { throw std::runtime_error("Error: Unable to open file " + filename); } //temp string to save line @@ -23,7 +24,7 @@ std::list<Cards> getListOfCards(const std::string& filename) { while (std::getline(file, temp)) { std::stringstream streamData(temp); //"cut" the string at the vertical bars(|) - char delim ='|'; + char delim = '|'; //string vector to hold the cut parts std::vector<std::string> stringVector; //temp to store Card attributes @@ -56,12 +57,6 @@ std::list<Cards> getListOfCards(const std::string& filename) { file.close(); - /* just for testing - for (const auto& card : CardsList) { - std::cout << card.name << ", " << card.mana << ", " << card.cmc << ", " << card.type << ", " << card.count << std::endl; - } - */ - //return the cards as a list return CardsList; } @@ -69,16 +64,16 @@ std::list<Cards> getListOfCards(const std::string& filename) { // Aufgabe 4 -void saveCardsToFile(const std::list<Cards>& cardsList) { +void saveCardsToFile(const std::list<Cards> &cardsList, const std::string &filename) { //Create new file - std::ofstream file("scrambledNew.txt"); + std::ofstream file(filename); //Check if file can be opened - if(!file) { + if (!file) { throw std::runtime_error("Error: Unable to open file."); } //Print every card object in CardsList into separate Line, add the vertical bar again - for (const auto& card : cardsList) { + for (const auto &card: cardsList) { file << card.name << "|" << card.mana << "|" << card.cmc << "|" << card.type << "|" << card.count << std::endl; } @@ -87,7 +82,7 @@ void saveCardsToFile(const std::list<Cards>& cardsList) { } //Aufgabe 5 -std::list<std::string> referenceNames(const std::string& referenceFile) { +std::list<std::string> referenceNames(const std::string &referenceFile) { std::ifstream file(referenceFile); //check if file can be opened if (!file) { @@ -105,12 +100,37 @@ std::list<std::string> referenceNames(const std::string& referenceFile) { //close file file.close(); - //for testing reasons - std::cout <<namesList.front() << std::endl; - std::cout <<namesList.back() << std::endl; + //return list + return namesList; +} - return namesList; +// Aufgabe 6 +void levenshteinDistance(const std::list<std::string> &intactNames, std::list<Cards> &brokenNames) { + for (auto &card: brokenNames) { + //reference value + double referenceV = 0.2675 * card.name.length(); + //bool for iterating + for (const auto &intactName: intactNames) { + //Following two lines are to remove the /r at the end of the string + std::string newIntact = intactName; + newIntact.erase(newIntact.size() - 1); + //calculate levenD of card for every intactName + int levenDist = levenD(card.name, newIntact); + + //if distance smaller than 26.75% of the card name length... + if (levenDist < referenceV) { + //replace broken name with intact name + card.name = newIntact; + //break and go on to next card + break; + } + } + } + //Aufgabe 7 + saveCardsToFile(brokenNames, "repaired.txt"); } + + diff --git a/functions.h b/functions.h index 0cdcbfb43e2a62fe0bea5ec669930b0f13014988..b55ad543749dd144764f2e8d70fb518b8badf732 100644 --- a/functions.h +++ b/functions.h @@ -2,15 +2,20 @@ // Created by Eray düzenli on 13.04.23. // #pragma once + #include <string> #include <iostream> #include <list> #include "Cards.h" -std::list<Cards> getListOfCards(const std::string& filename); -void saveCardsToFile(const std::list<Cards>& CardsList); -std::list<std::string> referenceNames(const std::string& referenceFile); +std::list<Cards> getListOfCards(const std::string &filename); + +void saveCardsToFile(const std::list<Cards> &CardsList, const std::string &filename); + +std::list<std::string> referenceNames(const std::string &referenceFile); + +void levenshteinDistance(const std::list<std::string> &intactNames, std::list<Cards> &brokenNames); diff --git a/main.cpp b/main.cpp index 1bd7891969afe667205a09a94c93c9e8089b7a8a..d0e1ee39f444eb01c7f4002bcb3f7eab3ceb3a12 100644 --- a/main.cpp +++ b/main.cpp @@ -4,28 +4,21 @@ #include "functions.h" int main() { - /* - std::cout << "Info 3 Projekt Nr.1" << std::endl; - levenshteinDistance("kitten", "sitting"); - levenshteinDistance("industry", "interests"); - levenshteinDistance("abcdef", "ghijkl"); - levenshteinDistance("hello", "hallo"); - levenshteinDistance("s", ""); - levenshteinDistance("", "s"); - Cards s; - s.cmc = 210; - s.count = "sf"; - s.name = "peter"; - s.type = "uwu"; - std::cout << s.name << s.count << s.type << s.cmc << std::endl; - */ - std::list<Cards> test; + + std::list<Cards> scrambledCards; + std::list<std::string> referenceCardNames; + //TODO relative path doesnt work - test = getListOfCards("/Users/erayduzenli/CLionProjects/magicCards/scrambled.txt"); + //Take scrambled cards and put them in a list + scrambledCards = getListOfCards("/Users/erayduzenli/CLionProjects/magicCards/scrambled.txt"); //TODO why does it save the file in the debug folder - saveCardsToFile(test); - referenceNames("/Users/erayduzenli/CLionProjects/magicCards/reference.txt"); + //take reference names and put them in a list + referenceCardNames = referenceNames("/Users/erayduzenli/CLionProjects/magicCards/reference.txt"); + + //calculate levenshteinDistance and create new file with intact names (if existing) + levenshteinDistance(referenceCardNames, scrambledCards); + return 0;