我正在使用NLP包来解析句子。我怎样才能从Tree
创建的输出?例如,我想抓住名词短语(NP
)来自下面的示例:
library(NLP)
library(openNLP)
s <- c(
"Really, I like chocolate because it is good.",
"Robots are rather evil and most are devoid of decency"
)
s <- as.String(s)
sent_token_annotator <- Maxent_Sent_Token_Annotator()
word_token_annotator <- Maxent_Word_Token_Annotator()
a2 <- annotate(s, list(sent_token_annotator, word_token_annotator))
parse_annotator <- Parse_Annotator()
p <- parse_annotator(s, a2)
ptexts <- sapply(p$features, `[[`, "parse")
ptexts
ptrees <- lapply(ptexts, Tree_parse)
ptrees
## [[1]]
## (TOP
## (S
## (S
## (S
## (ADVP (RB Really))
## (, ,)
## (NP (PRP I))
## (VP
## (VBP like)
## (NP (NN chocolate))
## (SBAR (IN because) (S (NP (PRP it)) (VP (VBZ is) (ADJP (JJ good)))))))
## (. .)
## (, ,)
## (NP (NNP Robots))
## (VP (VBP are) (ADJP (RB rather) (JJ evil))))
## (CC and)
## (S (NP (RBS most)) (VP (VBP are) (ADJP (JJ devoid) (PP (IN of) (NP (NN decency))))))))
我想从Tree
但无法从文档中找出Tree_parse
。使用str
表明这应该很容易做到,但我无法实现。
我希望它返回类似的内容:
[1] "I" "Robots"
Or as a list
而不是向量。
这可能需要有openNLPmodels.en安装自:http://datacube.wu.ac.at/src/contrib/ http://datacube.wu.ac.at/src/contrib/
下载并运行
install.packages(
"http://datacube.wu.ac.at/src/contrib/openNLPmodels.en_1.5-1.tar.gz",
repos=NULL,
type="source"
)
`
如果有帮助的话大家可以参考一下Tree
直接使用curl我的 Dropbox 中的包:
library(curl)
ptrees <- source(curl("https://dl.dropboxusercontent.com/u/61803503/Errors/tree.R"))[[1]]