diff --git a/Cards.h b/Cards.h index 50b46310fc8fdbe7dcd0b99c09c2788f38e9e786..5302c9a306aba04b3021217d6bd4cc14ee167b68 100644 --- a/Cards.h +++ b/Cards.h @@ -1,6 +1,3 @@ -// -// Created by Eray düzenli on 13.04.23. -// #pragma once #include <string> diff --git a/aufgabe1.cpp b/aufgabe1.cpp index ce8b962d9f6b5d578403436da7f87339b2cf951f..cc76307986b48f18bf9ddba935b27a0c3baf7604 100644 --- a/aufgabe1.cpp +++ b/aufgabe1.cpp @@ -1,4 +1,3 @@ -#include <iostream> #include "aufgabe1.h" //Helper function, calculate minimum of three values @@ -12,7 +11,7 @@ int min(int x, int y, int z) { } } - +//Given two strings,calculate the levenshtein distance between them and return it as an integer int levenD(const std::string &s, const std::string &t) { //Length of first string int n = s.length(); diff --git a/aufgabe1.h b/aufgabe1.h index d01fa9f3895a666aed45643ffc5d923d9aa69184..106a7106f0c3726c0cef67a4afda5123da592195 100644 --- a/aufgabe1.h +++ b/aufgabe1.h @@ -1,6 +1,3 @@ -// -// Created by erayd on 11.04.2023. -// #pragma once #include <string> diff --git a/functions.cpp b/functions.cpp index 0472e74166cc7e8cffb0a573c22a38b7827acdcc..a949a2edb54f114643545df4eb1fe9d1bcf5bb76 100644 --- a/functions.cpp +++ b/functions.cpp @@ -1,6 +1,3 @@ -// -// Created by Eray düzenli on 13.04.23. -// #include <iostream> #include "functions.h" #include <fstream> @@ -10,6 +7,7 @@ #include "aufgabe1.h" // Aufgabe 3 +//Given a file of cards, return a list consisting of card objects std::list<Cards> getListOfCards(const std::string &filename) { std::ifstream file(filename); if (!file) { @@ -63,7 +61,7 @@ std::list<Cards> getListOfCards(const std::string &filename) { // Aufgabe 4 - +//Given a list of cards, save the card objects back into a file void saveCardsToFile(const std::list<Cards> &cardsList, const std::string &filename) { //Create new file std::ofstream file(filename); @@ -106,12 +104,15 @@ std::list<std::string> referenceNames(const std::string &referenceFile) { // Aufgabe 6 +//Calculate the levenshtein distance for two lists, replace the broken names with the intact names if possible and save +// in a new .txt file 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 + /* On macOS use this loop instead 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); @@ -125,6 +126,18 @@ void levenshteinDistance(const std::list<std::string> &intactNames, std::list<Ca //break and go on to next card break; } + } */ + for (const auto &intactName: intactNames) { + //calculate levenD of card for every intactName + int levenDist = levenD(card.name, intactName); + + //if distance smaller than 26.75% of the card name length... + if (levenDist < referenceV) { + //replace broken name with intact name + card.name = intactName; + //break and go on to next card + break; + } } } //Aufgabe 7 diff --git a/main.cpp b/main.cpp index d0e1ee39f444eb01c7f4002bcb3f7eab3ceb3a12..0295d3148230e2991f1989f8c3a1cc1c2b7f4044 100644 --- a/main.cpp +++ b/main.cpp @@ -1,4 +1,3 @@ -#include <iostream> #include "aufgabe1.h" #include "Cards.h" #include "functions.h" @@ -9,12 +8,10 @@ int main() { std::list<std::string> referenceCardNames; - //TODO relative path doesnt work //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 + scrambledCards = getListOfCards("../scrambled.txt"); //take reference names and put them in a list - referenceCardNames = referenceNames("/Users/erayduzenli/CLionProjects/magicCards/reference.txt"); + referenceCardNames = referenceNames("../reference.txt"); //calculate levenshteinDistance and create new file with intact names (if existing) levenshteinDistance(referenceCardNames, scrambledCards);