2

私はSequenceMatcherこのタスクに difflib ( ) を使用しています: タイプミスのある 3000 冊の本のタイトルについて、128500 冊のタイトルを含むデータベースで最も近い一致を見つけます。コードは簡単です:

# imp and dbf are lists of ordered dicts 
# imp contains the messy titles to match (in imp['Titel:'] in each dict)
# dbf contains the clean titles to match against (in dbf['ti']) 

threshold = 0.65

for imprec in imp:
    bestmatch = threshold
    mpair = []

    try: 
        i = imprec['Titel:'][0]
    except KeyError: 
        print('record with InvNo %s has no title' % imprec['InvNo:'])
        continue

    for rec in dbf: 
        try: 
            r = rec['ti'][0]
        except KeyError:
            # record has no title. Do not make screen output.. 
            # print('record with priref %s has no title' % rec['%0'])
            continue

        m = SequenceMatcher(None, i, r)
        if m.ratio() > bestmatch: 
            bestmatch = m.ratio()
            mpair = [bestmatch, imprec, rec]
            print('{} matches for {:4.1f}% with {}'.format(i, m.ratio()*100, r))

    if bestmatch > threshold:
        match.append(mpair)
    else:
        nonmatch.append(imprec)

動作しますが、遅いです。約 100 時間後、3000 のリストの約 40% になります。問題はもちろん、128500 タイトルの 3000 回の反復 = 3 億 8550 万回の呼び出しSequenceMatcherです。

これを最適化する方法を探しています。この投稿では、OP はインデックスを作成し、それを照会し、SequenceMatched はそのクエリの歩留まりを一致させました。それは良いアプローチだと思いますが、それをどのように実装しますか?

スクリプトは 1 回限りのものであり、派手なアプリなどではありません。私の時間の予算は限られています。

edit Whoosh はあいまい用語クエリをサポートしています。SQLite にはLIKE.

私が調べるべき他の可能性はありますか?

4

0 に答える 0