标签: tidytext

tidytext 从文件夹中读取文件

我正在尝试将一个包含 pdf 文件的文件夹读入 R 中的数据帧。我能够使用pdftools库和pdf_text(filepath).

理想情况下,我可以获取一系列 pdf 的作者和标题,然后将它们推送到一个数据框中,该数据框中有一列包含这些内容,以便我可以tidytext在文本上使用基本功能。

对于现在的单个文件,我可以使用:

library(pdftools)
library(tidytext)
library(dplyr)
txt <- pdf_text("filpath")
txt <- data_frame(txt)
txt %>%
     unnest_tokens(word, txt)
Run Code Online (Sandbox Code Playgroud)

在这里,我有一个带有单个单词的数据框。我想进入一个数据框,在那里我解压了文章,包括标题和作者列。

nlp r tidytext

4
推荐指数
1
解决办法
1634
查看次数

使用 unnest_tokens() 对句子进行标记,忽略缩写

我正在使用优秀的tidytext包来标记几个段落中的句子。例如,我想摘录以下一段:

“我完全相信达西先生没有缺陷。他自己毫不掩饰地拥有它。”

并将其标记为两个句子

  1. “我完全相信达西先生没有缺陷。”
  2. “他毫不掩饰地拥有它。”

但是,当我使用默认句子标记器时,tidytext我得到三个句子。

代码

df <- data_frame(Example_Text = c("I am perfectly convinced by it that Mr. Darcy has no defect. He owns it himself without disguise."))


unnest_tokens(df, input = "Example_Text", output = "Sentence", token = "sentences")
Run Code Online (Sandbox Code Playgroud)

结果

# A tibble: 3 x 1
                              Sentence
                                <chr>
1 i am perfectly convinced by it that mr.
2                    darcy has no defect.
3    he owns it himself without disguise.
Run Code Online (Sandbox Code Playgroud)

有什么简单的方法可以用来tidytext标记句子,但不会遇到常见缩写(例如“Mr.”)的问题?或“博士”。被解释为句子结尾?

text r tidytext

4
推荐指数
2
解决办法
3871
查看次数

tidytext,quanteda和tm返回不同的tf-idf分数

我正在尝试研究tf-idf加权语料库(我希望tf是按文档而不是简单计数的比例).我希望所有经典文本挖掘库都能返回相同的值,但我得到的值不同.我的代码中是否有错误(例如,我是否需要转置对象?)或者tf-idf的默认参数是否与包中的数量不同?

library(tm)
library(tidyverse) 
library(quanteda)
df <- as.data.frame(cbind(doc = c("doc1", "doc2"), text = c("the quick brown fox jumps over the lazy dog", "The quick brown foxy ox jumps over the lazy god")), stringsAsFactors = F)

df.count1 <- df %>% unnest_tokens(word, text) %>% 
  count(doc, word) %>% 
  bind_tf_idf(word, doc, n) %>% 
  select(doc, word, tf_idf) %>% 
  spread(word, tf_idf, fill = 0) 

df.count2 <- df %>% unnest_tokens(word, text) %>% 
  count(doc, word) %>% 
  cast_dtm(document = doc,term = word, value = n, weighting = weightTfIdf) %>% 
  as.matrix() …
Run Code Online (Sandbox Code Playgroud)

r text-mining tm quanteda tidytext

4
推荐指数
1
解决办法
1202
查看次数

将tibble中的字符串替换为该字符串的一部分

我在这里搜索了很多正则表达式的答案,但找不到解决这类问题的方法.

我的数据集是维基百科链接:

library(tidytext)
library(stringr)
text.raw <- "Berthold Speer was een [[Duitsland (hoofdbetekenis)|Duits]] [[architect]]."
Run Code Online (Sandbox Code Playgroud)

我正在尝试从链接中清理我的文本.这个:

str_extract_all(text.raw, "[a-zA-Z\\s]+(?=\\])")
# [1] "Duits"     "architect"
Run Code Online (Sandbox Code Playgroud)

从括号中选择我需要的单词.

这个:

str_replace_all(text.raw, "\\[\\[.*?\\]\\]", str_extract(text.raw, "[a-zA-Z\\s]+(?=\\])"))
# [1] "Berthold Speer was een Duits Duits."
Run Code Online (Sandbox Code Playgroud)

按预期工作,但不是我需要的.这个:

str_replace_all(text.raw, "\\[\\[.*?\\]\\]", str_extract_all(text.raw, "[a-zA-Z\\s]+(?=\\])"))
# Error: `replacement` must be a character vector
Run Code Online (Sandbox Code Playgroud)

给出了我预期的错误 "Berthold Speer was een Duits architect"

目前我的代码看起来像这样:

text.clean <- data_frame(text = text.raw) %>%
  mutate(text = str_replace_all(text, "\\[\\[.*?\\]\\]", str_extract_all(text, "[a-zA-Z\\s]+(?=\\])")))
Run Code Online (Sandbox Code Playgroud)

我希望有人知道解决方案,或者如果存在问题,我可以指出一个重复的问题.我想要的输出是"Berthold Speer was een Duits architect".

regex r stringr tidytext

3
推荐指数
1
解决办法
454
查看次数

从 R 中的数字和停用词中过滤文本(不适用于 tdm)

我有文本语料库。

mytextdata = read.csv(path to texts.csv)
Mystopwords=read.csv(path to mystopwords.txt)
Run Code Online (Sandbox Code Playgroud)

如何过滤此文本?我必须删除:

1) all numbers

2) pass through the stop words

3) remove the brackets
Run Code Online (Sandbox Code Playgroud)

我不会使用dtm,我只需要从数字和停用词中清除此文本数据

样本数据:

112773-Tablet for cleaning the hydraulic system Jura (6 pcs.) 62715
Run Code Online (Sandbox Code Playgroud)

Jura,the 是停用词。

在我期望的输出中

  Tablet for cleaning hydraulic system 
Run Code Online (Sandbox Code Playgroud)

r tm tidytext

3
推荐指数
1
解决办法
5085
查看次数

用字符串中的单个数字替换数字范围

有没有办法用字符串中的单个数字替换数字范围?数字的范围可以从nn,最可能在1-15左右,也可能是4-10.

范围可以用a)表示 -

a <- "I would like to buy 1-3 cats"
Run Code Online (Sandbox Code Playgroud)

或者用词b)例如:to,bis,jusqu'à

b <- "I would like to buy 1 jusqu'à 3 cats"
Run Code Online (Sandbox Code Playgroud)

结果应该是这样的

"I would like to buy 1,2,3 cats"
Run Code Online (Sandbox Code Playgroud)

我发现这个:用一定数量替换数字范围,但在R中无法真正使用它.

text replace r tm tidytext

3
推荐指数
1
解决办法
182
查看次数

获取 TF-IDF 数据时的内存问题

介绍

\n

我正在努力对大型推文数据集进行文本分类,如果有人能给我指出正确的方向,我将不胜感激。

\n

总体而言,我需要训练一个分类器来区分庞大数据集(最多 600 万个文本)上的两个类。我一直在食谱框架中执行此操作,然后通过tidymodels运行 glmnet lasso 。具体问题是我在计算 tf-idf 时内存不足。

\n

问题

\n

我应该朝哪个方向努力来解决这个问题?我基本上可以批量手动获取所有 tf​​-idf 值,然后再次手动将它们组合成稀疏矩阵对象。这听起来很肛门,肯定有人以前遇到过这个问题并解决了它?另一种选择是 Spark,但它远远超出了我目前的能力范围,并且对于一次性任务来说可能有些过大了。或者也许我遗漏了一些东西,而现有的工具能够做到这一点?

\n

具体来说,我在运行以下命令时遇到两种问题(变量应该是不言自明的,但我稍后将提供完整的可重现代码):

\n
recipe <-\n  recipe(Class ~ text, data = corpus) %>% \n  step_tokenize(text) %>%\n  step_stopwords(text) %>% \n  step_tokenfilter(text, max_tokens = m) %>% \n  step_tfidf(text) %>% \n  prep()\n
Run Code Online (Sandbox Code Playgroud)\n

如果corpus太大或者m太大,Rstudio就会崩溃。如果它们相当大,它会发出警告

\n
In asMethod(object) :\n  sparse->dense coercion: allocating vector of size 1.2 GiB\n
Run Code Online (Sandbox Code Playgroud)\n

我在网上没有找到相关内容,我也不太明白。为什么它试图强迫某些东西从稀疏变成密集?这对于任何大型数据集来说肯定会带来麻烦。难道我做错了什么?如果这是可以预防的,也许我的完整数据集会有更好的运气?

\n

或者是否没有希望step_tfidf应对 600 万个观测值并且对最大令牌没有限制?

\n …

r classification tf-idf tidytext tidymodels

3
推荐指数
1
解决办法
139
查看次数

How to remove specific words in a column

I have a Column consisting of several Country Offices associated a with a company, where I would like to shorten fx: China Country Office and Bangladesh Country Office, to just China or Bangladesh- In other words removing the words "Office" and "Country" from the column called Imp_Office.

I tried using the tm-package, with reference to an earlier post, but nothing happened.

what I wrote:

library(tm)
stopwords = c("Office", "Country","Regional")
MY_df$Imp_Office <- gsub(paste0(stopwords, collapse = "|","", 
MY_df$Imp_Office))
Run Code Online (Sandbox Code Playgroud)

Where I got the …

string r tm tidytext

2
推荐指数
1
解决办法
5550
查看次数

如何在r中使用整洁的文本进行二元组主题建模?

所以我尝试使用tidytext包来做bigrams主题建模,按照tidytext网站上的步骤操作:https://www.tidytextmining.com/ngrams.html .

我能够进入"word_counts"部分,其中R计算每个bi-gram的频率.

"word_counts"返回以下内容:

   customer_id       word          n
   <chr>            <chr>        <int>
 1 00000001234  sample text        45
 2 00000002345  good morning       30
 3 00000003456  happy friday       24
Run Code Online (Sandbox Code Playgroud)

下一步是将上面的信息放入dtm格式

我的代码如下:

lda_dtm <- word_counts %>%
  cast_dtm(customer_id, word, n)
Run Code Online (Sandbox Code Playgroud)

提出了一条警告信息:

Warning message:
Trying to compute distinct() for variables not found in the data:
- `row_col`, `column_col`
This is an error, but only a warning is raised for compatibility reasons.
The operation will return the input unchanged. 
Run Code Online (Sandbox Code Playgroud)

但是"lda_dtm"看起来像是正确的格式.

lda_dtm
<<DocumentTermMatrix (documents: 9517, terms: 341545)>> …
Run Code Online (Sandbox Code Playgroud)

r text-mining n-gram topic-modeling tidytext

2
推荐指数
1
解决办法
462
查看次数

R 中用于文本分析的常见名字列表?

在分析文本时,识别文本数据中的人名可能很有用。

\n\n

预先打包的对象tidytext包括:

\n\n
    \n
  • 英语否定词、情态动词和副词 ( nma_words)
  • \n
  • 词类 ( parts_of_speech)
  • \n
  • 情绪 ( sentiments) 和
  • \n
  • 停用词(参见?stop_words:)
  • \n
\n\n

R 中是否有类似的对象(或其他地方的可访问格式)包含规范的名称列表?

\n\n

作为参考,以下是data.frame随附的现有产品tidytext

\n\n
nma_words\n# # A tibble: 44 x 2\n# word      modifier\n# <chr>     <chr>   \n#   1 cannot    negator \n# 2 could not negator \n# 3 did not   negator \n# 4 does not  negator \n# 5 had no    negator \n# 6 have no   negator \n# 7 may not …
Run Code Online (Sandbox Code Playgroud)

nlp r tidytext

2
推荐指数
1
解决办法
791
查看次数