【问题标题】:match_most_similar in Python string_grouper returning original stringsPython string_grouper 中的 match_most_similar 返回原始字符串
【发布时间】:2021-02-25 16:59:05
【问题描述】:

我有一个杂乱的字符串列表,我想从格式清晰的字符串列表中为每个字符串找到最佳匹配,该列表还包含有关每个字符串的元数据。混乱列表中的字符串在列表中随机重复(通常使用字符串的替代拼写)。乱七八糟的字符串列表太长了,循环fuzzywuzzy是不可行的。

我一直在尝试使用来自string_grouper 库的match_most_similar。当我使用此代码应用该功能时:

import pandas as pd
import numpy as np
from string_grouper import match_strings, match_most_similar, group_similar_strings, StringGrouper
new_strings = pd.Series(df['Cited'])
# Create all matches:
matches = match_most_similar(data['caseName'], new_strings)

# Display the results:
pd.DataFrame({'new_strings': new_strings, 'duplicates': matches})

match_most_similar 函数返回原始字符串,而不是它们在干净列表中的匹配项。 (data['caseName'] 是干净的字符串列表。)这是输出。 duplicates 中出现的内容都不是来自data['caseName']

pd.DataFrame({'new_strings': new_strings, 'duplicates': matches})
                                  new_strings                                    duplicates
0  Ashwander v. Tennessee Valley Authority,.txt  Ashwander v. Tennessee Valley Authority,.txt
1                             Bell v. Hood,.txt                             Bell v. Hood,.txt
2    Charles River Bridge v. Warren Bridge,.txt    Charles River Bridge v. Warren Bridge,.txt

有人知道我做错了什么吗?

作为参考,new_strings 看起来像这样(我已将其限制为帖子的 3 个元素):

0    Ashwander v. Tennessee Valley Authority,.txt
1                               Bell v. Hood,.txt
2      Charles River Bridge v. Warren Bridge,.txt
Name: Cited, dtype: object

data['caseName'] 看起来像这样:

data['caseName']
0       HALLIBURTON OIL WELL CEMENTING CO. v. WALKER e...
1                              CLEVELAND v. UNITED STATES
2           CHAMPLIN REFINING CO. v. UNITED STATES ET AL.
3        UNITED STATES v. ALCEA BAND OF TILLAMOOKS ET AL.
4              UNITED STATES v. HOWARD P. FOLEY CO., INC.
                              ...                        
9025     DEPARTMENT OF HOMELAND SECURITY v. THURAISSIGIAM
9026    SEILA LAW LLC v. CONSUMER FINANCIAL PROTECTION...
9027            LIU v. SECURITIES AND EXCHANGE COMMISSION
9028                      COLORADO DEPT. OF STATE v. BACA
9029                             TRUMP v. MAZARS USA, LLP
Name: caseName, Length: 9030, dtype: object```

【问题讨论】:

  • 既然 FuzzyWuzzy 很慢,你应该试试github.com/maxbachmann/RapidFuzz(我是作者)。它使用相同的指标,但速度要快得多。
  • 谢谢! rapidfuzz 减少了大约 70% 的时间。这很有帮助。

标签: python pandas string fuzzywuzzy fuzzy


【解决方案1】:

我是string_grouper 的投稿人,很抱歉没有早点注意到您的问题。

match_most_similar 如果在默认值为 0.8 的相似度阈值(min_similarity)之上找不到匹配项,则返回原始字符串。所以你可以尝试降低它的值,看看你是否得到更合适的结果。例如,

matches = match_most_similar(data['caseName'],
   new_strings,
   min_similarity=0.6)

请记住,min_similarity 的最小值为 0,其最大值为 1。此外,通常,相似度阈值越低,match_most_similar 运行所需的时间就越长。

另请参阅https://github.com/Bergvca/string_grouper#kwargs,了解您可以调整以改善结果的其他选项列表。

【讨论】:

    猜你喜欢
    • 2023-04-05
    • 2011-03-05
    • 2020-06-29
    • 2020-02-08
    • 1970-01-01
    • 2013-11-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多