在两个文本文件中找到最常用的单词

Find the most used words in both text files

本文关键字:常用 单词 文件 文本 两个      更新时间:2023-10-16

我有两个文本文件,它们都多次使用相同的单词。我已经设法将它们都拉到数组中,并通过插入排序格式化了一个非格式化的txt文件。

现在我需要比较两个格式化的数组,找出最常见的单词,以及它们被使用了多少次。

我知道我可以使用for循环,遍历每个数组,但我不确定如何使用。任何帮助吗?

编辑:以下是我到目前为止写的。

#include<iostream>
#include<fstream>
#include<string>
using namespace std;
const int size = 100;
void checkIF(string x)
{
    fstream infile;
    cout << "Attempting to open ";
    cout << x;
    cout << "n";
    infile.open(x);
    if( !infile )
    {
        cout << "Error: File couldn't be opened.n";
    }
    else
    {
        cout << "File opened succsesfully.n";
    }
}
void checkFile()
{
    string f1 = "text1.txt", f2 = "abbreviations.txt";
    checkIF(f1);
    checkIF(f2);
}
string* readFiles(string txt1[],string abb[])
{
    fstream intxt1("text1.txt");
    fstream inabb("abbreviations.txt");
    int i = 0;
    while (!intxt1.eof())
    {   
        intxt1 >> txt1[i];
        //cout << txt1[i];
        i++;
    }
        while (!inabb.eof())
    {   
        inabb >> abb[i];
        //cout << abb[i];
        i++;
    }
    return txt1;
    return abb;
}
string* insertionSort(string txt1[], int arraySize)
{
    int i, j;
    string insert;
    for (i = 1; i < arraySize; i++)
    {
        insert = txt1[i];
        j = i;
        while ((j > 0) && (txt1[j - 1] > insert))
        {
            txt1[j] = txt1[j - 1];
            j = j - 1;
        }
        txt1[j] = insert;
    }
    return txt1;
}

void compare(string txt1[],string abb[])
{
}
void main()
{
    string txt1Words[size];
    string abbWords[size];
    checkFile();
    readFiles(txt1Words,abbWords);
    insertionSort(txt1Words,100);
    compare(txt1Words,abbWords);
    system("Pause");
}

也许你应该从一个hashmap开始,将每个单词映射到它的使用次数

使用vector代替数组

string txt1Words[size];

,

vector<string> txt1Words;

你可以直接使用

std::count(txt1Words.begin(), txt1Words.end(), word_to_search);

您可以为找到的每个单词使用映射。

std::map<std::string, int> wordmap;
for (int i = 0; i < arraylength; ++i)
{
   ++wordmap[array[i]];
}

我假设arraystd::string的数组。之后,您可以查询具有特定单词的映射并获得该单词的计数。

wordmap[word] // returns count for word

首先让我们解决"两个文本文件中使用最多的单词"的问题。这取决于你如何定义最常用的。您实际上有两组带有计数的单词。

例如

File A: "apple apple apple banana"

File B: "apple apple banana orange orange orange orange orange"

如果将其存储为一组名称和计数,则得到

文件A: {("apple",5), ("banana",1)}

文件B: {("apple",2), ("banana",1), ("orange",5)}

注意:这不是代码,它只是一个模拟符号。

那么在这个小例子中,文件最常使用的是什么呢?但问题是,"apple"是否应该使用最多,因为它在两个文件中都出现?或者"orange"应该是最常用的,因为它在一个文件中使用得最多?

我假设你想要两个集合的某种交点。因此,只有在两个文件中都出现的单词才算数。此外,如果我是你,我会根据出现的最少次数对单词进行排名,这样,文件A中的5个"苹果"就不会对"苹果"的权重过高,因为它在文件b中只出现了两次。

如果我们把它写成代码你会得到这样的东西

class Word
{
public:
    std::string Token;
    int Count;
    Word (const std::string &token, int count)
        : Token(token), Count(count) {}
};

    std::map<std::string, int> FileA;
    std::map<std::string, int> FileB;
    std::vector<Word> intersection;
    for (auto i = FileA.begin(); i != FileA.end (); ++i)
    {
        auto bentry = FileB.find (i->first); //Look up the word from A in B
        if (bentry == FileB.end ())
        {
            continue; //The word from file A was not in file B, try the next word
        }
        //We found the word from A in B
        intersection.push_back(Word (i->first,std::min(i->second,bentry->second))); //You can replace the std::min call with whatever method you want to qualitate "most common"
    }
    //Now sort the intersection by Count
    std::sort (intersection.begin(),intersection.end(), [](const Word &a, const Word &b) { return a.Count > b.Count;});
    for (auto i = intersection.begin (); i != intersection.end (); ++i)
    {
        std::cout << (*i).Token << ": " << (*i).Count << std::endl;
    }

看它运行:http://ideone.com/jbPm1g