07_STD: Änderungen an string_similarity

This commit is contained in:
Johannes Theiner 2018-11-17 00:29:24 +01:00
parent 789099fbde
commit 6ef408d6db
1 changed files with 123 additions and 37 deletions

View File

@ -6,25 +6,27 @@
#include <list>
#include <algorithm>
#include <unordered_map>
#include <set>
#include <map>
#include "grundgeruest.hpp"
struct BinaryOctet {
bool evenParity;
char bitsAsDigits[8];
bool evenParity;
char bitsAsDigits[8];
BinaryOctet(int i) {
BinaryOctet(int i) {
}
}
};
int main() {
std::srand(static_cast<unsigned int>(std::time(0)));
std::srand(static_cast<unsigned int>(std::time(nullptr)));
int array[41];
std::default_random_engine generator;
std::uniform_int_distribution<int> distribution(0, 23);
for(int i = 0; i <= 41; i++) {
for (int i = 0; i <= 41; i++) {
int rnd = distribution(generator);
array[i] = rnd;
}
@ -35,24 +37,103 @@ int main() {
//TODO: Punkte gleichmässig verteilen
int stringSimilarity(std::string string1, std::string string2) {
std::string alphabet = "abcdefghijklmnopqrstuvwxyzäöü";
//wenn gleicher String, gleich 100 Punkte geben, spart den Rest
if(string1 == string2) return 100;
if (string1 == string2) return 100;
int points = 0;
//gleiche Länge +10 Punkte
if(string1.length() == string2.length()) points += 10;
if (string1.length() == string2.length()) points += 10;
//Anzahl der verschiedenen Zeichen zählen
std::map<char, int> charCount;
std::map<char, int> charCountString1;
std::map<char, int> charCountString2;
for (char c : string1) {
charCount[c]++;
charCountString1[c]++;
}
for (char c : string2) {
charCount[c]++;
charCountString2[c]++;
}
std::cout << charCount.size() << " : " << charCountString1.size() << " : " << charCountString2.size() << std::endl;
int equalCharsPercentage = static_cast<int>((double(charCountString1.size()) / double(charCount.size())) * 100);
points += equalCharsPercentage / 10;
/*//Anzahl der verschiedenen Zeichen zählen
std::unordered_map<char, int> string1map;
std::unordered_map<char, int> string2map;
std::unordered_map<char, int> characterCount;
std::set<char> characters;
for(char c : string1) {
characters.insert(c);
string1map[c]++;
characterCount[c]++;
}
for(char c : string2) {
characters.insert(c);
string2map[c]++;
characterCount[c]++;
}
@ -60,26 +141,30 @@ int stringSimilarity(std::string string1, std::string string2) {
//gleiche Anzahl an verschiedenen Zeichen +10 Punkte
if(string1map.size() == string2map.size()) points += 10;
int equalCharCount = 0;
int caseSensitiveCount = 0;
for(std::pair<char, int> c : characterCount) {
//gleiche Anzahl an gleichen Zeichen in beiden überprüfen
int charCount = c.second / 2;
if(charCount == string1map[c.first] && charCount == string2map[c.first]) {
equalCharCount++;
caseSensitiveCount++;
}
if(islower(c.first)) {
c.first = static_cast<char>(toupper(c.first));
}
if(isupper(c.first)) c.first = tolower(c.first);
}
//gleiche Anzahl an gleichen Zeichen +10 Punkte
if(equalCharCount == characterCount.size()) points += 10;
if(caseSensitiveCount == characterCount.size()) points += 10;
int caseInsensitiveCount = 0;
for(std::pair<char, int> c : characterCount) {
int charCount = c.second / 2;
if(charCount == string1map[c.first] && charCount == string2map[c.first]) {
caseInsensitiveCount++;
}
}
//TODO: Punkte für gleiche Buchstaben und gleiche Buchstaben Anzahl vergeben
if(caseInsensitiveCount == characterCount.size()) points += 10;*/
return points;
@ -91,21 +176,22 @@ void unittest_stringSimilarity() {
std::cout << "Hello World | Hallo Welt ---> " << stringSimilarity("Hello World", "Hallo Welt") << std::endl;
std::cout << "bla | ping ---> " << stringSimilarity("bla", "ping") << std::endl;
std::cout << "Lorem ipsum..... | Lorem ipsum ---> " << stringSimilarity("Lorem ipsum dolor sit amet, "
"consetetur sadipscing elitr, "
"sed diam nonumy eirmod tempor"
"invidunt ut labore et dolore "
"magna aliquyam erat, sed diam voluptua. "
"At vero eos et accusam et justo duo dolores "
"et ea rebum. Stet clita kasd gubergren, "
"no sea takimata sanctus est Lorem "
"ipsum dolor sit amet. "
"Lorem ipsum dolor sit amet, "
"consetetur sadipscing elitr, "
"sed diam nonumy eirmod tempor "
"invidunt ut labore et dolore magna "
"aliquyam erat, sed diam voluptua. "
"At vero eos et accusam et justo duo dolores "
"et ea rebum. Stet clita kasd gubergren, "
"no sea takimata sanctus est Lorem ipsum "
"dolor sit amet.", "Lorem ipsum") << std::endl;
"consetetur sadipscing elitr, "
"sed diam nonumy eirmod tempor"
"invidunt ut labore et dolore "
"magna aliquyam erat, sed diam voluptua. "
"At vero eos et accusam et justo duo dolores "
"et ea rebum. Stet clita kasd gubergren, "
"no sea takimata sanctus est Lorem "
"ipsum dolor sit amet. "
"Lorem ipsum dolor sit amet, "
"consetetur sadipscing elitr, "
"sed diam nonumy eirmod tempor "
"invidunt ut labore et dolore magna "
"aliquyam erat, sed diam voluptua. "
"At vero eos et accusam et justo duo dolores "
"et ea rebum. Stet clita kasd gubergren, "
"no sea takimata sanctus est Lorem ipsum "
"dolor sit amet.", "Lorem ipsum")
<< std::endl;
}