gpt4 book ai didi

c++ - 统计段落中出现次数最多的词

转载 作者:太空宇宙 更新时间:2023-11-04 12:48:30 26 4
gpt4 key购买 nike

今天我的代码遇到了一些问题。请求是读取一个包含

的txt文件

"Today is Sunday. Tomorrow is Monday. Yesterday was Saturday."

并统计一个句子、一个段落中的句子的词数,找到该段中出现次数最多的词,然后写入文件。前两个请求我已经完成了,但是最后一个,当我运行代码时,它来了:

"Monday", or nothing.

那么我可以寻求一些建议来处理我的问题吗?代码如下。非常感谢!

#include <algorithm>
#include <fstream>
#include <iostream>
#include <string>
#include <vector>
using namespace std;
int main()
{

ifstream is;
is.open("test.txt", ios::in);
string total = "";
if (is.is_open())
{
string line = "";
while (getline(is, line))
{
total += line;
}

is.close();
}
ofstream os;
os.open("tes.txt", ios::out);
os << total << endl;
os.close();
vector<string> sen_vector;
size_t prev_pos = 0;
size_t cur_pos = total.find_first_of("!?.");
while (cur_pos != string::npos)
{
string sen = total.substr(prev_pos, cur_pos - prev_pos);
sen_vector.push_back(sen);
prev_pos = cur_pos + 2;
cur_pos = total.find_first_of("!?.", prev_pos);
}
vector<vector<string>> para_vector;

for (int i = 0; i < sen_vector.size(); i++)
{
vector<string> temp;

string sen = sen_vector[i] + " ";
size_t prev_pos_w = 0;
size_t cur_pos_w = sen.find(' ', prev_pos_w);
while (cur_pos_w != string::npos)
{
string word = sen.substr(prev_pos_w, cur_pos_w - prev_pos_w);
temp.push_back(word);
prev_pos_w = cur_pos_w + 1;
cur_pos_w = sen.find(' ', prev_pos_w);
}
para_vector.push_back(temp);
}

for (int i = 0; i < para_vector.size(); i++)
{
for (int j = 0; j < para_vector[i].size(); j++)
{
cout << para_vector[i][j] << ' ';
}
}
cout << endl;
cout << "So cau trong doan: " << size(para_vector) << endl; // Amount of sentences in a paragraph.
for (int i = 0; i<sen_vector.size(); i++)
cout << "So tu trong cau " << i + 1 << " la: " << size(para_vector[i]) << endl; // Amount of words in a sentence.
string a[100], d[100];
int n = 0;
for (int i = 0; i < sen_vector.size(); i++) // From sentence to sentence-array
{
a[i] = sen_vector[i] + " ";
n++;
}
cout << endl;
int dem = 0, m = 0, vt = 0;
int b[100], dt = 0;
for (int i = 0; i < sen_vector.size(); i++) // From sentence-array to word-array
{
size_t prev_pos_w = 0;
size_t cur_pos_w = a[i].find(' ', prev_pos_w);

for (int j = 0; j < n; j++)
{
while (cur_pos_w != string::npos)
{
d[i] = a[i].substr(prev_pos_w, cur_pos_w - prev_pos_w);
prev_pos_w = cur_pos_w + 1;
cur_pos_w = a[i].find(' ', prev_pos_w);
cout << d[i] << " ";
dt++;
}

}
}

/*for (int i = 0; i < dt-1; i++) // I got confused with these code (it came nothing when ran)
{
for (int j = 1; j < dt; j++)
{
if (d[i] == d[j])
{
count++;
}
}
b[i] = count;
}
int max = 0;
for (int i = 0; i <= n; i++)
{
if (max < b[i])
{
max = b[i];
vt = i;
}
}
cout << d[vt];*/
system("pause");
return 0;


}

最佳答案

我会使用 std::multiset,为每个单词存储它被找到的次数。

std::multiset<std::string> word_set;

std::string word;
while (is >> word) {
word_set.insert(word); // it might be a good idea to remove non-word chars
}

然后你可以遍历元素,并返回具有最高重数的那个:

std::string most_seen = "";
int count = 0;

for (std::string i : word_set) {
if (word_set.count(i) > count)
most_seen = i;
}
return most_seen;

关于c++ - 统计段落中出现次数最多的词,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50202590/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com