R中tidytext的停用词在从Gutenbergr下载时无法一致过滤。

3
这是一个奇怪的难题。我从古登堡项目下载了两个文本文件 - 《爱丽丝梦游仙境》和《尤利西斯》。 在《爱丽丝梦游仙境》中,停用词已经被去除了,但是在《尤利西斯》中仍然存在。 即使使用 "filter (!word %in% stop_words$word)" 替换 "anti_join",这个问题仍然存在。
我该如何将《尤利西斯》中的停用词去除?
感谢您的帮助!
阅读《爱丽丝梦游仙境》和《尤利西斯》的前15个tf_idf的图表请点击此处
library(gutenbergr)
library(dplyr)
library(stringr)
library(tidytext)
library(ggplot2)

titles <- c("Alice's Adventures in Wonderland", "Ulysses")


books <- gutenberg_works(title %in% titles) %>%
  gutenberg_download(meta_fields = c("title", "author"))


data(stop_words)


tidy_books <- books %>%
  unnest_tokens(word, text) %>%
  anti_join(stop_words) %>%
  count(title, word, sort=TRUE) %>%
  ungroup()


plot_tidy_books <- tidy_books %>%
  bind_tf_idf(word, title, n) %>%
  arrange(desc(tf_idf))       %>%
  mutate(word = factor(word, levels = rev(unique(word)))) %>%
  mutate(title = factor(title, levels = unique(title)))


plot_tidy_books %>%
  group_by(title) %>%
  arrange(desc(n))%>%
  top_n(15, tf_idf) %>%
  mutate(word=reorder(word, tf_idf)) %>%
  ggplot(aes(word, tf_idf, fill=title)) +
  geom_col(show.legend = FALSE) +
  labs(x=NULL, y="tf-idf") +
  facet_wrap(~title, ncol=2, scales="free") +
  coord_flip()
1个回答

4
在对Ulysses进行标记化处理后,经过一番挖掘,发现文本中的“it's”实际上使用的是右单引号而不是撇号。然而在tidytext中的stop_words则使用了撇号。因此你需要用撇号来替换右单引号。
我是通过以下方法发现这个问题的:
> utf8ToInt('it’s')
[1]  105  116 8217  115 

在谷歌搜索“8217”会带您到这里。从那里,您只需获取C++/Java源代码中的\u2019并在anti-join之前添加一个mutategsub语句即可。

tidy_books <- books %>%
  unnest_tokens(word, text) %>%
  mutate(word = gsub("\u2019", "'", word)) %>% 
  anti_join(stop_words) %>%
  count(title, word, sort=TRUE) %>%
  ungroup() 

运行结果:

输入图片描述


那些小引号/撇号真是太狡猾了!感谢您找出问题并教我如何修复它。 - Regis Maria O'Connor
如果这个答案对你有帮助,请接受它,这样其他人就会知道。 - Jake Kaupp
好的,我是新来的 - 会的! - Regis Maria O'Connor

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接