如何从句子的向量中删除propwords

How to remove stopwords from a vector of sentences?

本文关键字:删除 propwords 向量 句子      更新时间:2023-10-16

我正在处理一些需要从句子中删除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";
}