【问题标题】:Efficient way to remove all proper names from corpus从语料库中删除所有专有名称的有效方法
【发布时间】:2017-01-01 15:53:33
【问题描述】:

在 R 中工作时,我试图找到一种有效的方法来搜索文本文件并删除或替换所有专有名称的实例(例如 Thomas)。我认为有一些东西可以做到这一点,但一直无法找到。

因此,在本例中,“Susan”和“Bob”这两个词将被删除。这是一个简化的示例,而实际上希望将其应用于数百个文档,因此需要一个相当大的名称列表。

texts <- as.data.frame (rbind (
    'This text stuff if quite interesting',
    'Where are all the names said Susan',
   'Bob wondered what happened to all the proper nouns'
    ))
names(texts) [1] <- "text"

【问题讨论】:

  • 除非您有一组固定的名称,否则这可能并不简单。你当然可以在网上找到一个常见的美国名字列表并将其添加到你的停用词词典中,但你永远不会得到所有的名字。
  • 对于这个例子:nms &lt;- c('Susan','Bob'); gsub(paste0(nms, collapse = '|'), '', texts$text)(正如@Hack-R所说:你需要一组固定的名称)。
  • 尝试寻找命名实体提取_/_命名实体识别,这是一个相当广泛的领域
  • 使用一些浅层解析器,例如依赖解析器和/或某些模型(例如 hmm)来训练一个词性标注器。
  • 投票重新开放,这是一个完全合理的问题。

标签: r text


【解决方案1】:

这是一种基于名字数据集的方法:

install.packages("gender") 
library(gender)
install_genderdata_package()

sets <- data(package = "genderdata")$results[,"Item"]
data(list = sets, package = "genderdata")
stopwords <- unique(kantrowitz$name)

texts <- as.data.frame (rbind (
  'This text stuff if quite interesting',
  'Where are all the names said Susan',
  'Bob wondered what happened to all the proper nouns'
))

removeWords <- function(txt, words, n = 30000L) {
  l <- cumsum(nchar(words)+c(0, rep(1, length(words)-1)))
  groups <- cut(l, breaks = seq(1,ceiling(tail(l, 1)/n)*n+1, by = n))
  regexes <- sapply(split(words, groups), function(words) sprintf("(*UCP)\\b(%s)\\b", paste(sort(words, decreasing = TRUE), collapse = "|")))
  for (regex in regexes)  txt <- gsub(regex, "", txt, perl = TRUE, ignore.case = TRUE)
  return(txt)
}
removeWords(texts[,1], stopwords)
# [1] "This text stuff if quite interesting"           
# [2] "Where are all the names said "                  
# [3] " wondered what happened to all the proper nouns"

它可能需要针对您的特定数据集进行一些调整。

另一种方法可以基于词性标记。

【讨论】:

    猜你喜欢
    • 2012-04-10
    • 2020-03-03
    • 1970-01-01
    • 1970-01-01
    • 2022-03-07
    • 2017-05-10
    • 2016-04-11
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多