gpt4 book ai didi

c++ - 如何使用 STL C++ 读取文件和保存连字符

转载 作者:行者123 更新时间:2023-11-28 04:40:28 24 4
gpt4 key购买 nike

我必须读取文本文件,将其转换为小写并删除非字母字符,但还需要保存连字符并且不将其计为单词。这是我的编码。它将连字符计为 UnknownWords 中的单词。我只想保存连字符,只想计算 .txt 中连字符左右两侧的单词。

我的输出:

110 Known words read
79 Unknown words read //it is because it is counting hyphen as word

期望的输出是:

110 Known words read
78 Unknown words read

代码:

void WordStats::ReadTxtFile(){
std::ifstream ifile(Filename);
if(!ifile)
{
std::cerr << "Error Opening file " << Filename << std::endl;
exit(1);
}
for (std::string word; ifile >> word; )
{

transform (word.begin(), word.end(), word.begin(), ::tolower);
word.erase(std::remove_if(word.begin(), word.end(), [](char c)
{
return (c < 'a' || c > 'z') && c != '\'' && c != '-';
}), word.end());
if (Dictionary.count(word))
{
KnownWords[word].push_back(ifile.tellg());
}
else
{
UnknownWords[word].push_back(ifile.tellg());
}
}
// std::string word; ifile >> word;


std::cout << KnownWords.size() << " known words read." << std::endl;
std::cout << UnknownWords.size() << " unknown words read." << std::endl;
}

最佳答案

如果您不想单独放置一个只有 "-" 的单词,请在添加到单词 vector 之前检查它:

for (std::string word; ifile >> word; )
{

transform (word.begin(), word.end(), word.begin(), ::tolower);
word.erase(std::remove_if(word.begin(), word.end(), [](char c)
{
return (c < 'a' || c > 'z') && c != '\'' && c != '-';
}), word.end());
if (word.find_first_not_of("-") == string::npos) { // Ignore word that's only hyphens
continue;
}
if (Dictionary.count(word))
{
KnownWords[word].push_back(ifile.tellg());
}
else
{
UnknownWords[word].push_back(ifile.tellg());
}
}

关于c++ - 如何使用 STL C++ 读取文件和保存连字符,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50264186/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com