DAA
DAA

Reputation: 13

Tagging part of speech for a particular word in R

I have a column A having sentences and column B have some words. I want to check the part of speech column B word belongs to sentence present in column A.

Currently I am able to get part of speech for a single sentence using following code:

I am trying to get part of speech corresponds to each sentence in text file. Please suggest code for this.

s <- unlist(lapply(posText, function(x) { str_split(x, "\n") }))

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)
}

tagged_str <-  tagPOS(s)

Upvotes: 1

Views: 3336

Answers (2)

Tyler Rinker
Tyler Rinker

Reputation: 109844

The tagger package I maintain may be of use here to make life easier. It has some Python-like behaviors which I'll demonstrate below:

The Data

posText<- "I gave him my heart, and he took and pinched it to death; and flung it back to me.
           People feel with their hearts, Ellen, and since he has destroyed mine, I have not power to feel for him."

Getting the packages

if (!require("pacman")) install.packages("pacman")
pacman::p_load_gh(c(
    "trinker/termco", 
    "trinker/tagger",
    "trinker/textshape"
))

Tagging

tagged <- tag_pos(split_sentence(posText)[[1]])

tagged

## 1] "I/PRP gave/VBD him/PRP my/PRP$ heart/NN ,/, and/CC he/PRP took/VBD and/CC pinched/VBD it/PRP to/TO death/NN ;/: and/CC flung/VBD it/PRP back/RB to/TO me/PRP ./."                            
## [2] "People/NNS feel/VBP with/IN their/PRP$ hearts/NNS ,/, Ellen/NNP ,/, and/CC since/IN he/PRP has/VBZ destroyed/VBN mine/NN ,/, I/PRP have/VBP not/RB power/NN to/TO feel/VB for/IN him/PRP ./."

What the output really is (list of pos named vectors)

c(tagged)

## [[1]]
##       PRP       VBD       PRP      PRP$        NN         ,        CC       PRP 
##       "I"    "gave"     "him"      "my"   "heart"       ","     "and"      "he" 
##       VBD        CC       VBD       PRP        TO        NN         :        CC 
##    "took"     "and" "pinched"      "it"      "to"   "death"       ";"     "and" 
##       VBD       PRP        RB        TO       PRP         . 
##   "flung"      "it"    "back"      "to"      "me"       "." 
## 
## [[2]]
##         NNS         VBP          IN        PRP$         NNS           , 
##    "People"      "feel"      "with"     "their"    "hearts"         "," 
##         NNP           ,          CC          IN         PRP         VBZ 
##     "Ellen"         ","       "and"     "since"        "he"       "has" 
##         VBN          NN           ,         PRP         VBP          RB 
## "destroyed"      "mine"         ","         "I"      "have"       "not" 
##          NN          TO          VB          IN         PRP           . 
##     "power"        "to"      "feel"       "for"       "him"         "." 

Selecting tags (regex available too)

select_tags(tagged, c("NN", "NNP", "NNPS", "NNS"))

## [1] "heart/NN death/NN"                               
## [2] "People/NNS hearts/NNS Ellen/NNP mine/NN power/NN"

Basic POS types

as_basic(tagged)

## [1] "I/pronoun gave/verb him/pronoun my/pronoun heart/noun ,/. and/conjunction he/pronoun took/verb and/conjunction pinched/verb it/pronoun to/preposition death/noun ;/. and/conjunction flung/verb it/pronoun back/adverb to/preposition me/pronoun ./."                     
## [2] "People/noun feel/verb with/preposition their/pronoun hearts/noun ,/. Ellen/noun ,/. and/conjunction since/preposition he/pronoun has/verb destroyed/verb mine/noun ,/. I/pronoun have/verb not/adverb power/noun to/preposition feel/verb for/preposition him/pronoun ./."

Get POS counts

count_tags(tagged, pretty = FALSE)

##    n.tokens , . : CC IN NN NNP NNS PRP PRP$ RB TO VB VBD VBN VBP VBZ id
## 1:       22 1 1 1  3  0  2   0   0   6    1  1  2  0   4   0   0   0  1
## 2:       24 3 1 0  1  3  2   1   2   3    1  1  1  1   0   1   2   1  2

Get POS tags only

lapply(tagged, names)

## [[1]]
##  [1] "PRP"  "VBD"  "PRP"  "PRP$" "NN"   ","    "CC"   "PRP"  "VBD"  "CC"  
## [11] "VBD"  "PRP"  "TO"   "NN"   ":"    "CC"   "VBD"  "PRP"  "RB"   "TO"  
## [21] "PRP"  "."   
## 
## [[2]]
##  [1] "NNS"  "VBP"  "IN"   "PRP$" "NNS"  ","    "NNP"  ","    "CC"   "IN"  
## [11] "PRP"  "VBZ"  "VBN"  "NN"   ","    "PRP"  "VBP"  "RB"   "NN"   "TO"  
## [21] "VB"   "IN"   "PRP"  "."  

Upvotes: 4

amitkb3
amitkb3

Reputation: 313

Using lapply you can tag multiple sentences. Since you didn't provide a reproducible data i created my own.

code

#Reproducible data - Quotes from  Wuthering Heights by  Emily Bronte
posText<- "I gave him my heart, and he took and pinched it to death; and flung it back to me.
           People feel with their hearts, Ellen, and since he has destroyed mine, I have not power to feel for him."

library(stringr)
#Spliting into sentence based on carriage return
s <- unlist(lapply(posText, function(x) { str_split(x, "\n") }))

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)
}

result <- lapply(s,tagPOS)
result <- as.data.frame(do.call(rbind,result))

The output creates a data frame with two columns one being the sentence having the word with the tags separated by "/". The second column has the set of tags ordered in the manner of appearance in the sentence.

Output:

> print(result)
                                                                                                                                                                                     POStagged
1                             I/PRP gave/VBD him/PRP my/PRP$ heart/NN ,/, and/CC he/PRP took/VBD and/CC pinched/VBD it/PRP to/TO death/NN ;/: and/CC flung/VBD it/PRP back/RB to/TO me/PRP ./.
2 People/NNS feel/VBP with/IN their/PRP$ hearts/NNS ,/, Ellen/NNP ,/, and/CC since/IN he/PRP has/VBZ destroyed/VBN mine/NN ,/, I/PRP have/VBP not/RB power/NN to/TO feel/VB for/IN him/PRP ./.
                                                                                                 POStags
1        PRP, VBD, PRP, PRP$, NN, ,, CC, PRP, VBD, CC, VBD, PRP, TO, NN, :, CC, VBD, PRP, RB, TO, PRP, .
2 NNS, VBP, IN, PRP$, NNS, ,, NNP, ,, CC, IN, PRP, VBZ, VBN, NN, ,, PRP, VBP, RB, NN, TO, VB, IN, PRP, .

> 

Upvotes: 5

Related Questions