Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up

Compute distance between sequences. 30+ algorithms, pure python implementation, common interface.

License

NotificationsYou must be signed in to change notification settings

awesome-archive/textdistance

 
 

Repository files navigation

TextDistance logo

Build StatusPyPI versionStatusCode sizeLicense

TextDistance -- python library for compare distance between two or more sequences by many algorithms.

Features:

  • 30+ algorithms
  • Pure python implementation
  • Simple usage
  • More than two sequences comparing
  • Some algorithms have more than one implementation in one class.
  • Optional numpy usage for maximum speed.

Algorithms

Edit based

AlgorithmClassFunctions
HammingHamminghamming
MLIPNSMlipnsmlipns
LevenshteinLevenshteinlevenshtein
Damerau-LevenshteinDamerauLevenshteindamerau_levenshtein
Jaro-WinklerJaroWinklerjaro_winkler,jaro
Strcmp95StrCmp95strcmp95
Needleman-WunschNeedlemanWunschneedleman_wunsch
GotohGotohgotoh
Smith-WatermanSmithWatermansmith_waterman

Token based

AlgorithmClassFunctions
Jaccard indexJaccardjaccard
Sørensen–Dice coefficientSorensensorensen,sorensen_dice,dice
Tversky indexTverskytversky
Overlap coefficientOverlapoverlap
Tanimoto distanceTanimototanimoto
Cosine similarityCosinecosine
Monge-ElkanMongeElkanmonge_elkan
Bag distanceBagbag

Sequence based

AlgorithmClassFunctions
longest common subsequence similarityLCSSeqlcsseq
longest common substring similarityLCSStrlcsstr
Ratcliff-Obershelp similarityRatcliffObershelpratcliff_obershelp

Compression based

Work in progress. Now all algorithms compare two strings as array of bits, not by chars.

NCD - normalized compression distance.

Functions:

  1. bz2_ncd
  2. lzma_ncd
  3. arith_ncd
  4. rle_ncd
  5. bwtrle_ncd
  6. zlib_ncd

Phonetic

AlgorithmClassFunctions
MRAMRAmra
EditexEditexeditex

Simple

AlgorithmClassFunctions
Prefix similarityPrefixprefix
Postfix similarityPostfixpostfix
Length distanceLengthlength
Identity similarityIdentityidentity
Matrix similarityMatrixmatrix

Installation

Stable:

pip install textdistance

Dev:

pip install -e git+https://github.com/orsinium/textdistance.git#egg=textdistance

Usage

All algorithms have 2 interfaces:

  1. Class with algorithm-specific params for customizing.
  2. Class instance with default params for quick and simple usage.

All algorithms have some common methods:

  1. .distance(*sequences) -- calculate distance between sequences.
  2. .similarity(*sequences) -- calculate similarity for sequences.
  3. .maximum(*sequences) -- maximum possible value for distance and similarity. For any sequence:distance + similarity == maximum.
  4. .normalized_distance(*sequences) -- normalized distance between sequences. The return value is a float between 0 and 1, where 0 means equal, and 1 totally different.
  5. .normalized_similarity(*sequences) -- normalized similarity for sequences. The return value is a float between 0 and 1, where 0 means totally different, and 1 equal.

Most common init arguments:

  1. qval -- q-value for split sequences into q-grams. Possible values:
    • 1 (default) -- compare sequences by chars.
    • 2 or more -- transform sequences to q-grams.
    • None -- split sequences by words.
  2. as_set -- for token-based algorithms:
    • True --t andttt is equal.
    • False (default) --t andttt is different.

Example

For example,Hamming distance:

importtextdistancetextdistance.hamming('test','text')# 1textdistance.hamming.distance('test','text')# 1textdistance.hamming.similarity('test','text')# 3textdistance.hamming.normalized_distance('test','text')# 0.25textdistance.hamming.normalized_similarity('test','text')# 0.75textdistance.Hamming(qval=2).distance('test','text')# 2

Any other algorithms have same interface.

About

Compute distance between sequences. 30+ algorithms, pure python implementation, common interface.

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages

  • Python99.9%
  • Shell0.1%

[8]ページ先頭

©2009-2025 Movatter.jp