python - 如何在 python 中将最多 "similar"字符串从一个列表映射到另一个列表?

标签 python string-matching

给定两个包含字符串的列表。

  1. 一个包含世界各地组织(主要是大学)的名称 - 不仅用英文书写,而且始终使用拉丁字母。

  2. 另一个列表主要包含完整地址,其中可能出现第一个列表中的字符串(组织)。

一个例子:

addresses = [
             "Department of Computer Science, Katholieke Universiteit Leuven, Leuven, Belgium",
             "Machine Learning and Computational Biology Research Group, Max Planck Institutes     Tübingen, Tübingen, Germany 72076",
             "Department of Computer Science and Engineering, University of Washington, Seattle, USA 98185",
             "Knowledge Discovery Department, Fraunhofer IAIS, Sankt Augustin, Germany 53754",    
             "Computer Science Department, University of California, Santa Barbara, USA 93106",
             "Fraunhofer IAIS, Sankt Augustin, Germany",
             "Department of Computer Science, Cornell University, Ithaca, NY",
             "University of Wisconsin-Madison"
            ]

organisations = [
                 "Catholic University of Leuven"
                 "Fraunhofer IAIS"
                 "Cornell University of Ithaca"
                 "Tübingener Max Plank Institut"
                ]

如您所见,所需的映射为:

"Department of Computer Science, Katholieke Universiteit Leuven, Leuven, Belgium",
--> Catholic University of  Leuven
"Machine Learning and Computational Biology Research Group, Max Planck Institutes     Tübingen, Tübingen, Germany 72076",
--> Max Plank Institut Tübingen
"Department of Computer Science and Engineering, University of Washington, Seattle, USA 98185",
--> --
"Knowledge Discovery Department, Fraunhofer IAIS, Sankt Augustin, Germany 53754",
--> Fraunhofer IAIS 
"Computer Science Department, University of California, Santa Barbara, USA 93106",
"Fraunhofer IAIS, Sankt Augustin, Germany",
--> Fraunhofer IAIS
"Department of Computer Science, Cornell University, Ithaca, NY"
--> "Cornell University of Ithaca",
"University of Wisconsin-Madison",
--> --

我的想法是使用某种“距离算法”来计算字符串的相似度。因为我不能仅仅通过执行 if address in organization 来查找地址中的组织,因为它在不同地方的写法可能略有不同。所以我的第一个猜测是使用 difflib 模块。特别是 difflib.get_close_matches() 函数,用于从组织列表中为每个地址选择最接近的字符串。但我不太有信心,结果是否足够准确。虽然我不知道我应该将接缝的比率设置多高作为相似性度量。

在花太多时间尝试 difflib 模块之前,我想问问这里更有经验的人,这是否是正确的方法,或者是否有更适合的工具/方法来解决我的问题。谢谢!

PS:我不需要最优解。

最佳答案

使用以下作为您的字符串距离函数(而不是普通的 levenshtein 距离):

def strdist(s1, s2):
    words1 = set(w for w in s1.split() if len(w) > 3)
    words2 = set(w for w in s2.split() if len(w) > 3)

    scores = [min(levenshtein(w1, w2) for w2 in words2) for w1 in words1]
    n_shared_words = len([s for s in scores if s <= 3])
    return -n_shared_words 

然后使用 Munkres 分配算法 shown here因为在组织和地址之间似乎存在 1:1 的映射。

关于python - 如何在 python 中将最多 "similar"字符串从一个列表映射到另一个列表?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8432799/

相关文章:

Python - 网页抓取 - 多线程 - 线程限制?

python - 编辑:pandas multiline value in ipython notebook

Python 诗歌 : Where is get-poetry. py?

string-matching - 一种更好的变长字符串相似度排序算法

Swift:如何识别和删除字符串中的介词

python - 根据另一个嵌套列表对嵌套列表进行排序

javascript - 从字符串 jquery 中删除所有非数字字符?

python - 数字作为 statsmodels.formula.api 无法识别的变量名称

python - 执行图像配准时图像之间的匹配不当

sql - 在sql中选择带有短语计数的数据