Вероятность отсечения для сущностей - PullRequest
0 голосов
/ 06 февраля 2020

Я использую пакет openNLP в R Studio для извлечения именованных сущностей из совокупности текстовых документов.

Например:

#Take in text document , store as a string
s <- paste(c("For two-minute audio updates, try The Briefing",
  "- on podcasts, smart speakers and WhatsApp.",
  "Huawei will be allowed to build parts of Britain's 5G network, Boris Johnson has ruled."),
  collapse = "") 
s <- as.String(s)

#Get sentence and word annotations
sent_token_annotator <- Maxent_Sent_Token_Annotator()
word_token_annotator <- Maxent_Word_Token_Annotator()
a2 <- annotate(s, list(sent_token_annotator, word_token_annotator))

#perform entity recognition for persons
entity_annotator <- Maxent_Entity_Annotator()

#Return named entities
s[entity_annotator(s, a2)]
#Output:
# [1] "WhatsApp"      "Boris Johnson"

#Get all annotations
annotate(s, Maxent_Entity_Annotator(probs = TRUE), a2)
#id type     start end features
#  1 sentence     1  89 constituents=<<integer,16>>
#  2 sentence    91 177 constituents=<<integer,18>>
#  . . . . . . . . . . 
#  . . . . . . . . . . 
#  . . . . . . . . . . 
# 35 word       172 176 
# 36 word       177 177 
# 37 entity      81  88 kind=person, prob=0.6929361
# 38 entity     154 166 kind=person, prob=0.9403688

Как вы можете видеть в этом коде, слова «WhatsApp» и «Борис Джонсон» аннотируются как именованные лица. Тем не менее, у «WhatsApp» есть шанс только 69% быть человеком, в то время как у Бориса Джонсона - 94%. Моя проблема заключается в том, как изменить этот код так, чтобы он возвращал только именованные объекты с вероятностью выше определенного процента того, чтобы быть человеком? В этом случае я не хотел бы, чтобы какие-либо именованные объекты с вероятностью менее 70% были показаны как люди.

Редактировать

Хорошо, думаю, я нашел решение.

Новый код:

entity_annotator <- Maxent_Entity_Annotator(probs = TRUE)
#Get sentence, word, and entity annotations all together
a2 <- annotate(s, list(sent_token_annotator, word_token_annotator, entity_annotator))

#Get the words annotated as 'entity'
entities <- a2[which(a2$type == "entity")]
#Create an empty array
NEs <- c()

#Loop through entities
for(i in seq(1:length(entities))) {
   #Store the probability of an entity being a person
    prob <- entities[i]$features[[1]][[2]]
    #If that probability is above a certain threshold, store the index
    #of that entity in the array
    if(prob >= 0.7) {
      NEs <- c(NEs, entities[i]$id)
    }
  }

#Return Named Entities (NEs) with a probability over 70% of being a person
s[a2][NEs]
#Output:
#[1] "Boris Johnson"

...