Search
 
SCRIPT & CODE EXAMPLE
 

PYTHON

levenshtein distance

def levenshtein(a, b):
    m = [[*range(len(a) + 1)] for _ in range(len(b) + 1)]
    for i in range(len(b) + 1):
        m[i][0] = i
    for i in range(1, len(b) + 1):
        for j in range(1, len(a) + 1):
            m[i][j] = min(m[i-1][j] + 1, m[i][j-1] + 1, m[i-1][j-1] + (b[i-1] != a[j-1]))
    return m[-1][-1]
Comment

levenshtein distance

#include <algorithm>
#include <vector>

template<typename T>
typename T::size_type LevenshteinDistance(const T &source, const T &target) {
    if (source.size() > target.size()) {
        return LevenshteinDistance(target, source);
    }

    using TSizeType = typename T::size_type;
    const TSizeType min_size = source.size(), max_size = target.size();
    std::vector<TSizeType> lev_dist(min_size + 1);

    for (TSizeType i = 0; i <= min_size; ++i) {
        lev_dist[i] = i;
    }

    for (TSizeType j = 1; j <= max_size; ++j) {
        TSizeType previous_diagonal = lev_dist[0], previous_diagonal_save;
        ++lev_dist[0];

        for (TSizeType i = 1; i <= min_size; ++i) {
            previous_diagonal_save = lev_dist[i];
            if (source[i - 1] == target[j - 1]) {
                lev_dist[i] = previous_diagonal;
            } else {
                lev_dist[i] = std::min(std::min(lev_dist[i - 1], lev_dist[i]), previous_diagonal) + 1;
            }
            previous_diagonal = previous_diagonal_save;
        }
    }

    return lev_dist[min_size];
}
Comment

PREVIOUS NEXT
Code Example
Python :: pandas dataframe filter 
Python :: initialise a 2d array python 
Python :: drop a list of index pandas 
Python :: django sessions 
Python :: dictionary indexing python 
Python :: python datetime format string 
Python :: add a button pyqt5 
Python :: numpy.ndarray to lsit 
Python :: python 3 custom sort with compare 
Python :: lerp function 
Python :: try python import 
Python :: turn a list into a string python 
Python :: python replace nth occurrence in string 
Python :: pandas drop if present 
Python :: how to make addition in python 
Python :: how to redirect to previous page in django 
Python :: pathlib remove extension 
Python :: python append filename to path 
Python :: read json file using python 
Python :: discord.py mention user 
Python :: matplotlib styles attr 
Python :: tuple plot python 
Python :: how to run django in jupyter 
Python :: python pandas column where 
Python :: how to sum all the numbers in a list in python 
Python :: build a pile of cubes python 
Python :: python3 shebang 
Python :: create columns in streamlit 
Python :: variable in python 
Python :: remove tab space from string in python 
ADD CONTENT
Topic
Content
Source link
Name
2+4 =