07_STD: Änderungen an string_similarity
This commit is contained in:
parent
789099fbde
commit
6ef408d6db
|
@ -6,6 +6,8 @@
|
||||||
#include <list>
|
#include <list>
|
||||||
#include <algorithm>
|
#include <algorithm>
|
||||||
#include <unordered_map>
|
#include <unordered_map>
|
||||||
|
#include <set>
|
||||||
|
#include <map>
|
||||||
#include "grundgeruest.hpp"
|
#include "grundgeruest.hpp"
|
||||||
|
|
||||||
struct BinaryOctet {
|
struct BinaryOctet {
|
||||||
|
@ -18,13 +20,13 @@ struct BinaryOctet {
|
||||||
};
|
};
|
||||||
|
|
||||||
int main() {
|
int main() {
|
||||||
std::srand(static_cast<unsigned int>(std::time(0)));
|
std::srand(static_cast<unsigned int>(std::time(nullptr)));
|
||||||
int array[41];
|
int array[41];
|
||||||
|
|
||||||
std::default_random_engine generator;
|
std::default_random_engine generator;
|
||||||
std::uniform_int_distribution<int> distribution(0, 23);
|
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);
|
int rnd = distribution(generator);
|
||||||
array[i] = rnd;
|
array[i] = rnd;
|
||||||
}
|
}
|
||||||
|
@ -35,24 +37,103 @@ int main() {
|
||||||
|
|
||||||
//TODO: Punkte gleichmässig verteilen
|
//TODO: Punkte gleichmässig verteilen
|
||||||
int stringSimilarity(std::string string1, std::string string2) {
|
int stringSimilarity(std::string string1, std::string string2) {
|
||||||
std::string alphabet = "abcdefghijklmnopqrstuvwxyzäöü";
|
|
||||||
//wenn gleicher String, gleich 100 Punkte geben, spart den Rest
|
//wenn gleicher String, gleich 100 Punkte geben, spart den Rest
|
||||||
if(string1 == string2) return 100;
|
if (string1 == string2) return 100;
|
||||||
int points = 0;
|
int points = 0;
|
||||||
|
|
||||||
//gleiche Länge +10 Punkte
|
//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> string1map;
|
||||||
std::unordered_map<char, int> string2map;
|
std::unordered_map<char, int> string2map;
|
||||||
std::unordered_map<char, int> characterCount;
|
std::unordered_map<char, int> characterCount;
|
||||||
|
std::set<char> characters;
|
||||||
|
|
||||||
for(char c : string1) {
|
for(char c : string1) {
|
||||||
|
characters.insert(c);
|
||||||
string1map[c]++;
|
string1map[c]++;
|
||||||
characterCount[c]++;
|
characterCount[c]++;
|
||||||
}
|
}
|
||||||
for(char c : string2) {
|
for(char c : string2) {
|
||||||
|
characters.insert(c);
|
||||||
string2map[c]++;
|
string2map[c]++;
|
||||||
characterCount[c]++;
|
characterCount[c]++;
|
||||||
}
|
}
|
||||||
|
@ -60,26 +141,30 @@ int stringSimilarity(std::string string1, std::string string2) {
|
||||||
//gleiche Anzahl an verschiedenen Zeichen +10 Punkte
|
//gleiche Anzahl an verschiedenen Zeichen +10 Punkte
|
||||||
if(string1map.size() == string2map.size()) points += 10;
|
if(string1map.size() == string2map.size()) points += 10;
|
||||||
|
|
||||||
int equalCharCount = 0;
|
int caseSensitiveCount = 0;
|
||||||
for(std::pair<char, int> c : characterCount) {
|
for(std::pair<char, int> c : characterCount) {
|
||||||
//gleiche Anzahl an gleichen Zeichen in beiden überprüfen
|
//gleiche Anzahl an gleichen Zeichen in beiden überprüfen
|
||||||
int charCount = c.second / 2;
|
int charCount = c.second / 2;
|
||||||
if(charCount == string1map[c.first] && charCount == string2map[c.first]) {
|
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
|
//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++;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if(caseInsensitiveCount == characterCount.size()) points += 10;*/
|
||||||
//TODO: Punkte für gleiche Buchstaben und gleiche Buchstaben Anzahl vergeben
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
return points;
|
return points;
|
||||||
|
@ -107,5 +192,6 @@ void unittest_stringSimilarity() {
|
||||||
"At vero eos et accusam et justo duo dolores "
|
"At vero eos et accusam et justo duo dolores "
|
||||||
"et ea rebum. Stet clita kasd gubergren, "
|
"et ea rebum. Stet clita kasd gubergren, "
|
||||||
"no sea takimata sanctus est Lorem ipsum "
|
"no sea takimata sanctus est Lorem ipsum "
|
||||||
"dolor sit amet.", "Lorem ipsum") << std::endl;
|
"dolor sit amet.", "Lorem ipsum")
|
||||||
|
<< std::endl;
|
||||||
}
|
}
|
Loading…
Reference in New Issue