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,6 +6,8 @@
#include <list>
#include <algorithm>
#include <unordered_map>
#include <set>
#include <map>
#include "grundgeruest.hpp"
struct BinaryOctet {
@ -18,7 +20,7 @@ struct BinaryOctet {
};
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;
@ -35,7 +37,6 @@ 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;
int points = 0;
@ -43,16 +44,96 @@ int stringSimilarity(std::string string1, std::string string2) {
//gleiche Länge +10 Punkte
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;
@ -107,5 +192,6 @@ void unittest_stringSimilarity() {
"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;
"dolor sit amet.", "Lorem ipsum")
<< std::endl;
}