Skip to main content
 首页 » 编程设计

python之在我的 Fuzzywuzzy 字符串匹配项目中包含分数截止值,以仅包含高于分数 x 的匹配项

2024年12月31日28mq0036

我一直在回收来自各地的一堆代码,为我拥有的两个 csv 文件创建一个字符串匹配器。

我的代码现在的输出是每个字符串的 3 个最高匹配项。 我想另外包括低于特定匹配分数的截止值。 这样只会显示高于该分数的匹配。我认为这很容易,因为根据文档我可以将 score_cutoff 参数包含到 process.extract 函数中,但出于某种原因,这不是可接受的输入。

这是我现在的代码:

from fuzzywuzzy import process 
import pandas as pd 
import os 
 
 
 
def StringMatch (master, testfile, num_match: object = 3): 
    master_names = master.iloc[:,3] 
    test_names = testfile.iloc[:,0]     
    fhp_new = [process.extract(x, master_names, limit=limit) for x in test_names] 
    lab=" " 
    i=1 
    while i<=num_match: 
        lab = lab + " " + "Match" + str(i) 
        i = i+1 
    aggregated_matches = pd.DataFrame(fhp_new, columns = lab.split()) 
    d={} 
    for x in range (1, num_match + 1): 
        d["Match{0}".format(x)] = [y[0] for y in aggregated_matches["Match" + str(x)]] 
    d["test_original"] = test_names 
    d["perfect match"] = d["Match1"] == d["test_original"] 
    out = pd.DataFrame(data=d) 
    out.to_csv(str(outFile + ".csv")) 
    return (out) 
    print ("finished") 
 
master = pd.read_csv("MasterVendorDevice.csv") 
testfile = pd.read_csv("testfile.csv", encoding='latin-1') 
limit=3 
 
baseDir = os.path.join("/Users", "Tim", "Desktop", "String Matcher") 
outDir = os.path.join(baseDir, "out") 
 
if not os.path.exists(outDir): 
    os.makedirs(outDir) 
 
outFile = os.path.join(outDir, "matches") 
 
StringMatch(master, testfile) 

请您参考如下方法:

我没有将“score_cutoff”视为 process.extract() 中的参数,但我认为您可以这样做

process.extractBests(x,master_names,limit=limit,score_cutoff=cutoff)