【问题标题】:apply function to textreuse corpus将函数应用于 textreuse 语料库
【发布时间】:2018-08-09 01:02:33
【问题描述】:

我有一个数据框如下:

df<-data.frame(revtext=c('the dog that chased the cat', 'the dog which chased the cat', 'World Cup Hair 2014 very funny.i can change', 'BowBow', 'this is'), rid=c('r01','r02','r03','r04','r05'), stringsAsFactors = FALSE)

                             revtext        rid
             the dog that chased the cat    r01
             the dog which chased the cat   r02
World Cup Hair 2014 very funny.i can change r03
             Bow Bow                        r04
             this is                        r05

我正在使用包textreusedf 转换为corpus 做:

#install.packages(textreuse)
library(textreuse)
d<-df$revtext
names(d)<-df$rid
corpus <- TextReuseCorpus(text = d,
                      tokenizer = tokenize_character, k=3,
                      progress = FALSE,
                      keep_tokens = TRUE)

tokenize_character 是我编写的函数:

 tokenize_character <- function(document, k) {
                       shingles<-c()
                 for( i in 1:( nchar(document) - k + 1 ) ) {
                         shingles[i] <- substr(document,start=i,stop= (i+k-1))
                     }
return( unique(shingles) )  
}   

但是,我收到了一些警告提示:Skipping document with ID 'r04' because it has too few words to create at least two n-grams with n = 3.。但请注意,我的标记器在字符级别上工作。 r04 的文字够长。事实上,如果我们运行tokenize_character('BowBow',3),我们会得到:"Bow" "owB" "wBo"

另请注意,对于r01TextReuseCorpus 正在按预期工作,返回:tokens(corpus)$`r01= "the" "he " "e d" " do" "dog" "og " "g t" " th" "tha" "hat" "at " "t c" " ch" "cha" "has" "ase" "sed" "ed " "d t" "e c" " ca" "cat"

有什么建议吗?我不知道我在这里错过了什么。

【问题讨论】:

  • 这里已经放了很多信息了。但我不太明白你工作的目的。如果你在做文本挖掘,你为什么期望有一些片段,例如'ca','ed'?
  • 我想计算文本之间的相似度。即使这没有意义,该函数也不应该那样表现。我的意思是,问题不在于文本挖掘是否合适。
  • 好的,对我有意义。

标签: r nlp text-mining corpus


【解决方案1】:

来自textreuse::TextReuseCorpusdocumentation的详情部分:

如果skip_short = TRUE,此函数将跳过很短或为空 文件。一个非常短的文档是只有两个单词的文档 创建至少两个 n-gram。例如,如果五克是 如果需要,那么文档必须至少有六个字长。如果没有 n 值 提供,则函数假定值 n = 3。

据此,我们知道具有 r04 和 r05 分别有 1 和 2 个单词. 要不跳过这些文档,您可以使用skip_short = F,它将按预期返回输出:

corpus <- TextReuseCorpus(text = d, tokenizer = tokenize_character, k=3,
                      skip_short = F, progress = FALSE, keep_tokens = TRUE)
tokens(corpus)$r04
[1] "Bow" "owB" "wBo"

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-03-20
    • 1970-01-01
    • 2023-03-11
    • 1970-01-01
    • 2017-07-11
    • 1970-01-01
    相关资源
    最近更新 更多