Levenshtein / edit distance for arbitrary sequences [duplicate]

∥☆過路亽.° 提交于 2019-12-11 07:13:52

问题


I want to compute the Levenshtein distance between two arbitrary sequences.

a <- 1:100
b <- c(1, 1:100)

edit_distance(a, b) == 1

I am aware of the adist function and the stringdist package, but they only work on character vectors. If the number of symbols in the sequences were small, I could just encode them as characters and use the above functions.

But there will typically be on the order of 1000 different symbols. Another option would be to encode them as Unicode characters (adist works on them: adist("\U00001", "\U00001\U00002")), but I don't know how to do this.


回答1:


You can use intToUtf8 to map your integers to Unicode characters:

a2 <- intToUtf8(a)
b2 <- intToUtf8(b)

adist(a2, b2)
#      [,1]
# [1,]    1


来源:https://stackoverflow.com/questions/43232409/levenshtein-edit-distance-for-arbitrary-sequences

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!