【问题标题】:How to remove odd characters in R wordcloud如何删除 R wordcloud 中的奇数字符
【发布时间】:2019-06-19 03:08:03
【问题描述】:

我正在尝试使用语料库和各种 tm_map 函数在 R 中构建词云。问题是我不断返回这个奇怪的符号,带有欧元符号和倒置引号的那个。它在我的语料库中排名第二。 (还有一两个其他的,但它们远没有那么频繁,所以问题不大。)

Word cloud with rogue €“

任何想法如何摆脱这个?

这是一个 .txt 格式的文本样本,在它被拉入 R 之前:

迁移到 Virtual Replication 6 后,AWS 内部和外部的复制功能在以前只有单向功能的情况下添加到了 Amazon 云存储平台中。 Zerto 技术布道者 Gjisbert Janssen van Doorn 说,在 AWS 中进行开发需要更长的时间。 “我们从 Azure 开始双向复制。我们尝试通过 API 为我们支持的云进行本地开发,但使用 AWS 需要更长的时间。” Zerto 还通过 IBM Cloud 添加了双向复制。 van Doorn 表示,该公司没有计划增加对谷歌云平台的支持。 “这是我们一直在关注的事情。它在愿望清单上,而不是在路线图上,”他说。

这是通过 Corpus() 拉入 R 后的结果:

迁移到 Virtual Replication 6 后,AWS 内部和外部的复制功能在以前只是单向的,并添加到了 Amazon 云存储平台中。\n\nZerto 技术布道者 Gjisbert 说,在 AWS 中进行开发需要更长的时间詹森·范·多恩。 “我们从 Azure 开始双向复制。我们尝试通过 API 为我们支持的云进行本地开发,但使用 AWS 需要更长的时间。”Zerto 还使用 IBM Cloud 添加了双向复制。 van Doorn 表示,该公司没有计划增加对谷歌云平台的支持。 “这是我们一直在关注的事情。它在愿望清单上,而不是在路线图上,”他说。

然后我运行这段代码:

# Convert the text to lower case
corpus <- tm_map(corpus, content_transformer(tolower))
# Remove numbers
corpus <- tm_map(corpus, removeNumbers)
# Remove english common stopwords
corpus <- tm_map(corpus, removeWords, stopwords("english"))
# Remove punctuations
corpus <- tm_map(corpus, removePunctuation)
# Remove your own stop word
# specify your stopwords as a character vector
corpus <- tm_map(corpus, removeWords, c("new", "products", "way", "back", 
"can", "need", "also", "â", "look", "will", "one", "right",
                                    "move", "gorge", "mathieu", "like", 
"said", "€“", "–", "â", "data",
                                    "use", "storage"))
# Remove punctuations
corpus <- tm_map(corpus, removePunctuation)
# Eliminate extra white spaces
corpus <- tm_map(corpus, stripWhitespace)

之后相同的正文如下所示:

虚拟复制增加了复制 aws 以前 oneway 亚马逊云平台需要更长时间 开发 aws zerto 技术布道者 gjisbert janssen van doorn €œ双向复制 azure 开始尝试通过 apis 云进行本地开发 支持需要更长时间 aws ' zerto 添加双向复制 ibm cloud van doorn公司计划添加支持谷歌云平台€œit’这是我们一直关注它的愿望清单而不是路线图

所以,那些 tm_map 函数并没有摆脱所有的垃圾,所以我从中运行的词云仍然包含它们。

任何想法如何解决这个问题?

【问题讨论】:

  • 您能添加一个用于制作 wordcloud 的文本示例吗?还是数据表?
  • 我添加了用于删除自定义停用词的 post-Corpus() 文本示例和代码。
  • 请添加语料库前文本示例和代码,直到您进入该部分或自定义停用词功能。问题可能与停用词删除无关,而是与早期的处理步骤有关。
  • 添加了 Pre-Corpus()、post-Corpus() 和后 tm_map 处理文本。

标签: r tm word-cloud


【解决方案1】:

如果您不介意使用额外的包,您可以使用textclean 包,它与 tm 函数结合使用非常好。这个包包含各种有用的功能,用于清理带有奇怪字符、url、表情符号等的文本。对于示例文本,您需要使用函数 replace_curly_quote 删除 ' 和 ' 字符和 replace_contraction 将 "it's" 替换为“它是”。请参阅下面的工作示例。毕竟你可以使用wordcloud 包来创建一个wordcloud。

txt <- "The move to Virtual Replication 6 added replication in and out of AWS where that had only previously been one-way, into the Amazon cloud storage platform. It had taken longer to develop in AWS, said Zerto technology evangelist Gjisbert Janssen van Doorn. “Bi-directional replication to and from Azure was where we started. We try to develop natively via APIs for the clouds we support but that had taken longer with AWS.” Zerto has also added bi-directional replication with IBM Cloud. van Doorn said the company had no plan to add support for Google Cloud Platform. “It’s something we’re keeping an eye on. It’s on the wishlist rather than the roadmap,” he said."

library(tm)
library(textclean)

corpus <- VCorpus(VectorSource(txt))
corpus <- tm_map(corpus, content_transformer(tolower))

# function from textclean to remove curly quotes ” and ’
corpus <- tm_map(corpus, replace_curly_quote)
# function from textclean to replace "it's" to "it is"
corpus <- tm_map(corpus, replace_contraction)

# Remove punctuations
corpus <- tm_map(corpus, removePunctuation)

# Remove numbers
corpus <- tm_map(corpus, removeNumbers)

# Remove english common stopwords
corpus <- tm_map(corpus, removeWords, stopwords("english"))

my_stopwords <- c("new", "products", "way", "back", "can", "need", "also", 
                  "look", "will", "one", "right","move", "gorge", "mathieu", 
                  "like", "said", "data","use", "storage")

corpus <- tm_map(corpus, removeWords, my_stopwords)

#remove created whitespaces
corpus <- tm_map(corpus, stripWhitespace)

content(corpus)
[[1]]
[1] " virtual replication added replication aws previously oneway amazon cloud platform taken longer develop aws zerto technology evangelist gjisbert janssen van doorn bidirectional replication azure started try develop natively via apis clouds support taken longer aws zerto added bidirectional replication ibm cloud van doorn company plan add support google cloud platform something keeping eye wishlist rather roadmap "

【讨论】:

  • 我第一次运行完整的语料库仍然有欧元符号; 555次! textclean 中是否有某种方法可以删除小于某个字长的单词?这样就可以了
  • 我看到你使用 VCorpus 来引入文本。我正在使用 Corpus() 因为我从许多 .txt 文件中引入文本。有没有可能这个错误已经出现了?
  • VCorpus 和 Corpus 之间存在差异,但在这种情况下应该无关紧要。您可以在加载多个文本文件时使用。无论哪种方式,我都会得到相同的结果。创建文档术语矩阵时,您可以在其控制部分指定 wordLength。默认值为 3,即 3 个或更多字符的单词。如果你愿意,你可以改变它。但我很惊讶你还有欧元符号。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-12-14
  • 1970-01-01
  • 2013-05-29
  • 2022-11-12
相关资源
最近更新 更多