Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
620 views
in Technique[技术] by (71.8m points)

r - could not find function tagPOS

Trying to tag a sentence using openNLP.

library(openNLP)
str <- "this is a the first sentence."
tagged_str <-  tagPOS(str)

Getting the following error:

Error: could not find function "tagPOS"

Any suggestions? Thanks.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

I think tagPOS is not a built in function of any of the package, so you'll have to add the function.

Here is the R Code:

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)

Output:

> tagged_str
$POStagged
[1] "this/DT is/VBZ a/DT the/DT first/JJ sentence/NN ./."

$POStags
[1] "DT"  "VBZ" "DT"  "DT"  "JJ"  "NN"  "."  

Hope this helps.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...