R - 采用levenshtein距离的多列文本挖掘

问题描述 投票:1回答:1

我想通过levenshtein距离(adist中的R函数)在多个列中多次比较文本字符串。我想做的是比较source1$namesource2$name。如果没有匹配(即如果为NA返回match.s1.s2$s2.i,则使用两个数据框中列出的地址(source1$addresssource2$address)执行相同的功能。基本上我正在寻找一种方法来优化我在多个字段中的匹配。在下面。

name <- c("holiday inn", "geico", "zgf", "morton phillips")
address <- c("400 lafayette pl tupelo ms", "227 geico plaza chevy chase md", 
         "811 quincy st washington dc", "1911 1st st rockville md")

source1 <- data.frame(name, address)

name <- c("williams sonoma", "mamas bbq", "davis polk", "hop a long diner",
      "joes crag shack", "mike lowry place", "holiday inn", "zummer")

name2 <- c(NA, NA, NA, NA, NA, NA, "hi express", "zummer gunsul frasca")
address <- c("2 reads way new castle de", "248 w 4th st newark de",
         "1100 21st st nw washington dc", "1804 w 5th st wilmington de",
         "1208 kenwood parkway holdridge nb", "4203 ocean drive miami fl",
         "400 lafayette pl tupelo ms", "811 quincy st washington dc")
source2 <- data.frame(name, name2, address)

removeSPE <- function(x) gsub("[[:punct:]]", " ", x)

cleanup <- function(x){
x <- as.character(x) # convert to character
x <- tolower(x) # make all lowercase
x <- sapply(x, removeSPE) # remove special characters
x <- trimws(x) # remove extra white space
#x <- sapply(x, removeStopWords) # remove stopwords, defined above
#x <- trimws(x) # since stopwords have been removed, there is extra white space left, this removes it
x <- gsub("^. .$", "", x)
return(x)
}

source1$name <- cleanup(source1$name)
source2$name <- cleanup(source2$name)
source2$name2 <- cleanup(source2$name2)

source1$address <- cleanup(source1$address)
source2$address <- cleanup(source2$address)

source1$name <- cleanup(source1$name)
source2$name <- cleanup(source2$name)
source2$name2 <- cleanup(source2$name2)

dist.name<- adist(source1$name,source2$name, partial = TRUE, ignore.case = TRUE)
dist.name2 <- adist(source1$name, source2$name2, partial = TRUE, ignore.case = TRUE)
dist.address <- adist(source1$address, source2$address, partial = TRUE, ignore.case = TRUE)

min.name<-apply(dist.name, 2, min)
min.name2 <- apply(dist.name2, 2, min)


match.s1.s2<-NULL  
for(i in 1:nrow(dist.address))
{
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[s2.i,]$name, s1name=source1[s1.i,]$name, 
                            adist=min.name[i], s1.i.address = source1[s1.i,]$address,
                            s2.i.address = source2[s2.i,]$address),match.s1.s2)
}

match.s1.s2

期望的结果是source1中的第3行与source2的第8行匹配,source1的第4行与source2的第7行匹配。有没有办法在上面的dist.name2中加入dist.addressfor-loop(如上所述)?也许一阵声明?我将使用的实际数据帧大约有500和24,000行。

r for-loop text tm levenshtein-distance
1个回答
1
投票

余弦距离看起来相当不错:

out_df <- c()
for(x in source1$name) {
  for(y in source2$full2) {
    if (is.na(source2[source2$full2 == y, "name2"])) {
      x2 <- source1[source1$name == x, "address"]
      y2 <- source2[source2$full2 == y, "address"]
      row <- data.frame(x, y2, stringdist(x, y, method="cosine", q = 1))
      names(row) <- c("name1", "full2", "distance")
      out_df <- rbind(out_df, row)
    } else {
      row <- data.frame(x, y, stringdist(x, y, method="cosine", q = 1))
      names(row) <- c("name1", "full2", "distance")
      out_df <- rbind(out_df, row)
    }
  }
}

names(out_df) <- c("name1", "full2", "distance")

grab <- aggregate(distance ~ name1, data = out_df, FUN = min)

merge(out_df, grab)

您仍然需要弄清楚如何排除您不想要的结果。

© www.soinside.com 2019 - 2024. All rights reserved.