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

标签 r text nlp classification

我有一个像下面这样的向量,想确定列表中的哪些元素是人名,哪些不是。我找到了 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 

关于r - 从字符串列表中,确定哪些是人名,哪些不是,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32545180/

相关文章:

R 分号将列分隔为行

python-3.x - FastText 0.9.2 - 召回是 'nan' 但精度是一个数字

python - 使用 NLTK 对 POS 标记词进行词形还原?

R Shiny : How to have sequential Modals in for loop

r - 带有一个因子的多列的ggplot箱线图

r - ggplot2 段——x 轴为时间时的方向

text - D3 使用文本而不是节点的力导向图

text - Lua 匹配错误的模式

python - 如何从 python 中的孟加拉语文本中删除外来词

node.js - 我如何检测具有上下文的意图? (Dialogflow API V2 的 Node.js SDK)