计算段落中最外观的单词

时间:2018-05-06 17:28:14

标签: c++

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

的txt文件
  

"今天是星期天。明天是星期一。昨天是星期六。"

并计算句子中的单词数量,段落中的句子,找到段落中最外观的单词,然后写入文件。我完成的前两个请求,但最后一个,当我运行代码时,它来了:

  

"星期一"或者什么也没有。

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

#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;


}

1 个答案:

答案 0 :(得分: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;