Skip to content
Permalink
Browse files
Use native levenshtein distance
  • Loading branch information
YoannQDQ authored and nyalldawson committed Mar 17, 2023
1 parent 5040a3b commit d2d7c5b
Showing 1 changed file with 2 additions and 10 deletions.
@@ -30,7 +30,7 @@
from operator import itemgetter
from pathlib import Path

from qgis.core import Qgis, QgsApplication, QgsBlockingNetworkRequest, QgsFileUtils, QgsSettings
from qgis.core import Qgis, QgsApplication, QgsBlockingNetworkRequest, QgsStringUtils, QgsSettings
from qgis.gui import QgsCodeEditorPython, QgsMessageBar
from qgis.PyQt.Qsci import QsciScintilla
from qgis.PyQt.QtCore import QByteArray, QCoreApplication, QDir, QEvent, QFileInfo, QJsonDocument, QSize, Qt, QUrl
@@ -59,15 +59,7 @@ def findMinimalDistanceIndex(source, target):
""" Find the source substring index that most closely matches the target string"""
index = min(len(source), len(target))

# Fallback to difflib if Levenshtein is not available
# Levenshtein is faster than difflib
try:
from Levenshtein import distance
except ImportError:
from difflib import SequenceMatcher

def distance(s, t):
return 1 - SequenceMatcher(None, s, t).ratio()
distance = QgsStringUtils.levenshteinDistance

d0 = distance(source[:index], target)
if d0 == 0:

0 comments on commit d2d7c5b

Please sign in to comment.