python从文件中打印最大行模糊比

时间:2014-07-07 14:23:52

标签: python

我正在尝试一个python代码,找到文本文件中所有行的最大模糊比率,用“' good'”,将打印出具有最大模糊比率的行。我试过了程序:

from fuzzywuzzy import fuzz
from collections import defaultdict
with open(qwer.txt, 'r') as my_file:
     for line in my_file:
        for part in line.split():
          a=line
          b='good'
          fuzziness = fuzz.ratio(a,b)
          filenames2fuzz[line].append(fuzziness)

    best_fuzziness_ratio = 0 
    for k, v in filenames2fuzz.items():
        if max(v) > best_fuzziness_ratio:
            best_fuzzy_line = k
            best_fuzziness_line_ratio = max(v)

    print(best_fuzzy_line)

实施例: qwer.txt有1000多行

.................
I love God
he is a goody boy
.............

输出:

he is a goody boy

请帮助我获取输出

1 个答案:

答案 0 :(得分:2)

一个简短的解决方案可能就是这种形式:

def good_ratio(a):
    return fuzz.ratio(a, 'good')

with open('qwer.txt', 'r') as my_file:
    print(max(my_file, key=good_ratio))

甚至更短

with open('qwer.txt', 'r') as my_file:
    print(max(my_file, key=lambda a: fuzz.ratio(a, 'good')))