我一直在按照教程学习如何在R中使用mallet来创建主题模型。我的文本文件每行有 1 个句子。它看起来像这样,大约有 50 个句子。
Thank you again and have a good day :).
This is an apple.
This is awesome!
LOL!
i need 2.
.
.
.
这是我的代码:
Sys.setenv(NOAWT=TRUE)
#setup the workspace
# Set working directory
dir<-"/Users/jxn"
Dir <- "~/Desktop/Chat/malletR/text" # adjust to suit
require(mallet)
documents1 <- mallet.read.dir(Dir)
View(documents1)
stoplist1<-mallet.read.dir("~/Desktop/Chat/malletR/stoplists")
View(stoplist1)
**mallet.instances <- mallet.import(documents1$id, documents1$text, "~/Desktop/Chat/malletR/stoplists/en.txt", token.regexp ="\p{L}[\p{L}\p{P}]+\p{L}")**
除了代码的最后一行之外,一切正常
**`**mallet.instances <- mallet.import(documents1$id, documents1$text, "~/Desktop/Chat/malletR/stoplists/en.txt", token.regexp ="\p{L}[\p{L}\p{P}]+\p{L}")**`**
我不断收到此错误:
Error in .jcall("RJavaTools", "Ljava/lang/Object;", "invokeMethod", cl, :
java.lang.NoSuchMethodException: No suitable method for the given parameters
根据包,这就是函数应该的样子:
mallet.instances <- mallet.import(documents$id, documents$text, "en.txt",
token.regexp = "\p{L}[\p{L}\p{P}]+\p{L}")
我相信它与 token.regexp 参数有关,因为
documents1 <- mallet.read.dir(Dir)
工作得很好,这意味着提供给 mallet.instances 的前 3 个参数是正确的。
这是我遵循教程的 git 存储库的链接。https://github.com/shawngraham/R/blob/master/topicmodel.R
任何帮助将不胜感激。
谢谢J
我怀疑问题出在您的文本文件上。 我遇到了同样的错误,并使用as.character()
函数解决了它,如下所示:
mallet.instances <- mallet.import(as.character(documents$id),
as.character(documents$text),
"en.txt",
FALSE,
token.regexp="\p{L}[\p{L}\p{P}]+\p{L}")
您确定将 id 字段也转换为字符吗?很容易忽略建议并将其保留为整数。
Also there is a typo in the code sample: the backslashes have to be escaped:
token.regexp = "\p{L}[\p{L}\p{P}]+\p{L}"
This usually occurs because the html text editor eats up one backslash.