如何制作对用户生成的拼写错误有效的工具?

数据挖掘 r 数据争吵 错误处理
2022-02-14 19:59:43

背景:客户正在生成数据集(excel 文件)。他们要求我制作一个应用程序来分析数据集,例如汇总表和数字。我在 R 闪亮中这样做。

问题:用户生成的数据中有很多错别字。例如,Alice有时输入为Alce, alice, alice., Aalice, 或Jennifer (middle name). 有些错误很容易纠正,例如尾随空格和大写/小写。其他人几乎是不可能的,就像知道Jennifer (middle name)是真的一样Alice

当我无法控制数据输入时,如何使我的工具对数据中的错误具有鲁棒性?

2个回答

TL;博士

  1. 使用模糊字符串匹配来解决拼写错误。
  2. 解决这些Jennifer问题需要Alice您知道在数据库中查找这些情况的位置,或者讨论制作更好的 Excel 文件以强制人们仅输入名字(例如,对限制在给定列表的单元格进行输入)。

模糊字符串匹配

在 R 中,您可以使用adiststringdist包。这些可用于测量从条目(例如Aalice到潜在匹配列表)的距离[Alice, Bianca, Chris]

是一篇解释如何使用两者的文章。

文章摘录:

source1.devices<-read.csv('[path_to_your_source1.csv]')
source2.devices<-read.csv('[path_to_your_source2.csv]')
# To make sure we are dealing with charts
source1.devices$name<-as.character(source1.devices$name)
source2.devices$name<-as.character(source2.devices$name)

# It creates a matrix with the Standard Levenshtein distance between the name fields of both sources
dist.name<-adist(source1.devices$name,source2.devices$name, partial = TRUE, ignore.case = TRUE)

# We now take the pairs with the minimum distance
min.name<-apply(dist.name, 1, min)

match.s1.s2<-NULL  
for(i in 1:nrow(dist.name))
{
    s2.i<-match(min.name[i],dist.name[i,])
    s1.i<-i
    match.s1.s2<-rbind(data.frame(s2.i=s2.i,s1.i=s1.i,s2name=source2.devices[s2.i,]$name, s1name=source1.devices[s1.i,]$name, adist=min.name[i]),match.s1.s2)
}
# and we then can have a look at the results
View(match.s1.s2)

所有这些都假设您有一个实际有效的名称列表。

您可以使用正则表达式解决拼写错误问题。关于知道谁使用中间名和不使用中间名,您将不得不告诉 Alice,她在数据库中的身份是 Jennifer。

对于正则表达式:这是Jan Goyvaerts 和 Steven Levithan(2012 年)的《 Regular Expressions Cookbook》一书的释义节选。我推荐这本书。

要匹配相似的单词或名称:

# color or colour
colou?r

# Bat, cat, or rat
[bcr]at

# words ending with "phobia"
\\w*phobia

# Steve, Steve, or Stephen
Ste(?:ven?|phen)

基于浏览器的正则表达式测试器: https ://spannbaueradam.shinyapps.io/r_regex_tester/

https://github.com/AdamSpannbauer/r_regex_tester_app

您将需要发挥创造力并找到一种方法来编写适用于您的特定问题的正则表达式,或者您可以提供一个可重复的示例并祈祷这里有人会为您解决。