如何从词云中删除单词?

2024-02-10

我正在使用 R 中的 wordcloud 包创建一个 wordcloud,并在“的帮助下R 中的词云 http://onertipaday.blogspot.com/2011/07/word-cloud-in-r.html".

我可以很容易地做到这一点,但我想从这个词云中删除单词。我的文件中有单词(实际上是一个 Excel 文件,但我可以更改它),并且我想排除所有这些单词,其中有几百个。有什么建议么?

require(XML)
require(tm)
require(wordcloud)
require(RColorBrewer)
ap.corpus=Corpus(DataframeSource(data.frame(as.character(data.merged2[,6]))))
ap.corpus=tm_map(ap.corpus, removePunctuation)
ap.corpus=tm_map(ap.corpus, tolower)
ap.corpus=tm_map(ap.corpus, function(x) removeWords(x, stopwords("english")))
ap.tdm=TermDocumentMatrix(ap.corpus)
ap.m=as.matrix(ap.tdm)
ap.v=sort(rowSums(ap.m),decreasing=TRUE)
ap.d=data.frame(word = names(ap.v),freq=ap.v)
table(ap.d$freq)

@Tyler Rinker 已经给出了答案,只需添加另一行removeWords(),但这里有更多细节。

假设您的 excel 文件名为nuts.xls并且有一列像这样的单词

stopwords
peanut
cashew
walnut
almond
macadamia

In R你可以像这样继续

     library(gdata) # package with xls import function
     library(tm)
     # now load the excel file with the custom stoplist, note a few of the arguments here 
     # to clean the data by removing spaces that excel seems to insert and prevent it from 
     # importing the characters as factors. You can use any args from read.table(), which is
     # handy
     nuts<-read.xls("nuts.xls", header=TRUE, stringsAsFactor=FALSE, strip.white=TRUE)

     # now make some words to build a corpus to test for a two-step stopword removal process...
     words1<- c("peanut, cashew, walnut, macadamia, apple, pear, orange, lime, mandarin, and, or, but")
     words2<- c("peanut, cashew, walnut, almond, apple, pear, orange, lime, mandarin, if, then, on")
     words3<- c("peanut, walnut, almond, macadamia, apple, pear, orange, lime, mandarin, it, as, an")
     words.all<-data.frame(rbind(words1,words2,words3))
     words.corpus<-Corpus(DataframeSource((words.all)))

     # now remove the standard list of stopwords, like you've already worked out
     words.corpus.nostopwords <- tm_map(words.corpus, removeWords, stopwords("english"))
     # now remove the second set of stopwords, this time your custom set from the excel file, 
     # note that it has to be a reference to a character vector containing the custom stopwords
     words.corpus.nostopwords <- tm_map(words.corpus.nostopwords, removeWords, nuts$stopwords)

     # have a look to see if it worked
     inspect(words.corpus.nostopwords)
     A corpus with 3 text documents

     The metadata consists of 2 tag-value pairs and a data frame
     Available tags are:
          create_date creator 
     Available variables in the data frame are:
          MetaID 

     $words1
        , , , , apple, pear, orange, lime, mandarin, , , 

     $words2
        , , , , apple, pear, orange, lime, mandarin, , , 

     $words3
        , , , , apple, pear, orange, lime, mandarin, , , 

成功!标准停用词消失了,Excel 文件中的自定义列表中的单词也消失了。毫无疑问,还有其他方法可以做到这一点。

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

如何从词云中删除单词? 的相关文章

随机推荐

  • Solr 4.0 是否能够对多核使用“join”?

    我注意到 Solr 4 0 为具有关系的文档引入了 连接 功能 这很棒 但是 我注意到给出的例子http wiki apache org solr Join http wiki apache org solr Join适用于单核 所有文档都
  • 从多模块 pom 构建单个模块

    可以做吗 环境 多模块pom由3个模块组成 mm1 mm2 mm3 模块 mm2 有 mm1 作为依赖项 可以没有任何错误地构建父 pom 问题 是否可以在不将 mm1 安装到本地存储库的情况下构建单个模块 mm2 即从 mm2 基本目录运
  • 切换到 Android 应用程序包分发后,应用程序有时会崩溃并出现 Resources$NotFoundException

    App每月活跃用户超过20000人 它已经在 Google Play 上发布几个月了 在我最近从 apk 发行版切换到 aab 发行版后 我开始在 crashlytics 和 google play store 上收到随机崩溃的消息 在导致
  • 更少的变量:这可能吗

    所以我的代码在类型方面存在重大问题 但我似乎无法解决它 每当我减去1从线8有问题 我该如何解决这个问题 max columns 2 column 1 width min 30 column 2 width min 40 loop colum
  • .NET DB2 OLEDB 先决条件

    我用 C NET Framework 2 0 编写了一个 Windows 窗体应用程序 它使用System Data OleDb与 SQL Server 2000 数据库对话 该数据库运行良好 我现在需要增强应用程序以与DB2 http e
  • 使用聚合将多个函数应用于数据框中的每一列

    当我需要将多个函数按顺序应用于多个列并按多个列聚合并希望将结果绑定到我通常使用的数据框中时aggregate 通过以下方式 bogus functions foo1 lt function x mean x var x foo2 lt fu
  • npm start 如何指定index.js以外的启动文件

    我运行 create react app 然后切换到目录并运行 npm start 只是想知道如何指定一个与 index js 不同的文件在服务器上启动 这是我的 package json 文件 name todo version 0 1
  • 错误:联合中不允许复制赋值运算符

    当出现以下错误时 我正在编译下面的代码 我找不到原因 typedef union struct const int j tag X int main return 0 error member lt anonymous union gt l
  • Django 模板内部总结

    我在 django 中有以下模板 我想获取每个文档对象的最后 2 列的总计 for documento in documentos for cuenta in documento cuentasxdocumento set all tr i
  • 通过蓝牙将文件从 PC 传输到其他设备代码示例 C++ [关闭]

    Closed 这个问题需要多问focused help closed questions 目前不接受答案 Stackoverflow 用户大家好 问题 我正在寻找一些代码示例 这些示例展示了如何找到连接到我的计算机的蓝牙设备 我正在寻找一些
  • 是否有可用的 WPF“WrapGrid”控件或创建控件的简单方法?

    本质上我想要一个wrapPanel 但我希望项目能够捕捉到网格而不是被压到左侧 这样我就可以获得一个漂亮的统一外观的网格 它会自动消耗可用空间 WrapPanel 处理调整大小部分 WPF Contrib AutoGrid 处理一个很好的自
  • Android XML 属性中的星号 (*) [重复]

    这个问题在这里已经有答案了 谁能解释一下 Android XML 属性中星号的含义 允许您访问私有资源 私有资源之所以私有是有原因的 因为它们的名称将来可能会作为固件或皮肤更新的一部分而
  • 在php中对对象数组进行排序

    嗨 我想对一个对象数组进行排序 它采用数组的形式 其中包含对象 每个对象都有键 值 我想根据值对对象进行排序 问题是值包含其中有空格的数字之间 有些也是空数字 所以我无法使用 usort 对其进行排序 任何帮助将不胜感激 这是一个代码片段
  • 浏览器文本框自动完成事件,什么时候发生?

    If i dont put autocomplete off on my
  • 使用 fread 将文件内容读取到结构中

    在 Unix 环境中的高级编程 一书中 有一部分 第 8 14 章 第 251 页 作者向我们展示了 acct 结构 用于存储会计记录信息 的定义 然后 他展示了一个程序 在该程序中 他将文件中的会计数据读取到结构中 其关键部分是 frea
  • 如何更新反冲状态对象的特定值

    我有这个反冲状态对象 export const LivePolygon atom key LivePolygon default radii coordinates tilt 在另一个文件上 我像这样导入它 import LivePolyg
  • 如何在用户点击react.js中的按钮时动态添加输入字段

    我有两个问题 gt 第一个是我想在用户单击react js中的 按钮时动态添加用户输入字段 如果用户点击次数越多 表单中就会添加更多的字段 如何在react js中做到这一点 第二个当用户更改其值时 我想将每个输入字段的相应值存储到我的组件
  • 尽管 UseColumnTextForButtonText 设置为 true,但 DataGridView 按钮文本未显示

    我已向 DataGridView 添加了一个按钮列 并希望在其上显示文本 比较 我已经设置了Text比较和属性UseColumnTextForButtonValue为 True 但不显示任何文本 这在运行时也是如此 所以它不仅仅是不显示在设
  • 如何使用 proguard 混淆 war 文件

    我想使用 proguard 混淆 war 文件 我该怎么做 请解释一下步骤 这个问题我很疑惑 与其他 Java 混淆器一样 Proguard 通过重命名每个它认为可以安全重命名的变量 方法等来混淆已编译的字节码 class 文件 我想你知道
  • 如何从词云中删除单词?

    我正在使用 R 中的 wordcloud 包创建一个 wordcloud 并在 的帮助下R 中的词云 http onertipaday blogspot com 2011 07 word cloud in r html 我可以很容易地做到这