微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

从字符串列表中,确定哪些是人名,哪些不是

我有一个类似下面的矢量,并希望确定列表中的哪些元素是人名,哪些不是.我发现了humaniformat包,它可以格式化名称,但遗憾的是并不能确定字符串是否实际上是一个名称.我还发现了一些用于实体提取的包,但它们似乎需要实际的文本来进行词性标注,而不是单个名称.

pkd.names.quotes <- c("Mr. Rick Deckard",# Name
                      "Do Androids Dream of Electric Sheep",# Not a name
                      "Roy Batty",# Name 
                      "How much is an electric ostrich?",# Not a name
                      "My schedule for today lists a six-hour self-accusatory depression.",# Not a name
                      "Upon him the contempt of three planets descended.",# Not a name
                      "J.F. Sebastian",# Name
                      "Harry Bryant",# Name
                      "goat class",# Not a name
                      "Holden,Dave",# Name
                      "Leon Kowalski",# Name
                      "Dr. Eldon Tyrell") # Name

解决方法

这是一种方法.美国人口普查局列出了一个姓氏列表>在其数据库中100次(有频率):全部152,000.如果使用完整列表,则所有字符串都有一个名称.例如,“class”,“him”和“the”是某些语言的名称(不知道哪种语言).同样,有许多名字列表(见 this post).

下面的代码抓住了2000年人口普查中的所有姓氏,并列出了所引用帖子的名字,然后是每个列表中最常见的10,000个子集,组合并清理列表,并将其用作tm包中的字典识别哪些字符串包含名称.您可以通过更改freq变量来控制“灵敏度”(freq = 10,000似乎可以生成您想要的结果).

url <- "http://www2.census.gov/topics/genealogy/2000surnames/names.zip"
tf <- tempfile()
download.file(url,tf,mode="wb")                     # download archive of surname data
files    <- unzip(tf,exdir=tempdir())               # unzips and returns a vector of file names
surnames <- read.csv(files[grepl("\\.csv$",files)])  # 152,000 surnames occurring >100 times
url <- "http://deron.meranda.us/data/census-derived-all-first.txt"
firstnames <- read.table(url(url),header=FALSE)
freq <- 10000
dict  <- unique(c(tolower(surnames$name[1:freq]),tolower(firstnames$V1[1:freq])))
library(tm)
corp <- Corpus(VectorSource(pkd.names.quotes))
tdm  <- TermDocumentMatrix(corp,control=list(tolower=TRUE,dictionary=dict))
m    <- as.matrix(tdm)
m    <- m[rowSums(m)>0,]
m
#            Docs
# Terms       1 2 3 4 5 6 7 8 9 10 11 12
#   bryant    0 0 0 0 0 0 0 1 0  0  0  0
#   dave      0 0 0 0 0 0 0 0 0  1  0  0
#   deckard   1 0 0 0 0 0 0 0 0  0  0  0
#   eldon     0 0 0 0 0 0 0 0 0  0  0  1
#   harry     0 0 0 0 0 0 0 1 0  0  0  0
#   kowalski  0 0 0 0 0 0 0 0 0  0  1  0
#   leon      0 0 0 0 0 0 0 0 0  0  1  0
#   rick      1 0 0 0 0 0 0 0 0  0  0  0
#   roy       0 0 1 0 0 0 0 0 0  0  0  0
#   sebastian 0 0 0 0 0 0 1 0 0  0  0  0
#   tyrell    0 0 0 0 0 0 0 0 0  0  0  1
which(colSums(m)>0)
#  1  3  7  8 10 11 12

原文地址:https://www.jb51.cc/html/226381.html

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐