在两个文本文件中找到最常用的单词
Find the most used words in both text files
我有两个文本文件,它们都多次使用相同的单词。我已经设法将它们都拉到数组中,并通过插入排序格式化了一个非格式化的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]];
}
我假设array
是std::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
- 文本文件中的单词链表
- 在指针的帮助下,文本文件中单词的频率
- 如何根据单词在文本中出现的概率输出单词
- 从字符串变量中逐字符读取单词
- 使用std::mt19937从字符串中返回一个随机单词
- 如何用for循环在c++中生成单词三角形
- 将数字转换为单词
- 使用if-else将数字转换为单词
- C++-字符串是否包含一个带有简单循环的单词
- 使用单词"not"作为C ++类的名称会导致VS2019错误
- 为什么这个程序返回最后一个单词而不是最长的单词?
- 3-3. 编写一个程序来计算每个不同单词在其输入中出现的次数
- C++为一串单词添加空格
- 在C++中查找(奇怪的)字符串中的单词
- 当字符串是某个单词时给出输出?
- 当您在此单词中搜索单词时调整字符数组的大小?
- 数每个单词的元音
- 向量中的常用单词用C++表示单词出现的次数
- 使用哈希图 c++ 查找最常用的单词
- 在两个文本文件中找到最常用的单词