当您说词嵌入等可能会产生“糟糕”的结果时,我认为您是对的,因为名称和地址是高度个性化的。
根据您的问题类型,您可能需要比较1:n,这是昂贵的。由于每个条目只需要一次查看彼此的条目,因此您可以通过使用itertools.combinations(lst, 2)生成唯一对来减少工作量。但是,有 9 mio。条目这仍然会提供大约 40,500,000,000,000 对来检查(包括“自我”作为参考)。
如果您不想使用string distances,一种可能的解决方案是将单个矢量化条目与所有其他条目进行比较。这可以基于计数矢量化器来完成n字符级别的-grams。通过这样做,您可以比较“相似”的名称、地址等。冗余文本在这里似乎不是一个大问题。
from sklearn.feature_extraction.text import CountVectorizer
true = ["Mister Example 10 Liversidge St Acton ACT 2601 Australia"]
alt1 = ["Mr Example Liversidge St 10 2601 Acton ACT Australia"]
alt2 = ["Mister Example 10 Liversidge St Acton ACT 2601 Australia and some redundant text here"]
alt3 = ["Ms Catlady 11 Liversidge St Acton ACT 2601 Australia"]
alt4 = ["Mr Entirely Different Other Street 123 9031 Anywhere USA"]
vec = CountVectorizer(analyzer="char_wb", ngram_range=(3,3))
x = vec.fit(true)
print(x.get_feature_names())
print(vec.transform(true).todense().sum()) # "truth" is the reference here
print(vec.transform(alt1).todense().sum())
print(vec.transform(alt2).todense().sum())
print(vec.transform(alt3).todense().sum())
print(vec.transform(alt4).todense().sum())
# The single arguments true, alt1 etc can also be packt into a vector and transformed in one step
这将给出:
48 # truth as reference value
42 # minor variations to truth
48 # truth with redundant text
33 # similar address
3 # different address