简体   繁体   中英

Counting the most appearance word in the paragraph

Today I've got some problems with my code. The request is to read a txt file contain

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

and count the amount of words in a sentence, sentences in a paragraph, find the most appearance word in the paragraph, then write to file. The first two requests I have completed, but the last one, when I ran the code, it came:

"Monday", or nothing.

So can I ask for some advice to deal with my problems? The code is below. Thank you so much!

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


}

I would use std::multiset , for each word storing how many times it was found.

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
}

Then you can iterate through the elements, and return the one with the highest multiplicity:

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;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM