Алгоритм поиска для переполненной базы данных Django

Я ищу алгоритм поиска для быстрого поиска. В настоящее время я использую алгоритм levenshtein_ratio_and_distance для поиска. Но поиск все равно занимает много времени. Вот мой код:

def levenshtein_ratio_and_distance(s, t, ratio_calc = False):
# Initialize matrix of zeros
rows = len(s)+1
cols = len(t)+1
distance = np.zeros((rows,cols),dtype = int)

# Populate matrix of zeros with the indeces of each character of both strings
for i in range(1, rows):
    for k in range(1,cols):
        distance[i][0] = i
        distance[0][k] = k

# Iterate over the matrix to compute the cost of deletions,insertions and/or substitutions    
for col in range(1, cols):
    for row in range(1, rows):
        if s[row-1] == t[col-1]:
            cost = 0 # If the characters are the same in the two strings in a given position [i,j] then the cost is 0
        else:
            # In order to align the results with those of the Python Levenshtein package, if we choose to calculate the ratio
            # the cost of a substitution is 2. If we calculate just distance, then the cost of a substitution is 1.
            if ratio_calc == True:
                cost = 2
            else:
                cost = 1
        distance[row][col] = min(distance[row-1][col] + 1,      # Cost of deletions
                             distance[row][col-1] + 1,          # Cost of insertions
                             distance[row-1][col-1] + cost)     # Cost of substitutions
if ratio_calc == True:
    # Computation of the Levenshtein Distance Ratio
    Ratio = ((len(s)+len(t)) - distance[row][col]) / (len(s)+len(t))
    return Ratio
else:
    # print(distance) # Uncomment if you want to see the matrix showing how the algorithm computes the cost of deletions,
    # insertions and/or substitutions
    # This is the minimum number of edits needed to convert string a to string b
    return 0

Можете ли вы порекомендовать какой-либо другой алгоритм поиска для уменьшения сложности. Здесь вы можете продемонстрировать StarofGeeks

Вернуться на верх