找不到函数 tagPOS
could not find function tagPOS
正在尝试使用 openNLP 标记句子。
library(openNLP)
str <- "this is a the first sentence."
tagged_str <- tagPOS(str)
出现以下错误:
Error: could not find function "tagPOS"
有什么建议吗?谢谢
我认为 tagPOS
不是任何包的内置函数,因此您必须添加该函数。
这是R代码:
library(NLP)
library(openNLP)
tagPOS <- function(x, ...) {
s <- as.String(x)
word_token_annotator <- Maxent_Word_Token_Annotator()
a2 <- Annotation(1L, "sentence", 1L, nchar(s))
a2 <- annotate(s, word_token_annotator, a2)
a3 <- annotate(s, Maxent_POS_Tag_Annotator(), a2)
a3w <- a3[a3$type == "word"]
POStags <- unlist(lapply(a3w$features, `[[`, "POS"))
POStagged <- paste(sprintf("%s/%s", s[a3w], POStags), collapse = " ")
list(POStagged = POStagged, POStags = POStags)
}
str <- "this is a the first sentence."
tagged_str <- tagPOS(str)
输出:
> tagged_str
$POStagged
[1] "this/DT is/VBZ a/DT the/DT first/JJ sentence/NN ./."
$POStags
[1] "DT" "VBZ" "DT" "DT" "JJ" "NN" "."
希望对您有所帮助。
正在尝试使用 openNLP 标记句子。
library(openNLP)
str <- "this is a the first sentence."
tagged_str <- tagPOS(str)
出现以下错误:
Error: could not find function "tagPOS"
有什么建议吗?谢谢
我认为 tagPOS
不是任何包的内置函数,因此您必须添加该函数。
这是R代码:
library(NLP)
library(openNLP)
tagPOS <- function(x, ...) {
s <- as.String(x)
word_token_annotator <- Maxent_Word_Token_Annotator()
a2 <- Annotation(1L, "sentence", 1L, nchar(s))
a2 <- annotate(s, word_token_annotator, a2)
a3 <- annotate(s, Maxent_POS_Tag_Annotator(), a2)
a3w <- a3[a3$type == "word"]
POStags <- unlist(lapply(a3w$features, `[[`, "POS"))
POStagged <- paste(sprintf("%s/%s", s[a3w], POStags), collapse = " ")
list(POStagged = POStagged, POStags = POStags)
}
str <- "this is a the first sentence."
tagged_str <- tagPOS(str)
输出:
> tagged_str
$POStagged
[1] "this/DT is/VBZ a/DT the/DT first/JJ sentence/NN ./."
$POStags
[1] "DT" "VBZ" "DT" "DT" "JJ" "NN" "."
希望对您有所帮助。