“如何绘制最新地图”;类似的“;python中从一个列表到另一个列表的字符串?

“如何绘制最新地图”;类似的“;python中从一个列表到另一个列表的字符串?,python,string-matching,Python,String Matching,给出了两个包含字符串的列表 其中一个包含世界各地组织(主要是大学)的名称——不仅用英语书写,而且总是使用拉丁字母 另一个列表包含大部分完整地址,其中可能出现第一个列表中的字符串(组织) 例如: addresses = [ "Department of Computer Science, Katholieke Universiteit Leuven, Leuven, Belgium", "Machine Learning and Computat

给出了两个包含字符串的列表

  • 其中一个包含世界各地组织(主要是大学)的名称——不仅用英语书写,而且总是使用拉丁字母

  • 另一个列表包含大部分完整地址,其中可能出现第一个列表中的字符串(组织)

  • 例如:

    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\u close\u matches()
    函数,用于为每个地址从组织列表中选择最近的字符串。但我不太相信,结果会足够准确。虽然我不知道我应该设置多高的比率来作为相似性度量

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


    PS:我不需要最佳解决方案。

    你可以使用soundex或metaphone将句子翻译成音素列表,然后比较最相似的列表


    以下是的Python实现。

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

    def标准列表(s1、s2): words1=set(如果len(w)>3,则s1.split()中的w代表w) words2=set(如果len(w)>3,则s2.split()中的w代表w) 分数=[min(单词2中w2的levenshtein(w1,w2))和单词1的w1]
    n_shared_words=len([s代表分数中的s,如果s可能对你有用:@Rodin:The表示Levenstein距离根据“操作”的数量度量距离)需要将一个字符串转换为另一个字符串。这些操作包括插入、删除和替换。我的组织列表有大约8000个条目,我的地址列表有230000个条目。如果组织字符串很短(例如Fraunhofer IAIS)而地址很长,Levenstein是否仍有路要走(例如:知识发现部门,德国圣奥古斯丁弗劳恩霍夫IAIS,邮编53754)?我认为你的假设是正确的。我想到了莱文施坦距离,这是:。@Aufwind:你应该在比较字符串之前将字符串拆分为单词,然后计算匹配的单词数。例如,“弗劳恩霍夫IAIS”,在每个地址中搜索“Fraunhofer”和“IAIS”的类似单词。您还应规范所有单词的大小写(例如,小写),并可能希望忽略“of”等“噪音词”。给出“精确匹配=5,紧密匹配=1”等分数并以得分最高的地址为例。也许一个好的启发方法是给长匹配更高的分数。哦,这里有一篇与你的问题密切相关的非常有趣的阅读:“如何编写拼写更正器”
    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