簡體   English   中英

如何優化算法以更快地找到帶有fuzzywuzzy的相似字符串?

[英]How to optimize an algorithm to find similar strings with fuzzywuzzy faster?

我在我的數據庫中找到相似的食物名稱時遇到問題(大約有 10 萬個產品名稱)。 我已經決定使用fuzz.token_sort_ratio從LIB fuzzywuzzy找到類似的產品名稱。 這是它的工作原理:

s1 = 'Pepsi Light'
s2 = 'Light Pepsi'
fuzz.token_sort_ratio(s1, s2)

100

現在我想找到所有具有相似詞的產品名稱,其結果為fuzz.token_sort_ratio >= 90 這是我的代碼:

#Find similar
start=datetime.now()
l = list(v_foods.name[0:20000])
i=0
df = pd.DataFrame(columns=['name1', 'name2', 'probab_same'])
for k in range(len(l)):
    for s in range(k+1,len(l)):
        probability = fuzz.token_sort_ratio(l[k], l[s])
        if  probability >= 90:
            df.loc[i] = [l[k], l[s], probability]
            i +=1
print('Spent time: {}' .format(datetime.now() - start))           
df.head(5)   

這需要很多時間。 我擁有的產品越多,花費的時間就越多

  1. l = list(v_foods.name[0:5000])花費時間:~3 分鍾
  2. l = list(v_foods.name[0:10000])花費時間:~13 分鍾
  3. l = list(v_foods.name[0:20000])花費時間:~53 分鍾

正如我上面所說,我的基地有 10 萬個名字,它的工作速度非常慢。 有什么方法可以優化我的算法嗎?

您的問題是您正在將每個名稱與其他名稱進行比較。 那是n^2比較,所以變慢了。 您需要做的只是比較有可能足夠相似的名稱對。

為了做得更好,我們需要知道圖書館實際上在做什么。 多虧了這個出色的答案,我們才能知道。 它在兩個名稱上調用fuzz._process_and_sort(name, True) ,然后查找 Levenshtein 比率。 也就是說,它計算從一個字符串到另一個字符串的最佳方式,然后計算100 * matched_chars / (matched_chars + edits) 為了使這個分數達到 90+,編輯次數最多為len(name) / 9 (該條件是必要但不充分的,如果這些編輯包括此字符串中的替換和刪除,則會降低匹配字符的數量並降低比率。)

所以你可以很容易地規范化所有的名字。 問題是,對於給定的規范化名稱,您能否找到所有其他規范化名稱的最大編輯次數?

訣竅是首先將所有規范化名稱放入Trie數據結構中。 然后我們可以並行遍歷 Trie 來探索特定編輯距離內的所有分支。 這允許刪除超出該距離的大組標准化名稱,而無需單獨檢查它們。

這是 Trie 的 Python 實現,可讓您找到這些標准化名稱對。

import re

# Now we will build a trie.  Every node has a list of words, and a dictionary
# from the next letter farther in the trie.
class Trie:
    def __init__(self, path=''):
        self.strings = []
        self.dict = {}
        self.count_strings = 0
        self.path = path

    def add_string (self, string):
        trie = self

        for letter in string:
            trie.count_strings += 1
            if letter not in trie.dict:
                trie.dict[letter] = Trie(trie.path + letter)
            trie = trie.dict[letter]
        trie.count_strings += 1
        trie.strings.append(string)

    def __hash__ (self):
        return id(self)

    def __repr__ (self):
        answer = self.path + ":\n  count_strings:" + str(self.count_strings) + "\n  strings: " + str(self.strings) + "\n  dict:"
        def indent (string):
            p = re.compile("^(?!:$)", re.M)
            return p.sub("    ", string)
        for letter in sorted(self.dict.keys()):
            subtrie = self.dict[letter]
            answer = answer + indent("\n" + subtrie.__repr__())
        return answer

    def within_edits(self, string, max_edits):
        # This will be all trie/string pos pairs that we have seen
        found = set()
        # This will be all trie/string pos pairs that we start the next edit with
        start_at_edit = set()

        # At distance 0 we start with the base of the trie can match the start of the string.
        start_at_edit.add((self, 0))
        answers = []
        for edits in range(max_edits + 1): # 0..max_edits inclusive
            start_at_next_edit = set()
            todo = list(start_at_edit)
            for trie, pos in todo:
                if (trie, pos) not in found: # Have we processed this?
                    found.add((trie, pos))
                    if pos == len(string):
                        answers.extend(trie.strings) # ANSWERS FOUND HERE!!!
                        # We have to delete from the other string
                        for next_trie in trie.dict.values():
                            start_at_next_edit.add((next_trie, pos))
                    else:
                        # This string could have an insertion
                        start_at_next_edit.add((trie, pos+1))
                        for letter, next_trie in trie.dict.items():
                            # We could have had a a deletion in this string
                            start_at_next_edit.add((next_trie, pos))
                            if letter == string[pos]:
                                todo.append((next_trie, pos+1)) # we matched farther
                            else:
                                # Could have been a substitution
                                start_at_next_edit.add((next_trie, pos+1))
            start_at_edit = start_at_next_edit
        return answers

# Sample useage
trie = Trie()
trie.add_string('foo')
trie.add_string('bar')
trie.add_string('baz')
print(trie.within_edits('ba', 1))

正如其他人指出的那樣 FuzzyWuzzy 使用 Levenshtein 距離,即 O(N^2)。 但是,在您的代碼中,有很多可以優化以大大改善運行時的內容。 這不會像 btilly 的 trie 實現一樣快,但您將保持類似的行為(例如預先對單詞進行排序)

  1. 使用RapidFuzz而不是 FuzzyWuzzy(我是作者)。 它實現了相同的算法,但速度要快得多。

  2. 您當前在每次調用 fuzz.token_sort_ratio 時預處理字符串,這可以預先完成一次。

  3. 您可以將 score_cutoff 傳遞給 Rapidfuzz,因此當它知道無法達到分數時,它可以以 0 的分數提前退出。

以下實現在我的機器上大約需要 47 秒,而您當前的實現運行大約 7 分鍾。

from rapidfuzz import fuzz, utils
import random
import string
from datetime import datetime
import pandas as pd

random.seed(18)

l = [''.join(random.choice(string.ascii_letters + string.digits + string.whitespace)
       for _ in range(random.randint(10, 20))
    )
    for s in range(10000)
]

start=datetime.now()
processed=[utils.default_process(name) for name in l]
i=0
res = []

for k in range(len(l)):
    for s in range(k+1,len(l)):
        probability = fuzz.token_sort_ratio(
            processed[k], processed[s], processor=False, score_cutoff=90)
        if  probability:
            res.append([l[k], l[s], probability])
            i +=1

df = pd.DataFrame(res, columns=['name1', 'name2', 'probab_same'])

print('Spent time: {}' .format(datetime.now() - start))           
print(df.head(5))

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM