创建文本文件中出现的前10个单词的直方图

Create a histogram of the top 10 words that appear in a text file

本文关键字:10个 单词 直方图 文本 文件 创建      更新时间:2023-10-16

我正在尝试创建一个文本中前十个单词出现频率的直方图(以百分比为单位)。它需要采用与此类似的格式:

用于:-------------------20(百分比和20个破折号)

是:---------------15

他:----------10

战争:-----5

有:-----5

英雄:-----5

有:----4

女孩:-----5

愤怒:----4

其他:-------------------------------32

我知道我需要一个10的for循环,在它里面还有一个带破折号的循环,去百分比

int main(){

string file_name;
cout << "Enter the file name, the odyssey or pride and prejudice, that you would like to analyze: ";
getline(cin,file_name);
vector<string> words; //loading words into vector called words
if(file_name == "test")
{
    words = words_to_vector("test.txt"); //used a test files so it would take less time
}
else if(file_name == "pride and prejudice")
{
    words = words_to_vector("pride-prejudice.txt");
}
else if (file_name == "the odyssey")
{
    words = words_to_vector("the-odyssey.txt");
}
else
{
    cout << "Sorry, the file name you entered is invalid.";
}
vector<word_stats> stats = get_word_stats(words); // calculating the number of words, and how many of those words are unique
cout << endl << endl << "Total words: " << words.size() << ", " << stats.size() << " unique" << endl;
sort(stats.begin(), stats.end(), compare_stats);
cout << "Most common word is '" << stats[0].word << "' occuring " << stats[0].count << " times." << endl;

int topMostCount = 10; //where I need help putting this into a histogram
//int totalWords = words.size();
int totalUniqueWords = stats.size();
//most common
for (int i=0; i<stats.size() && i<topMostCount; i++) {
    cout << stats[i].word << ": " << stats[i].count << endl;
}

创建一个生成破折号并将其输出为const char*的方法怎么样?

for(int i = 0; i < stats.size() && i < topMostCount; i++){
    int percent = (double)stats[i].count / words.size * 100.00;
    const char * dashes = genDashes(percent);
    cout << stats[i].word << ": " << dashes << percent << "%";
}

genDashes函数如下所示:

const char * genDashes(int n)
{
    std::stringstream ss_dashes;
    for(int i = 0; i < n; i++){
        ss_dashes << "-";
    }
    std::string s_dashes = ss_dashes.str();
    return s_dashes.c_str();
}