什麼是一個好辦法,組名稱的列表:算法分組名稱
Doctor Watson.
Dr. John Watson.
Dr. J Watson.
Watson.
J Watson.
Sherlock.
Mr. Holmes.
S Holmes.
Holmes.
Sherlock Holmes.
成獨特而完整的名稱的分組列表:
Dr. John Watson.
Mr. Sherlock Holmes.
也很有趣:
Mr Watson
Watson
Mrs Watson
Watson
John Watson
由於該算法不需要推論第一個沃森是否是Mr(可能)或是Mrs,而只是第組唯一的問題是,John Watson顯然屬於Mr,而不是Watson夫人。沒有每個性別的名字的字典,這是無法推斷的。
到目前爲止,我已經想過遍歷列表並檢查每個項目與剩餘的項目。在每場比賽中,你再次分組並從頭開始,並且在沒有進行分組的第一輪中停止。
下面是一些粗糙的(還沒有經過測試的)Python。你會用名稱列表來調用它。
def groupedNames(ns):
if len(ns) > 1:
# First item is query, rest are target names to try matching
q = ns[0]
# For storing unmatched names, passed on later
unmatched = []
for i in range(1,len(ns)):
t = ts[i]
if areMatchingNames(q,t):
# groupNames() groups two names into one, retaining all info
return groupedNames([groupNames(q,t)] + unmatched + ns[i+1:])
else:
unmatched.append(t)
# When matching is finished
return ns
這功課嗎?如果是這樣,它應該被標記爲這樣。 – 2012-04-14 17:39:40