From d2f3e022db95b8ab87b886e35c016f40264cda0e Mon Sep 17 00:00:00 2001 From: erayosso <erayduezenli@googlemail.com> Date: Tue, 11 Apr 2023 23:46:27 +0200 Subject: [PATCH] Implemented Algorithm for levenshtein Distance --- .idea/magicCards.iml | 8 +------ .idea/vcs.xml | 6 +++++ CMakeLists.txt | 2 +- aufgabe1.cpp | 57 ++++++++++++++++++++++++++++++++++++++++++++ aufgabe1.h | 8 +++++++ main.cpp | 7 ++++++ 6 files changed, 80 insertions(+), 8 deletions(-) create mode 100644 .idea/vcs.xml create mode 100644 aufgabe1.cpp create mode 100644 aufgabe1.h diff --git a/.idea/magicCards.iml b/.idea/magicCards.iml index bc2cd87..f08604b 100644 --- a/.idea/magicCards.iml +++ b/.idea/magicCards.iml @@ -1,8 +1,2 @@ <?xml version="1.0" encoding="UTF-8"?> -<module type="CPP_MODULE" version="4"> - <component name="NewModuleRootManager"> - <content url="file://$MODULE_DIR$" /> - <orderEntry type="inheritedJdk" /> - <orderEntry type="sourceFolder" forTests="false" /> - </component> -</module> \ No newline at end of file +<module classpath="CMake" type="CPP_MODULE" version="4" /> \ No newline at end of file diff --git a/.idea/vcs.xml b/.idea/vcs.xml new file mode 100644 index 0000000..35eb1dd --- /dev/null +++ b/.idea/vcs.xml @@ -0,0 +1,6 @@ +<?xml version="1.0" encoding="UTF-8"?> +<project version="4"> + <component name="VcsDirectoryMappings"> + <mapping directory="" vcs="Git" /> + </component> +</project> \ No newline at end of file diff --git a/CMakeLists.txt b/CMakeLists.txt index 68991b2..91af747 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -3,4 +3,4 @@ project(magicCards) set(CMAKE_CXX_STANDARD 20) -add_executable(magicCards main.cpp) +add_executable(magicCards main.cpp aufgabe1.h aufgabe1.cpp) diff --git a/aufgabe1.cpp b/aufgabe1.cpp new file mode 100644 index 0000000..49c3fa8 --- /dev/null +++ b/aufgabe1.cpp @@ -0,0 +1,57 @@ +#include <iostream> +#include "aufgabe1.h" + +//calculate minimum of three values +int min(int x, int y, int z) { + if (x < y && x < z) { + return x; + } else if (y < z) { + return y; + } else { + return z; + } +} + + +int levenshteinDistance(const std::string& s, const std::string& t) { + //Length of first string + int n = s.length(); + //Length of second string + int m = t.length(); + int cost = 0; + + //Matrix to hold the levenshtein distance + int Matrix[n + 1][m + 1]; + + // Initialize the first row and column of the matrix,[0][0] is empty + for (int i = 0; i <= n; i++) { + Matrix[i][0] = i; + } + for (int j = 0; j <= m; j++) { + Matrix[0][j] = j; + } + + // Calculate the Levenshtein distances for all pairs of characters + for (int i = 1; i <= n; i++) { + for (int j = 1; j <= m; j++) { + + 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 LevenDist = Matrix[n][m]; + std::cout << "The Levenshtein distance between " << s << " and " << t << " is " << LevenDist << std::endl; + return 0; +} + diff --git a/aufgabe1.h b/aufgabe1.h new file mode 100644 index 0000000..1b1e5b1 --- /dev/null +++ b/aufgabe1.h @@ -0,0 +1,8 @@ +// +// Created by erayd on 11.04.2023. +// +#pragma once +#include <string> + +int min(int x, int y, int z); +int levenshteinDistance(const std::string& s, const std::string& t); diff --git a/main.cpp b/main.cpp index 48139b9..6639000 100644 --- a/main.cpp +++ b/main.cpp @@ -1,6 +1,13 @@ #include <iostream> +#include "aufgabe1.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"); return 0; } -- GitLab