首页 > 解决方案 > 从R中的文本中提取专有名词?

问题描述

有没有更好的方法从自由文本中提取专有名词(例如“London”、“John Smith”、“Gulf of Carpentaria”)?

也就是说,像这样的函数

proper_nouns <- function(text_input) {
  # ...
}

这样它将从文本输入中提取专有名词列表。

例子

这是一组 7 个文本输入(有些容易,有些难):

text_inputs <- c("a rainy London day",
  "do you know John Smith?",
  "sail the Adriatic",
  
  # tougher examples
  
  "Hey Tom, where's Fred?" # more than one proper noun in the sentence
  "Hi Lisa, I'm Joan." # more than one proper noun in the sentence, separated by capitalized word
  "sail the Gulf of Carpentaria", # proper noun containing an uncapitalized word
  "The great Joost van der Westhuizen." # proper noun containing two uncapitalized words
  )

以下是这样的函数、规则集或 AI 应该返回的内容:

proper_nouns(text_inputs)

[[1]]
[1] "London"

[[2]]
[1] "John Smith" 

[[3]]
[1] "Adriatic"

[[4]]
[1] "Tom"    "Fred"

[[5]]
[1] "Lisa"    "Joan"

[[6]]
[1] "Gulf of Carpentaria"

[[7]]
[1] "Joost van der Westhuizen"

问题:简单的正则表达式是不完美的

考虑一些简单的正则表达式规则,它们有明显的缺陷:

问题

我目前最好的方法是简单地使用上面的正则表达式并且成功率很低。有没有更好或更准确的方法从 R 中的文本中提取专有名词?如果我能在真实文本上获得 80-90% 的准确率,那就太好了。

标签: rnlptidytext

解决方案


您可以从查看spacyr库开始。

library(spacyr)
result <- spacy_parse(text_inputs, tag = TRUE, pos = TRUE)
proper_nouns <- subset(result, pos == 'PROPN')
split(proper_nouns$token, proper_nouns$doc_id)

#$text1
#[1] "London"

#$text2
#[1] "John"  "Smith"

#$text3
#[1] "Adriatic"

#$text4
#[1] "Hey" "Tom"

#$text5
#[1] "Lisa" "Joan"

#$text6
#[1] "Gulf"        "Carpentaria"

"John"因此,这将每个单词分开处理,"Smith"而不是组合在一起。您可能需要在此基础上添加一些规则,并根据需要进行一些后处理。


推荐阅读