2015-06-23 16 views
3

这里是R代码:如何使用OpenNLP获取R中的POS标签?

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]“这/ DT是/ VBZ一个/ DT的/ DT第一/ JJ句子/ NN ./“。

现在我想从上面的句子中只提取NN字,即句子,并希望将它存储到一个变量中。任何人都可以帮助我解决这个问题。

回答

2

可能有更优雅的方式来获得结果,但这个应该工作:

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

希望这有助于。

+0

我只能跟你提供的示例工作 - 它为你提出了什么。如果您遇到其他示例的问题,请随时发布新问题。 – RHertel

+0

假设的例子是 “2汤匙全蛋蛋黄酱” “1茶匙全麦芥末” “70克混合沙拉叶” “西红柿2个,切成薄片” “面包与黄油黄瓜,服务” “90克Hakuba有机干荞麦面“ ”1个大胡萝卜,去皮,切成火柴梗“ ”1/2束西兰花,切成5厘米长“ ”60g婴儿玉米,对角切成薄片“ 试试这些例子。在某些情况下工作 – user4599

+0

谢谢你的评论,@HimaanshuGauba。我很抱歉听到我的建议解决方案在某些遇到的情况下无法提供预期的结果。如果在您的OP中出现这种隐患的可能性已经很明显,那么我会试着提供一个适用于这些案例的答案。 – RHertel

5

下面是一个更一般的解决方案,您可以在其中描述您希望使用正则表达式提取的Treebank标记。例如,在你的情况下,“NN”返回所有名词类型(例如NN,NNS,NNP,NNPS),而“NN $”只返回NN。

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

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] "" 
2

下面是使用spaCy解析器和恶搞,从Python和spacyr包称之为另一种答案。

这个函数库比stanford NLP模型快了几个数量级。它在某些语言中仍然不完整,但对于英语而言,这是一个相当不错的选择。

您首先需要安装Python并安装了spaCy和语言模块。说明可从spaCy pagehere获得。

然后:

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 . 
相关问题