嗨,如何使用Jaro Winkler將List 1與List 2進行比較來計算字符串距離?
輸出文件應具有列表1和列表2中的名稱以及具有JW分數的第三列。
https://en.wikipedia.org/wiki/Jaro%E2%80%93Winkler_distance
列表1
約翰·史密斯
老虎伍茲
托尼莫莉
詹姆斯A.
米老鼠
唐納德
鄧肯甜甜圈
東京
列表2:
約翰遜史密斯
老虎
約翰莫莉
JAMES XXXX LLC
詹姆士
TOKIO
JMP提供函數Shortest Edit Script(),腳本索引中的示例提供了一個腳本來查找最長的公共序列。
為JSL Companion第二版編寫的腳本之一定義了Levenshtein距離並提供了示例;它附在下面。
Web不會附加jsl文件,因此我將擴展名更改為.txt
我沒有看到任何類似的統計數據,因此我在另一種語言中找到了一些代碼並將其移植到JMP。 下面是一個示例腳本,可以執行我認為您想要的內容。 將其複製到腳本窗口並運行它。 它生成單詞列表到單獨的數據表.....我相信這就是你想要的。 然後它將它們組合成一個數據表,並使用Jaro-Winkler統計信息創建一個新列。
names default to here(1);
dt1 = New Table( "Table 1",
Add Rows( 8 ),
New Column( "List 1",
Character,
"Nominal",
Set Values(
{"JOHN SMITH", "TIGER WOODS", "TONY MOLLY", "JAMES A", "MICKEY MOUSE",
"DONALD DUCK", "DUNKIN DONUTS", "TOKYO"}
)
)
);
dt2 = New Table( "Table 2",
Add Rows( 6 ),
New Column( "List 2",
Character,
"Nominal",
Set Values(
{"JOHNTHAN SMITH", "TIGER WOO", "JOHN MOLLY", "JAMES XXXX LLC", "JAMES",
"TOKIO"}
)
)
);
// Join the tables
dtJoined = dt1 << Join(
With( dt2 ),
By Row Number,
Output Table( "Joined Table" )
);
// Create New column
dtJoined << New Column("Jaro-Winkler");
// Add the functions that create the Jaro-Winkler
jaromatch = Function( {string1, string2}, {i},
str1_len = Length( string1 );
str2_len = Length( string2 );
allowedDist = Floor( Max( str1_len, str2_len ) / 2 ) - 1;
//*** walk through string1 and match characters to string2 ***;//
matchChars = "";
For( i = 1, i <= str1_len, i++,
//*** get the part of string2 to search ***;
allowed_start = Max( 1, i - allowedDist );
//*** starting char position ***;
allowed_str2 = Substr( string2, allowed_start, i + allowedDist - allowed_start + 1 );
//*** find i char from string1 in string2 within the allowedDist ***;
position = Contains( allowed_str2, Substr( string1, i, 1 ) );
If( position > 0,
matchChars = Concat( matchChars, Substr( allowed_str2, position, 1 ) );
//*** Once a char is assigned, it can not be assigned again. So, chg char in string2. ***;
string2 = Substr( string2, 1, allowed_start + position - 2 ) || "~" ||
Substr( string2, allowed_start + position );
);
);
Trim( matchChars );
);
/* count the number of "half" transpositions */
jarotrans = Function( {string1, string2},{i},
ntrans = 0;
For( i = 1, i <= Min( Length( Trim( string1 ) ), Length( Trim( string2 ) ) ), i++,
If( Substr( string1, i, 1 ) != Substr( string2, i, 1 ),
ntrans
++)
);
ntrans / 2;
);
/* get the length of the matching characters at the beginning */
getPrefixlen = Function( {string1, string2, maxprelen},{i},
n = Min( maxprelen, Length( string1 ), Length( string2 ) );
If( n == 0,
n = 0,
For( i = 1, i <= n, i++,
If( Substr( string1, i, 1 ) != Substr( string2, i, 1 ),
n = (i - 1)
); //*** all maxprelen characters match ***;
)
);
n;
);
/* calc the jaro distance */
jarodist = Function( {string1, string2}, {i},
//*** get number of matched characters in string1 ***;
m1 = jaromatch( string1, string2 );
m1_len = Length( m1 );
If( m1_len == 0, j_dist = 0 );
//*** get number of matched characters in string2 ***;
m2 = jaromatch( string2, string1 );
m2_len = Length( m2 );
If( m2_len == 0, j_dist = 0 );
//*** get number of transposed characters ***;
ntrans = jarotrans( m1, m2 );
//*** calc jaro distance ***;
j_dist = (m1_len / Length( string1 ) + m2_len / Length( string2 ) + (m1_len - ntrans) / m1_len) / 3;
j_dist;
);
/* calc the jaro-winkler distance */
jarowink = Function( {string1, string2, prefixscale},{i},
string1 = Uppercase( Trim( string1 ) );
string2 = Uppercase( Trim( string2 ) );
//*** check for trivial case and calc JW if needed ***;
If( string1 == string2,
returned = 1.0,
jarodist1 = jarodist( string1, string2 );
prelen = getPrefixlen( string1, string2, 4 );
returned = jarodist1 + prelen * prefixscale * (1 - jarodist1);
);
returned;
);
// Loop through the data table and create the values for the new column
For(i=1,i<=n rows(dtJoined), i++,
dtJoined:Name("Jaro-Winkler")[i] = jarowink(dtJoined:List 1[i], dtJoined:List2[i], 0.1);
)我們在JMP中是否有任何其他類似的內置函數哈羅,溫克勒?
從兩個不同的文件中讀取2個不同的列表是微不足道的,然後組合也不是問題。 真正的工作是開發一個計算Jaro-Winkler距離的腳本。 沒有內置函數來計算Jaro-Winkler距離。 雖然JMP可以處理這項任務,但它需要付出努力來開發它。