gpt4 book ai didi

r - 我需要什么算法来找到 n-gram?

转载 作者:行者123 更新时间:2023-12-03 21:11:38 25 4
gpt4 key购买 nike

使用什么算法来查找 ngram?

假设我的输入数据是一个单词数组和我想要查找的 ngram 的大小,我应该使用什么算法?

我要求提供代码,优先选择 R。数据存储在数据库中,因此也可以是 plgpsql 函数。 Java 是一种我更了解的语言,所以我可以将它“翻译”成另一种语言。

我不是懒惰,我只是要求代码,因为我不想重新发明轮子尝试执行已经完成的算法。

编辑:知道每个 n-gram 出现多少次很重要。

编辑 2:N-GRAMS 有 R 包吗?

最佳答案

如果您想使用 R要识别 ngram,您可以使用 tm包和RWeka包裹。它会告诉您 ngram 在您的文档中出现的次数,如下所示:

  library("RWeka")
library("tm")

data("crude")

BigramTokenizer <- function(x) NGramTokenizer(x, Weka_control(min = 2, max = 2))
tdm <- TermDocumentMatrix(crude, control = list(tokenize = BigramTokenizer))

inspect(tdm[340:345,1:10])

A term-document matrix (6 terms, 10 documents)

Non-/sparse entries: 4/56
Sparsity : 93%
Maximal term length: 13
Weighting : term frequency (tf)

Docs
Terms 127 144 191 194 211 236 237 242 246 248
and said 0 0 0 0 0 0 0 0 0 0
and security 0 0 0 0 0 0 0 0 1 0
and set 0 1 0 0 0 0 0 0 0 0
and six-month 0 0 0 0 0 0 0 1 0 0
and some 0 0 0 0 0 0 0 0 0 0
and stabilise 0 0 0 0 0 0 0 0 0 1

帽尖: http://tm.r-forge.r-project.org/faq.html

关于r - 我需要什么算法来找到 n-gram?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8161167/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com