如何使用OpenNLP在R中获取POS标签?

如何使用OpenNLP在R中获取POS标签?,r,nlp,text-mining,opennlp,pos-tagger,R,Nlp,Text Mining,Opennlp,Pos Tagger,以下是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

以下是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)
库(NLP)
图书馆(openNLP)

tagPOS可能有更优雅的方法来获得结果,但这一种方法应该有效:

q <- strsplit(unlist(tagged_str[1]),'/NN')
q <- tail(strsplit(unlist(q[1])," ")[[1]],1)
#> q
#[1] "sentence"

q这里有一个更通用的解决方案,您可以使用正则表达式描述希望提取的树库标记。例如,在您的示例中,“NN”返回所有名词类型(例如NN、NNS、NNP、NNPS),而“NN$”仅返回NN

它对字符类型进行操作,因此如果您将文本作为列表,则需要
lappy()
It,如下例所示

txt <- c("This is a short tagging example, by John Doe.",
         "Too bad OpenNLP is so slow on large texts.")

extractPOS <- function(x, thisPOSregex) {
    x <- as.String(x)
    wordAnnotation <- annotate(x, list(Maxent_Sent_Token_Annotator(), Maxent_Word_Token_Annotator()))
    POSAnnotation <- annotate(x, Maxent_POS_Tag_Annotator(), wordAnnotation)
    POSwords <- subset(POSAnnotation, type == "word")
    tags <- sapply(POSwords$features, '[[', "POS")
    thisPOSindex <- grep(thisPOSregex, tags)
    tokenizedAndTagged <- sprintf("%s/%s", x[POSwords][thisPOSindex], tags[thisPOSindex])
    untokenizedAndTagged <- paste(tokenizedAndTagged, collapse = " ")
    untokenizedAndTagged
}

lapply(txt, extractPOS, "NN")
## [[1]]
## [1] "tagging/NN example/NN John/NNP Doe/NNP"
## 
## [[2]]
## [1] "OpenNLP/NNP texts/NNS"
lapply(txt, extractPOS, "NN$")
## [[1]]
## [1] "tagging/NN example/NN"
## 
## [[2]]
## [1] ""

txt这里是另一个答案,它使用Python中的解析器和标记器以及调用它的包

该库速度快几个数量级,几乎与斯坦福NLP模型一样好。在某些语言中,它仍然是不完整的,但对于英语来说,它是一个非常好且有希望的选择

首先需要安装Python,并安装spaCy和语言模块。有关说明,请访问和

然后:


txt我只能使用您提供的示例—它适用于您所介绍的内容。如果你在其他例子中遇到问题,请随意发布一个新问题。假设这个例子是“2汤匙全蛋蛋黄酱”“1茶匙全麦芥末”“70克混合沙拉叶”“2个西红柿,切成薄片”“面包和黄油黄瓜”“90克白薯有机干索巴面条”“1个大胡萝卜,去皮,切成火柴条”“1/2束花椰菜,切成5厘米长”“60克小玉米,斜切成薄片“试试这些例子。在某些情况下可能不起作用。谢谢你的评论,@HimaanshuGauba。我很遗憾地听到,我建议的解决方案在您遇到的某些情况下没有给出预期的结果。如果在您的OP中发现此类陷阱的可能性,我会尝试提供一个同样适用于这些案例的答案。无论如何@RHertel,对于我在我的问题(非评论)中提到的示例,我对您的评论反应迟缓表示抱歉你的代码运行得很好,所以我有正确的答案。干杯:)非常好的功能谢谢分享
txt <- c("This is a short tagging example, by John Doe.",
         "Too bad OpenNLP is so slow on large texts.")

require(spacyr)
## Loading required package: spacyr
spacy_initialize()
## Finding a python executable with spacy installed...
## spaCy (language model: en) is installed in /usr/local/bin/python
## successfully initialized (spaCy Version: 1.8.2, language model: en)

spacy_parse(txt, pos = TRUE, tag = TRUE)
##    doc_id sentence_id token_id   token   lemma   pos tag   entity
## 1   text1           1        1    This    this   DET  DT         
## 2   text1           1        2      is      be  VERB VBZ         
## 3   text1           1        3       a       a   DET  DT         
## 4   text1           1        4   short   short   ADJ  JJ         
## 5   text1           1        5 tagging tagging  NOUN  NN         
## 6   text1           1        6 example example  NOUN  NN         
## 7   text1           1        7       ,       , PUNCT   ,         
## 8   text1           1        8      by      by   ADP  IN         
## 9   text1           1        9    John    john PROPN NNP PERSON_B
## 10  text1           1       10     Doe     doe PROPN NNP PERSON_I
## 11  text1           1       11       .       . PUNCT   .         
## 12  text2           1        1     Too     too   ADV  RB         
## 13  text2           1        2     bad     bad   ADJ  JJ         
## 14  text2           1        3 OpenNLP opennlp PROPN NNP         
## 15  text2           1        4      is      be  VERB VBZ         
## 16  text2           1        5      so      so   ADV  RB         
## 17  text2           1        6    slow    slow   ADJ  JJ         
## 18  text2           1        7      on      on   ADP  IN         
## 19  text2           1        8   large   large   ADJ  JJ         
## 20  text2           1        9   texts    text  NOUN NNS         
## 21  text2           1       10       .       . PUNCT   .