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

前端之家收集整理的这篇文章主要介绍了从字符串列表中,确定哪些是人名,哪些不是前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我有一个类似下面的矢量,并希望确定列表中的哪些元素是人名,哪些不是.我发现了humaniformat包,它可以格式化名称,但遗憾的是并不能确定字符串是否实际上是一个名称.我还发现了一些用于实体提取的包,但它们似乎需要实际的文本来进行词性标注,而不是单个名称.

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

解决方法

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

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

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

猜你在找的HTML相关文章