c++ - How to read file and save hyphen using STL C++
问题描述
I have to read text file, convert it to lower case and remove non-alphabetic characters but also need to save hyphen and do not count it as a word. here is my coding. It is counting hyphen as word in UnknownWords
. I just want to save hyphen and just only want to count words which are on the left and right side of the hyphen in the .txt
.
My output:
110 Known words read
79 Unknown words read //it is because it is counting hyphen as word
Desired output is:
110 Known words read
78 Unknown words read
Code:
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;
}
解决方案
如果您不想放置一个单独的单词"-"
,请在添加到单词向量之前检查它:
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());
}
}
推荐阅读
- ajax - MethodArgumentTypeMismatchException:无法将“java.lang.String”类型的值转换为所需的“int”类型;
- java - 我们可以说 try-catch 块是 java 中处理异常的唯一方法吗?
- python - 使用 mongodb 时出现错误“NoneType”对象没有属性“is_absolute”
- python - Python圆形条形图中心太小
- asp.net-core-mvc - 在 ASP.NET Core MVC 中显示拒绝访问的登录模式
- laravel - 错误:Laravel 8 中唯一注册 URL 上的 500 SERVER ERROR.*
- node.js - 如何在反应节点中一次上传多个图像
- scope - 我的变量已定义,但 Julia 说不是?
- java - Android 从值中获取字符串资源 ID
- ios - BoringSSL 在启动时崩溃