added similarStrings() helper

This commit is contained in:
ZappoMan 2014-11-25 09:25:49 -08:00
parent a28d74570d
commit e9812d0608
2 changed files with 22 additions and 1 deletions

View file

@ -655,3 +655,24 @@ QString formatSecondsElapsed(float seconds) {
}
return result;
}
bool similarStrings(const QString& stringA, const QString& stringB) {
QStringList aWords = stringA.split(" ");
QStringList bWords = stringB.split(" ");
float aWordsInB = 0.0f;
foreach(QString aWord, aWords) {
if (bWords.contains(aWord)) {
aWordsInB += 1.0f;
}
}
float bWordsInA = 0.0f;
foreach(QString bWord, bWords) {
if (aWords.contains(bWord)) {
bWordsInA += 1.0f;
}
}
float similarity = 0.5f * (aWordsInB / (float)bWords.size()) + 0.5f * (bWordsInA / (float)aWords.size());
const float SIMILAR_ENOUGH = 0.5f; // half the words the same is similar enough for us
return similarity >= SIMILAR_ENOUGH;
}

View file

@ -131,6 +131,6 @@ bool isNaN(float value);
QString formatUsecTime(float usecs, int prec = 3);
QString formatSecondsElapsed(float seconds);
bool similarStrings(const QString& stringA, const QString& stringB);
#endif // hifi_SharedUtil_h