0

文字列を含む2つのベクトルがあります。vector1の各文字列をvector2の各文字列と比較し、両方の文字列で同じ単語がいくつあるかを確認します。私が持っているコードは、2つの文字列が完全に類似している場合にのみ機能します:

Compare::Compare(vector<string> text1, vector<string> text2, int ratio)
{
    text1Size_ = text1.size();
    text2Size_ = text2.size();

    if(text1Size_ > text2Size_)
    {
        totalWords_ = text1Size_;
    }
    else
    {
        totalWords_ = text2Size_;
    }

    it = text1.begin();

    for(int i = 0; i < text1Size_; i++)
    {
        it2 = text2.begin();

        for(int i = 0; i < text2Size_; i++)
        {
            if(*it == *it2)
            {
                cout << "Perfect match";
            }
            it2++;
        }
        it++;
    }
}

少なくとも類似した単語の比率がある場合は、類似した各文字列を返す必要があります。

各文字列を解析し、各単語を配列に入れて比較するよりも簡単な方法はありますか?

-編集-

言葉で私は「鳥」のような書かれた言葉を意味します。例を挙げましょう。

ベクトルごとに1つの文字列しかなく、70%の類似率が必要だとします。

string1 : The blue bird.
string2 : The bird.

私がやりたいのは、両方の文で一致する書かれた単語の少なくとも60%があるかどうかを確認することです。

ここに、一致する「The」と「Bird」があります。だから私は2/3の類似した単語を持っています(66.666%)。したがって、これらの文字列は受け入れられます。

-編集2-

ここで「.compare()」を使用することはできないと思います。これは、書かれた各単語ではなく、各文字をチェックするためです...

4

1 に答える 1

1

文字列ストリームを使用して、文字列を単語に分割します。

#include <sstream>

bool is_similar(string str1, string str2)
{
    vector<string> words1, words2;
    string temp;

    // Convert the first string to a list of words
    std::stringstream stringstream1(str1);
    while (stringstream1 >> temp)
        words1.push_back(temp);

    // Convert the second string to a list of words
    std::stringstream stringstream2(str2);
    while (stringstream2 >> temp)
        words2.push_back(temp);

    int num_of_identical_words = 0;
    // Now, use the code you already have to count identical words
    ...

    double ratio = (double)num_of_identical_words / words2.size();
    return ratio > 0.6;
}
于 2012-10-11T18:49:56.893 に答える