Извлечение действий над объектами из предложения в R

Я хочу извлечь действия над объектами из списка предложений в R. Чтобы дать небольшой обзор.

S = “The boy opened the box. He took the chocolates. He ate the chocolates. 
     He went to school”

Я ищу следующие комбинации:

Opened box
Took chocolates
Ate chocolates
Went school

Мне удалось извлечь глаголы и существительные по отдельности. Но не могу найти способ объединить их, чтобы получить такие идеи.

library(openNLP)
library(openNLPmodels.en)
library(NLP)

s = as.String("The boy opened the box. He took the chocolates. He ate the 
               chocolates. He went to school")

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

nouns = c("/NN", "/NNS","/NNP","/NNPS")
verbs = c("/VB","/VBD","/VBG","/VBN","/VBP","/VBZ")

s = tolower(s)
s = gsub("\n","",s)
s = gsub('"',"",s)

tags = tagPOS(s)
tags = tags$POStagged
tags = unlist(strsplit(tags, split=","))

nouns_present = tags[grepl(paste(nouns, collapse = "|"), tags)]
nouns_present = unique(nouns_present)
verbs_present = tags[grepl(paste(verbs, collapse = "|"), tags)]
verbs_present = unique(verbs_present)
nouns_present<- gsub("^(.*?)/.*", "\\1", nouns_present)
verbs_present = gsub("^(.*?)/.*", "\\1", verbs_present)
nouns_present = 
paste("'",as.character(nouns_present),"'",collapse=",",sep="")
verbs_present = 
paste("'",as.character(verbs_present),"'",collapse=",",sep="")

Идея состоит в том, чтобы построить сетевой граф, в котором при нажатии на узел глагола будут появляться все прикрепленные к нему объекты, и наоборот. Любая помощь в этом была бы отличной.


person Priyanka Basu    schedule 27.09.2017    source источник


Ответы (1)


Я предполагаю, что вы также хотите получить слова до и после ключевых глаголов действия. Я смог добиться этого, используя пакет tidytext. (Ссылка: https://uc-r.github.io/word_relationships)

library(tidytext)
library(tidyverse)

#first create another column with divided up text strings by n(i set as every two words paired together)
mydf <-unnest_tokens(comments, "tokens", Response, token = "ngrams", n=2, to_lower = TRUE, drop = FALSE)

#remove stopwords:
mydf %>%
  separate(tokens, c("word1", "word2"), sep = " ") %>%
  filter(!word1 %in% stop_words$word,
         !word2 %in% stop_words$word,
         ) %>%
  count(word1, word2, sort = TRUE) %>% view()
person Ellen-Yilun Wang    schedule 01.03.2020