如何从句子的向量中删除propwords
How to remove stopwords from a vector of sentences?
我正在处理一些需要从句子中删除stopwords的代码。我目前的解决方案不起作用。
我有两个测试句子的向量:
std::vector<std::string> sentences = {"this is a test", "another a test"};
我有一套无序的字符串,其中包含停车词:
std::unordered_set<std::string> stopwords;
现在,我试图循环浏览向量中的句子,检查并将每个单词与停止词进行比较,如果是停车词,则应删除停止字。
sentences.erase(std::remove_if(sentences.begin(), sentences.end(),
[](const std::string &s){return stopwords.find(s) != stopwords.end();}),
sentences.end());
的想法是,我的向量 - 删除stopwords-包含没有停止字的句子,但是目前,我得到了完全相同的句子。知道为什么?
我的无序集充满了以下功能:
void load() {
std::ifstream file;
file.open ("stopwords.txt");
if(!file.is_open()) {return;}
std::string stopword;
while (file >> stopword) {
stopwords.insert(stopword);
}
}
您的当前代码无法工作,因为您没有从每个字符串中删除单词。您的erase/remove_if
调用需要整个字符串,并尝试将集合中的单词与整个字符串匹配。
首先,您应该编写一个简单的功能,当给出std::string
和单词映射以删除时,请用已删除的单词返回字符串。
这是一个使用std::istringstream
的小函数,可以做到这一点:
#include <unordered_set>
#include <sstream>
#include <string>
#include <iostream>
std::string remove_stop_words(const std::string& src, const std::unordered_set<std::string>& stops)
{
std::string retval;
std::istringstream strm(src);
std::string word;
while (strm >> word)
{
if ( !stops.count(word) )
retval += word + " ";
}
if ( !retval.empty())
retval.pop_back();
return retval;
}
int main()
{
std::string test = "this is a test";
std::unordered_set<std::string> stops = {"is", "test"};
std::cout << "Changed word:n" << remove_stop_words(test, stops) << "n";
}
输出:
Changed word:
this a
因此,一旦您正确工作,std::vector
版本无非是通过向量中的每个项目循环并调用remove_stop_words
函数:
int main()
{
std::vector<std::string> test = {"this is a test", "another a test"};
std::unordered_set<std::string> stops = {"is", "test"};
for (size_t i = 0; i < test.size(); ++i)
test[i] = remove_stop_words(test[i], stops);
std::cout << "Changed words:n";
for ( auto& s : test )
std::cout << s << "n";
}
输出:
Changed words:
this a
another a
请注意,您可以利用std::transform
函数在上面的示例中删除手动循环:
#include <algorithm>
//...
int main()
{
std::vector<std::string> test = {"this is a test", "another a test"};
std::unordered_set<std::string> stops = {"is", "test"};
// Use std::transform
std::transform(test.begin(), test.end(), test.begin(),
[&](const std::string& s){return remove_stop_words(s, stops);});
std::cout << "Changed words:n";
for ( auto& s : test )
std::cout << s << "n";
}
相关文章:
- 将数组的地址分配给变量并删除
- 删除一个线程上有数百万个字符串的大型哈希映射会影响另一个线程的性能
- C/C++编译器通常会删除重复的库吗
- 从链接列表c++中删除一个项目
- C++如何通过用户输入删除列表元素
- 为什么在C++中使用私有复制构造函数与删除复制构造函数
- 是否需要删除包含对象的"pair"?
- 如何在自删除后将对象设置为nullptr
- 迭代时从向量和内存中删除对象
- 使用函数"remove"删除重复元素
- 如何从多映射中删除特定的重复项
- 运算符C++ "delete []"仅删除 2 个前值
- 删除指向指针的指针是运行时错误吗
- 将指针设置为"nullptr"并不能防止双重删除?
- 为什么示例代码访问IUnknown中已删除的内存
- 如何通过 getter 函数删除矢量的元素?
- 从控制台中删除最后打印的元素
- C++中的线程安全删除
- 如何从存储在std::映射中的std::集中删除元素
- 如何从句子的向量中删除propwords