删除带有前导和尾随停用词的 ngram

2023-12-29

我想识别一堆学术论文中的主要 n 元语法,包括带有嵌套停用词的 n 元语法,但不包括带有前导或尾随停用词的 n 元语法。

我有大约 100 个 pdf 文件。我通过 Adob​​e 批处理命令将它们转换为纯文本文件,并将它们收集在一个目录中。从那里我使用 R。(这是代码的拼凑,因为我刚刚开始进行文本挖掘。)

My code:

library(tm)
# Make path for sub-dir which contains corpus files 
path <- file.path(getwd(), "txt")
# Load corpus files
docs <- Corpus(DirSource(path), readerControl=list(reader=readPlain, language="en"))

#Cleaning
docs <- tm_map(docs, tolower)
docs <- tm_map(docs, stripWhitespace)
docs <- tm_map(docs, removeNumbers)
docs <- tm_map(docs, removePunctuation)

# Merge corpus (Corpus class to character vector)
txt <- c(docs, recursive=T)

# Find trigrams (but I might look for other ngrams as well)
library(quanteda)
myDfm <- dfm(txt, ngrams = 3)
# Remove sparse features
myDfm <- dfm_trim(myDfm, min_count = 5)
# Display top features
topfeatures(myDfm)
#                  as_well_as             of_the_ecosystem                  in_order_to         a_business_ecosystem       the_business_ecosystem strategic_management_journal 
#603                          543                          458                          431                          431                          359 
#in_the_ecosystem        academy_of_management                  the_role_of                the_number_of 
#336                          311                          289                          276

例如,在此处提供的顶部 ngrams 示例中,我想保留“管理学院”,但不保留“以及”,也不保留“the_role_of”。我希望代码适用于任何 n 元语法(最好包括少于 3 元语法,尽管我知道在这种情况下先删除停用词会更简单)。


使用corpusR 包,带有绿野仙踪举个例子(古腾堡项目 ID#55):

library(corpus)
library(Matrix) # needed for sparse matrix operations

# download the corpus
corpus <- gutenberg_corpus(55)

# set the preprocessing options
text_filter(corpus) <- text_filter(drop_punct = TRUE, drop_number = TRUE)

# compute trigram statistics for terms appearing at least 5 times;
# specify `types = TRUE` to report component types as well 
stats <- term_stats(corpus, ngrams = 3, min_count = 5, types = TRUE)

# discard trigrams starting or ending with a stopword
stats2 <- subset(stats, !type1 %in% stopwords_en & !type3 %in% stopwords_en)

# print first five results:
print(stats2, 5)
##    term               type1 type2 type3     count support
## 4  said the scarecrow said  the   scarecrow    36       1
## 7  back to kansas     back  to    kansas       28       1
## 16 said the lion      said  the   lion         19       1
## 17 said the tin       said  the   tin          19       1
## 48 road of yellow     road  of    yellow       12       1
## ⋮  (35 rows total)

# form a document-by-term count matrix for these terms
x <- term_matrix(corpus, select = stats2$term)

就您而言,您可以从tm语料库对象

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

删除带有前导和尾随停用词的 ngram 的相关文章

  • picker输入字体或背景颜色

    我在闪亮的仪表板中使用 pickerInput 这很好 除了一个问题 背景颜色和字体颜色太相似 使得过滤器选择难以阅读 有什么办法可以改变背景或字体颜色吗 如果可能的话 我想继续使用 pickerInput 但如果有一个带有 selectI
  • 行对名称中具有特定模式的列求和

    我有一个像这样的数据表 DT lt ata table data table ref rep 3L 4L nb 12 15 i1 c 3 1e 05 0 044495 0 82244 0 322291 i2 c 0 000183 0 155
  • 更新 R6 对象实例中的方法定义

    如何更新 R6 类实例的方法定义 正如我所期望的 S3 使用当前的方法定义 对于 R5 参考类 我可以使用 myInstance myInstance copy 在 R6 中 我尝试了 myInstance myInstance clone
  • 在 RcppArmadillo 中将列向量乘以数值标量

    我在编译这个简单的程序时遇到一些麻烦c 代码使用Rcpp和RcppArmadillo包裹 采用以下简单示例 将矩阵的每一列乘以数值标量 code lt arma mat out Rcpp as
  • 闪亮的应用程序包:css 和所有 www/ 目录内容

    我正在尝试将 Shiny 应用程序转换为 R 包 但我在处理有关 www 目录以及 松散 文件的所有问题时遇到了问题 我闪亮的应用程序运行得很好 但是当我尝试 打包它 时 它不起作用 我闪亮的应用程序目录 my shiny app R ut
  • 在 RMarkdown 输出到 PDF 时缩进而不添加项目符号点或编号

    之前有人问过如何在没有项目符号的情况下缩进文本 RMarkdown 中的点 但这是针对 HTML 输出的 在 RMarkdown 中缩进而不添加项目符号点或数字 https stackoverflow com questions 47087
  • 在网格中制作一个矩形图例,并标记行和列

    我有一个 ggplot 我将因子映射到填充和 alpha 如下所示 set seed 47 the data lt data frame value rpois 6 lambda 20 cat1 rep c A B each 3 cat2
  • R在Windows平台Rstudio上打印data.frames中的UTF-8代码

    当数据框中存在UTF 8字符时 将无法正常显示 例如 以下内容是正确的 gt U6731 1 朱 但是当我将其放入数据框中并打印出来时 它是 gt data frame x U6731 x 1
  • dplyr:连接中的 NSE (by)

    我很难弄清楚如何使用 dplyr left join 和 NSE 连接两个表 问题是我无法为 by 提供正确的值 我想我现在已经找到了解决方案 但感觉我正在以一种额外复杂的方式来做 因此 如果您知道更简单 更优雅的解决方案 请告诉我 这就是
  • R:如何将字符/数字转为1,NA转为0?

    有没有一种简单的方法可以将列的字符 数字变为 1 将 NA 变为 0 这里有一些示例数据 我想将其应用于 3 4 structure list Item Code c 176L 187L 191L 201L 217L 220L Item x
  • 如何在 R 中的 dygraph 标题中使用 UTF-8 字符

    使用 Rstudio Windows8 当我使用 dygraph 函数绘制时间序列时 在尝试在主标题中使用 UTF 8 字符时遇到问题 library dygraphs dygraph AirPassengers main T tulo 这
  • 如何在Rstudio中快速给几个单词加上引号?

    如何将 MI ID FL 转换为 MI ID FL 而无需键入每个双引号 Hmisc 包有一个函数 Cs 它将评估逗号分隔的文本是否带有引号 Cs MI ID FL becomes MI ID FL
  • 如何动态地将 sliderInput 添加到闪亮的应用程序中?

    使用闪亮 我上传一个 csv 文件 并根据列名称 我需要向 ui 添加滑块 sidebarPanel fileInput file1 Upload CSV File to Create a Model accept c text csv t
  • R 改变构面的顺序

    我正在尝试将方面的顺序从 BA SLG 更改为 SLG BA 我发现了与此类似的问题 但我认为我的解决方案可能不起作用 因为我已经在Excel中汇总了数据 因此 我的数据框可能会有所不同 无论如何 我尝试实现这个但无济于事 df2 lt f
  • HTTR GET 新错误:SSL 证书问题:证书已过期

    我已经运行这段代码几个月了 没有出现任何问题 今天我突然开始在我的两台 AWS 服务器上收到以下错误消息 错误 curl curl fetch memory url handle handle SSL证书问题 证书已过期 当尝试运行以下代码
  • 如何将参数从 Excel/VBA 传递到 Rstudio 中的脚本

    我正在尝试使用 Rstudio 从 VBA 打开 R 脚本 同时将参数传递给 R 脚本 然后我可以使用 commandArgs 访问该脚本 该问题与此处描述的问题非常相似 WScript Shell 用于运行路径中包含空格且来自 VBA 的
  • 安装 2.15 后 ggplot2 中的 alpha 通道不起作用

    更新到 R 2 15 后 ggplot 中的 alpha 通道似乎不再起作用 plot rnorm 100 rnorm 100 bg cc000055 pch 21 工作得很好但是 qplot rnorm 100 rnorm 100 col
  • 如何在R中分离两个图?

    每当我运行这段代码时 第一个图就会简单地覆盖前一个图 R中有没有办法分开得到两个图 plot pc title main abc xlab xx ylab yy plot pcs title main sdf xlab sdf ylab x
  • 斯皮尔曼相关性和联系

    我正在一小组配对排名上计算斯皮尔曼的 rho 斯皮尔曼因处理领带不当而闻名 例如 取2组8个排名 即使两组中有6个是平局 相关性仍然很高 gt cor test c 1 2 3 4 5 6 7 8 c 0 0 0 0 0 0 7 8 met
  • 闪亮井板宽度

    library shiny library shinydashboard ui lt dashboardPage dashboardHeader dashboardSidebar dashboardBody wellPanel tags d

随机推荐