Last active
June 4, 2016 17:08
-
-
Save mckelvin/3481054 to your computer and use it in GitHub Desktop.
code related at: http://blog.mckelv.in/articles/1453.html
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/python2 | |
# -*- coding:UTF-8 -*- | |
# code related at: http://blog.mckelv.in/articles/1453.html | |
import sys | |
distance = lambda a,b : 0 if a==b else 1 | |
def dtw(sa,sb): | |
''' | |
>>>dtw(u"幹啦今今今今今天天氣氣氣氣氣好好好好啊啊啊", u"今天天氣好好啊") | |
2 | |
''' | |
MAX_COST = 1<<32 | |
#初始化一個len(sb) 行(i),len(sa)列(j)的二維矩陣 | |
len_sa = len(sa) | |
len_sb = len(sb) | |
# BUG:這樣是錯誤的(淺拷貝): dtw_array = [[MAX_COST]*len(sa)]*len(sb) | |
dtw_array = [[MAX_COST for i in range(len_sa)] for j in range(len_sb)] | |
dtw_array[0][0] = distance(sa[0],sb[0]) | |
for i in xrange(0, len_sb): | |
for j in xrange(0, len_sa): | |
if i+j==0: | |
continue | |
nb = [] | |
if i > 0: nb.append(dtw_array[i-1][j]) | |
if j > 0: nb.append(dtw_array[i][j-1]) | |
if i > 0 and j > 0: nb.append(dtw_array[i-1][j-1]) | |
min_route = min(nb) | |
cost = distance(sa[j],sb[i]) | |
dtw_array[i][j] = cost + min_route | |
return dtw_array[len_sb-1][len_sa-1] | |
def main(argv): | |
s1 = u'幹啦今今今今今天天氣氣氣氣氣好好好好啊啊啊' | |
s2 = u'今天天氣好好啊' | |
d = dtw(s1, s2) | |
print d | |
return 0 | |
if __name__ == '__main__': | |
sys.exit(main(sys.argv)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment