stringutils.cpp 7.38 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
#include "stringutils.hpp"

#include <algorithm>
#include <cstddef>
#include <numeric>
#include <string>


namespace cvv
{
namespace stfl
{

int stringEquality(const QString &str1, const QString &str2)
{
16 17 18 19 20
    if (isSingleWord(str1) && isSingleWord(str2))
    {
        return phoneticEquality(str1, str2);
    }
    return editDistance(str1, str2);
21 22 23 24
}

size_t editDistance(const QString &str1, const QString &str2)
{
25 26
    const unsigned len1 = str1.size();
    const unsigned len2 = str2.size();
27

28 29
    std::vector<size_t> col(len2 + 1);
    std::vector<size_t> prevCol(len2 + 1);
30

31 32
    // fills the vector with ascending numbers, starting by 0
    std::iota(prevCol.begin(), prevCol.end(), 0);
33

34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50
    for (unsigned i = 0; i < len1; i++)
    {
        col[0] = i + 1;
        for (unsigned j = 0; j < len2; j++)
        {
            if (str1[i] == str2[j])
                col[j + 1] =
                    std::min({ 1 + col[j], 1 + prevCol[1 + j],
                           prevCol[j] });
            else
                col[j + 1] =
                    std::min({ 1 + col[j], 1 + prevCol[1 + j],
                           prevCol[j] + 1 });
        }
        std::swap(col, prevCol);
    }
    return prevCol[len2];
51 52 53 54
}

int phoneticEquality(const QString &word1, const QString &word2)
{
55 56 57 58 59
    if (word1 == word2)
    {
        return 0;
    }
    return editDistance(nysiisForWord(word1), nysiisForWord(word2)) + 1;
60 61 62 63
}

QString nysiisForWord(QString word)
{
64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86
    static std::map<QString, QString> replacements = { { "MAC", "MCC" },
                                                   { "KN", "NN" },
                                                   { "K", "C" },
                                                   { "PH", "FF" },
                                                   { "PF", "FF" },
                                                   { "SCH", "SSS" } };
    static std::map<QString, QString> replacements2 = { { "EE", "Y" },
                                                    { "IE", "Y" },
                                                    { "DT", "D" },
                                                    { "RT", "D" },
                                                    { "NT", "D" },
                                                    { "ND", "D" } };
    static std::map<QString, QString> replacements3 = { { "EV", "AF" },
                                                    { "\xC3\x9C", "A" },
                                                    { "\xC3\x96", "A" },
                                                    { "\xC3\x84", "A" },
                                                    { "O", "G" },
                                                    { "Z", "S" },
                                                    { "M", "N" },
                                                    { "KN", "N" },
                                                    { "K", "C" },
                                                    { "SCH", "SSS" },
                                                    { "PH", "FF" } };
87

88 89 90 91
    if (word.isEmpty())
    {
        return "";
    }
92

93 94
    QString code;
    word = word.toUpper();
95

96
    replaceIfStartsWith(word, replacements);
97

98 99 100
    replaceIfEndsWith(word, replacements2);
    code.append(word[0]);
    word = word.right(word.size() - 1);
101

102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133
    while (word.size() > 0)
    {
        if (isVowel(word[0]))
            word[0] = QChar('A');
        replaceIfStartsWith(word, replacements);
        if (!(word.startsWith("H") &&
              (!isVowel(code[code.size() - 1]) ||
               (word.size() >= 2 && !isVowel(word[1])))) &&
            !(word.startsWith("W") && isVowel(code[code.size() - 1])))
        {
            if (word[0] != code[code.size() - 1])
            {
                code.append(word[0]);
            }
        }
        word = word.right(word.size() - 1);
    }
    if (code.endsWith("S"))
    {
        code = code.left(code.size() - 1);
    }
    if (code.endsWith("AY"))
    {
        code = code.right(code.size() - 1);
        code[code.size() - 1] = QChar('Y');
    }
    else if (code.endsWith("A"))
    {
        code = code.left(code.size() - 1);
    }
    code = removeRepeatedCharacters(code);
    return code;
134 135 136 137
}

QString nysiisForWordCached(const QString &word)
{
138 139 140 141 142 143 144 145 146 147 148 149 150
    static std::map<QString, QString> cache;
    if (word.isEmpty())
        return "";
    if (cache.count(word))
    {
        return cache[word];
    }
    else
    {
        QString code = nysiisForWord(word);
        cache[word] = code;
        return code;
    }
151 152 153 154
}

QString removeRepeatedCharacters(const QString &str)
{
155 156 157 158 159 160 161 162 163 164 165 166
    if (str.isEmpty())
    {
        return "";
    }
    QString res;
    res += str[0];
    auto iterator = str.begin();
    iterator++;
    std::copy_if(str.begin(), str.end(), std::back_inserter(res),
                 [res](QChar c)
    { return c != res[res.size() - 1]; });
    return res;
167 168 169 170 171
}

void replaceIfStartsWith(QString &str, const QString &search,
                         const QString &replacement)
{
172 173 174 175 176 177 178 179 180 181 182 183 184 185 186
    if (str.startsWith(search))
    {
        if (search.size() == replacement.size())
        {
            for (int i = 0; i < replacement.size(); i++)
            {
                str[i] = replacement[i];
            }
        }
        else
        {
            str = str.right(str.size() - search.size())
                      .prepend(replacement);
        }
    }
187 188 189 190 191
}

void replaceIfStartsWith(QString &word,
                         const std::map<QString, QString> &replacements)
{
192 193 194 195 196
    for (auto iterator = replacements.begin();
         iterator != replacements.end(); iterator++)
    {
        replaceIfStartsWith(word, iterator->first, iterator->second);
    }
197 198 199 200 201
}

void replaceIfEndsWith(QString &str, const QString &search,
                       const QString &replacement)
{
202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217
    if (str.endsWith(search))
    {
        if (search.length() == replacement.length())
        {
            for (int i = str.length() - replacement.length();
                 i < str.length(); i++)
            {
                str[i] = replacement[i];
            }
        }
        else
        {
            str = str.left(str.length() - search.length())
                      .append(replacement);
        }
    }
218 219 220 221 222
}

void replaceIfEndsWith(QString &word,
                       const std::map<QString, QString> &replacements)
{
223 224 225 226 227
    for (auto iterator = replacements.begin();
         iterator != replacements.end(); iterator++)
    {
        replaceIfEndsWith(word, iterator->first, iterator->second);
    }
228 229 230 231
}

bool isVowel(const QChar &someChar)
{
232 233 234
    static std::vector<QChar> vowels = { 'a', 'e', 'i', 'o', 'u' };
    return std::find(vowels.begin(), vowels.end(), someChar) !=
           vowels.end();
235 236 237 238
}

bool isSingleWord(const QString &str)
{
239 240 241
    const auto isLetter = [](QChar c)
    { return c.isLetter(); };
    return std::find_if_not(str.begin(), str.end(), isLetter) != str.end();
242 243 244 245
}

void unescapeCommas(QString &str)
{
246
    str.replace("\\,", ",");
247 248 249 250
}

QString shortenString(QString &str, int maxLength, bool cutEnd, bool fill)
{
251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267
    if (str.size() > maxLength)
    {
        if (cutEnd)
        {
            str = str.mid(0, maxLength - 1) + u8"…";
        }
        else
        {
            str = u8"…" +
                  str.mid(str.size() + 1 - maxLength, str.size());
        }
    }
    else if (fill)
    {
        str = str + QString(maxLength - str.size(), ' ');
    }
    return str;
268 269 270 271
}

QString asciiCharVectorToQString(std::vector<char> chars)
{
272
    return QString::fromStdString(std::string(chars.begin(), chars.end()));
273 274 275
}
}
}