r - 从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"
问题:简单的正则表达式是不完美的
考虑一些简单的正则表达式规则,它们有明显的缺陷:
规则:取大写单词,除非它们是句子中的第一个单词(通常是大写的)。问题:会在句首漏掉专有名词。
规则:假设连续的大写单词是同一个专有名词的一部分(多部分专有名词,如
"John Smith"
)。问题:"Gulf of Carpentaria"
将被遗漏,因为它之间有一个未大写的单词。- 包含非大写单词的人名也存在类似问题,例如
"Joost van der Westhuizen"
.
- 包含非大写单词的人名也存在类似问题,例如
问题
我目前最好的方法是简单地使用上面的正则表达式并且成功率很低。有没有更好或更准确的方法从 R 中的文本中提取专有名词?如果我能在真实文本上获得 80-90% 的准确率,那就太好了。
解决方案
您可以从查看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"
而不是组合在一起。您可能需要在此基础上添加一些规则,并根据需要进行一些后处理。
推荐阅读
- azure - Azure 数据工厂发布管道 - 找不到资源错误
- python - Python异步调用分离函数
- python - 无法在 Python 3.7.1 上安装 SSL?
- java - 无法将 java.lang.String 类型的对象转换为 com.example.ezcompras.model.Lojas 类型
- spring-boot - @FeignClient 在 Spring Boot 中不能与 @Autowired 一起使用
- excel - 对两个数组中的逻辑值进行与运算和计数
- python - 为什么 Python eval 为 Keras 正则化器返回相同的对象?
- javascript - 如何在不拆分文件的情况下将文件发布到本地主机?(使用 ajax/jquery)
- java - 为什么这不关闭 CallableStatement?
- spring-boot - 我是否需要使用 Kotlin Junit5 和 Spring Boot 将 Spring 注释复制到内部类?