【问题标题】:Counting the number of stop words in a text计算文本中停用词的数量
【发布时间】:2018-10-01 23:19:18
【问题描述】:

我想知道是否有人可以帮助我解决以下问题: 我正在尝试确定客户评论文本中停用词的数量(计数)。我在 R 中使用“quanteda”包停用词列表。 我使用以下代码标记了文本并过滤掉了停用词:

stop.words <- tokens_select(corpus2.tokens, stopwords())

但是,我现在无法保存这些结果,以便我可以计算每条评论中包含的实际停用词数量。

任何提示将不胜感激。提前致谢!

【问题讨论】:

  • 请告诉我们您的确切预期输出是什么。
  • 我想创建一个新变量,其中包含每条评论的停用词计数。例如句子“我从来没有吃过更好的拉猪肉披萨!他们在上面分层的浇头数量令人震惊......培根,玉米,更多拉猪肉,而且酱汁很美味。我和我分享了我的披萨其他 2 个人。我等不及要回去了。我想创建一个值为“21”的变量,因为它包含 21 个停用词。希望这能澄清吗?我对编码和stackoverflow都很陌生。感谢您的帮助!
  • 为什么不简单地lengths(tokens_select(corpus2.tokens, stopwords()))
  • 嗨@StevenBeaupré,这更简单,正是我想要的 - 谢谢!
  • 很高兴它有帮助!

标签: r text-mining stop-words


【解决方案1】:

您可以使用来自stringrstr_detect(或来自stringistri_detect)来计算停用词的数量。 str_detect 将返回TRUEFALSE,这些你可以数数。根据您拥有的停用词列表,您可以获得不同的结果。 stopwords("en") 来自 stopwords 包将返回 28。如果您使用 stopwords(source = "smart"),您将得到 61。

text <- "I've never had a better pulled pork pizza! The amount of toppings that they layered on it was astounding...bacon, corn, more pulled pork, and the sauce was delicious. I shared my pizza with 2 other people. I can't wait to go back."
stopwords <- stopwords::stopwords("en")

sum(stringr::str_detect(tolower(text), stopwords))
28

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-04-21
    • 1970-01-01
    • 2015-07-13
    • 2022-01-01
    • 1970-01-01
    • 2021-09-17
    相关资源
    最近更新 更多