【解決方法】テキストファイルを分類しようとしています


I have 15 text files includes articles about different topics and I have another 5 text files each one includes token words about one topic politics, sports, weather, literature, history I want full (not just a sample) C++ code that read the five files that includes the token words and the 15 text files and classify each article with the appropriate topic using the tokens files then print each article with the token file which it classified to, and I want the classification by the frequency of each word in article comparing with its the frequency in token file and at the output each article with token file which it classified to

私が試したこと:

#include <iostream>
#include <fstream>
#include <string>
#include <unordered_map>
#include <vector>

using namespace std;

const int N = 20; // number of text files
const int M = 5;  // number of token files

// function to read a text file and return a vector of words
vector<string> readTextFile(string filename) {
    vector<string> words;
    string word;
    ifstream infile(filename);
    while (infile >> word) {
        words.push_back(word);
    }
    return words;
}

// function to read a token file and return a map of words to their frequencies
unordered_map<string, int> readTokenFile(string filename) {
    unordered_map<string, int> wordFreqs;
    string word;
    int freq;
    ifstream infile(filename);
    while (infile >> word >> freq) {
        wordFreqs[word] = freq;
    }
    return wordFreqs;
}

int main() {
    // read all the text files and store them in a vector
    vector<vector<string>> textFiles(N);
    for (int i = 0; i < N; i++) {
        string filename = "text" + to_string(i) + ".txt";
        textFiles[i] = readTextFile(filename);
    }

    // read all the token files and store them in a vector
    vector<unordered_map<string, int>> tokenFiles(M);
    for (int i = 0; i < M; i++) {
        string filename = "token" + to_string(i) + ".txt";
        tokenFiles[i] = readTokenFile(filename);
    }

    // classify each text file based on the token files
    for (int i = 0; i < N; i++) {
        // calculate the similarity score of the text file with each token file
        vector<double> scores(M);
        for (int j = 0; j < M; j++) {
            double score = 0;
            for (string word : textFiles[i]) {
                if (tokenFiles[j].count(word)) {
                    score += tokenFiles[j][word];
                }
            }
            scores[j] = score;
        }

        // find the token file with the highest similarity score
        int maxIdx = 0;
        for (int j = 1; j < M; j++) {
            if (scores[j] > scores[maxIdx]) {
                maxIdx = j;
            }
        }

        // print the text file and the token file it was classified to
        cout << "text" << i << ".txt" << " classified to token" << maxIdx << ".txt" << endl;
    }

    return 0;
}

解決策 1

引用:

これは出力ですが、問題が見つかりません。構文エラーなしで実行されます。
token0.txt に分類された text0.txt
text1.txt は token0.txt に分類されます
token0.txtに分類されたtext2.txt
token0.txtに分類されたtext3.txt
token0.txtに分類されたtext4.txt
token0.txtに分類されたtext5.txt
token0.txtに分類されたtext6.txt
token0.txtに分類されたtext7.txt
token0.txtに分類されたtext8.txt
token0.txtに分類されたtext9.txt
token0.txt に分類された text10.txt
token0.txtに分類されたtext11.txt
token0.txtに分類されたtext12.txt
token0.txtに分類されたtext13.txt
token0.txtに分類されたtext14.txt
text15.txt は token0.txt に分類されます
token0.txtに分類されたtext16.txt
token0.txtに分類されたtext17.txt
token0.txtに分類されたtext18.txt
token0.txtに分類されたtext19.txt

構文エラーはコンパイル時にのみ発生します。構文エラーがあると、実行可能ファイルが生成されないため、実行できません。

しかし、構文エラーは氷山の一角です。コンパイルが成功しても、コードが正しいとは限りません。 :笑う:
開発プロセスは電子メールを書くことと考えてください。コンパイルが成功したということは、電子メールを適切な言語 (たとえば、ドイツ語ではなく英語) で作成したことを意味します。電子メールに送信したいメッセージが含まれていたわけではありません。

これで、開発の第 2 段階に入ります (実際には第 4 段階または第 5 段階ですが、後で前の段階に進みます): テストとデバッグです。

それが何をするのか、そしてそれがあなたが望んでいたものとどのように違うのかを見ることから始めてください。 これは、なぜそれを行っているのかについての情報を提供するため、重要です。 たとえば、プログラムがユーザーに数字を入力させることを目的としており、それを2倍にして答えを出力する場合、入力/出力が次のようになると:

Input   Expected output    Actual output
  1            2                 1
  2            4                 4
  3            6                 9
  4            8                16

次に、問題がそれを2倍にするビットにあることは明らかです-それ自体を加算したり、2倍したりするのではなく、それ自体を乗算して入力の2乗を返します。
それで、コードを見ることができ、それがここのどこかにあることは明らかです:

C++
int Double(int value)
   {
   return value * value;
   }

何がうまくいかないのかがわかったら、デバッガーを使用して原因を突き止めます。 メソッドの最初の行にブレークポイントを置き、アプリを実行します。 ブレークポイントに到達すると、デバッガーが停止し、制御がユーザーに渡されます。 コードを行ごとに実行し (「シングル ステップ」と呼ばれます)、必要に応じて変数の内容を確認 (または変更) できるようになりました (コードを変更して、必要に応じて再試行することもできます)。
コードを実行する前に、コードの各行が何をすべきかを考え、「ステップ オーバー」ボタンを使用して各行を順番に実行したときに実際に何をしたかを比較します。 それはあなたが期待したことをしましたか? その場合は、次の行に進みます。
そうでない場合、なぜですか? どう違うの?
うまくいけば、そのコードのどの部分に問題があり、何が問題なのかを突き止めるのに役立つはずです。
これはスキルであり、開発だけでなく現実の世界でも役立つため、開発する価値のあるスキルです。 そして、すべてのスキルと同様に、それは使用することによってのみ向上します!

解決策 2

私が理解できる限りでは、 「トークンファイルに基づいて各テキストファイルを分類する」 間違っている。
そこで、記事内の単語の頻度を計算し (つまり、記事ごとに、トークン ファイルに使用されるものと同様のマップを作成します)、その頻度をトークン ファイルにリストされているものと比較する必要があります。

コメント

タイトルとURLをコピーしました