词条 | Levenshtein distance | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
释义 |
In information theory, linguistics and computer science, the Levenshtein distance is a string metric for measuring the difference between two sequences. Informally, the Levenshtein distance between two words is the minimum number of single-character edits (insertions, deletions or substitutions) required to change one word into the other. It is named after the Soviet mathematician Vladimir Levenshtein, who considered this distance in 1965.[1] Levenshtein distance may also be referred to as edit distance, although that term may also denote a larger family of distance metrics.[2]{{rp|32}} It is closely related to pairwise string alignments. DefinitionMathematically, the Levenshtein distance between two strings (of length and respectively) is given by where where is the indicator function equal to 0 when and equal to 1 otherwise, and is the distance between the first characters of and the first characters of . Note that the first element in the minimum corresponds to deletion (from to ), the second to insertion and the third to match or mismatch, depending on whether the respective symbols are the same. ExampleFor example, the Levenshtein distance between "kitten" and "sitting" is 3, since the following three edits change one into the other, and there is no way to do it with fewer than three edits:
Upper and lower boundsThe Levenshtein distance has several simple upper and lower bounds. These include:
An example where the Levenshtein distance between two strings of the same length is strictly less than the Hamming distance is given by the pair "flaw" and "lawn". Here the Levenshtein distance equals 2 (delete "f" from the front; insert "n" at the end). The Hamming distance is 4. ApplicationsIn approximate string matching, the objective is to find matches for short strings in many longer texts, in situations where a small number of differences is to be expected. The short strings could come from a dictionary, for instance. Here, one of the strings is typically short, while the other is arbitrarily long. This has a wide range of applications, for instance, spell checkers, correction systems for optical character recognition, and software to assist natural language translation based on translation memory. The Levenshtein distance can also be computed between two longer strings, but the cost to compute it, which is roughly proportional to the product of the two string lengths, makes this impractical. Thus, when used to aid in fuzzy string searching in applications such as record linkage, the compared strings are usually short to help improve speed of comparisons.{{citation needed|date=January 2019}} In linguistics, the Levenshtein distance is used as a metric to quantify the linguistic distance, or how different two languages are from one another.[3] It is related to mutual intelligibility, the higher the linguistic distance, the lower the mutual intelligibility, and the lower the linguistic distance, the higher the mutual intelligibility. Relationship with other edit distance metrics{{main|Edit distance}}There are other popular measures of edit distance, which are calculated using a different set of allowable edit operations. For instance,
Edit distance is usually defined as a parameterizable metric calculated with a specific set of allowed edit operations, and each operation is assigned a cost (possibly infinite). This is further generalized by DNA sequence alignment algorithms such as the Smith–Waterman algorithm, which make an operation's cost depend on where it is applied. Computing Levenshtein distanceRecursiveThis is a straightforward, but inefficient, recursive C implementation of a // len_s and len_t are the number of characters in string s and t respectively int LevenshteinDistance(const char *s, int len_s, const char *t, int len_t) } This implementation is very inefficient because it recomputes the Levenshtein distance of the same substrings many times. A more efficient method would never repeat the same distance calculation. For example, the Levenshtein distance of all possible prefixes might be stored in an array {{code|lang=C|code=d[][]}} where {{code|lang=C|code=d[i][j]}} is the distance between the first Iterative with full matrix{{main|Wagner–Fischer algorithm}}{{small|Note: This section uses 1-based strings instead of 0-based strings}} Computing the Levenshtein distance is based on the observation that if we reserve a matrix to hold the Levenshtein distances between all prefixes of the first string and all prefixes of the second, then we can compute the values in the matrix in a dynamic programming fashion, and thus find the distance between the two full strings as the last value computed. This algorithm, an example of bottom-up dynamic programming, is discussed, with variants, in the 1974 article The String-to-string correction problem by Robert A. Wagner and Michael J. Fischer.[4] This is a straightforward pseudocode implementation for a function function LevenshteinDistance(char s[1..m], char t[1..n]): Two examples of the resulting matrix (hovering over a tagged number reveals the operation performed to get that number):
The invariant maintained throughout the algorithm is that we can transform the initial segment {{code|lang=C|code=s[1..i]}} into {{code|lang=C|code=t[1..j]}} using a minimum of {{code|lang=C|code=d[i,j]}} operations. At the end, the bottom-right element of the array contains the answer. Iterative with two matrix rowsIt turns out that only two rows of the table are needed for the construction if one does not want to reconstruct the edited input strings (the previous row and the current row being calculated). The Levenshtein distance may be calculated iteratively using the following algorithm:[5] function LevenshteinDistance(char s[1..m], char t[1..n]): // create two work vectors of integer distances declare int v0[n + 1] declare int v1[n + 1] // initialize v0 (the previous row of distances) // this row is A[0][i]: edit distance for an empty s // the distance is just the number of characters to delete from t for i from 0 to n: v0[i] = i for i from 0 to m-1: // calculate v1 (current row distances) from the previous row v0 // first element of v1 is A[i+1][0] // edit distance is delete (i+1) chars from s to match empty t v1[0] = i + 1 // use formula to fill in the rest of the row for j from 0 to n-1: // calculating costs for A[i+1][j+1] deletionCost := v0[j + 1] + 1 insertionCost := v1[j] + 1 if s[i] = t[j]: substitutionCost := v0[j] else: substitutionCost := v0[j] + 1 // copy v1 (current row) to v0 (previous row) for next iteration swap v0 with v1 // after the last swap, the results of v1 are now in v0 return v0[n] (The above is also true for two-columns, as the Levenshtein matrix can be imagined flipped across a diagonal vertex and get the same results.) This two row variant is suboptimal—the amount of memory required may be reduced to one row and one word of overhead.[6] And similarly if, a Levenshtein distance algorithm exists for one row then the same can be achieved with one column. A one column algorithm would lend itself to SQL and other database languages. Hirschberg's algorithm combines this method with divide and conquer. It can compute the optimal edit sequence, and not just the edit distance, in the same asymptotic time and space bounds.[7]Word PreparationIt can be shown that the Levenshtein Distance between "Saturday" and "Sunday" is the same as between "atur" and "un" (Saturday and Sunday). Use of a preparation algorithm that shortens the words to compare by removing similar characters from the beginning of each word and by removing similar characters from the end of each word can reduce the computing time for the Levenshtein distance calculation. If after preparation one of the strings is empty then the Levenshtein distance is the length of the other string. "Kit" and "Kitten" would become "" and "ten", so L = 3. Obviously, this is best used when it's known in advance that strings have a high probabilty of being similar. Adaptive variantThe dynamic variant is not the ideal implementation. An adaptive approach may reduce the amount of memory required and, in the best case, may reduce the time complexity to linear in the length of the shortest string, and, in the worst case, no more than quadratic in the length of the shortest string. [8] ApproximationThe Levenshtein distance between two strings of length {{mvar|n}} can be approximated to within a factor where {{math|ε > 0}} is a free parameter to be tuned, in time {{math|O(n1 + ε)}}.[9] Computational complexityIt has been shown that the Levenshtein distance of two strings of length {{mvar|n}} cannot be computed in time {{math|O(n2 - ε)}} for any ε greater than zero unless the strong exponential time hypothesis is false.[10] See also{{div col|colwidth=25em}}
References1. ^{{cite journal |author=Влади́мир И. Левенштейн |script-title=ru:Двоичные коды с исправлением выпадений, вставок и замещений символов |language=Russian |trans-title=Binary codes capable of correcting deletions, insertions, and reversals |journal=Доклады Академий Наук СCCP |volume=163 |issue=4 |pages=845–8 |year=1965}} Appeared in English as: {{cite journal |author=Levenshtein, Vladimir I. |title=Binary codes capable of correcting deletions, insertions, and reversals |journal=Soviet Physics Doklady |volume=10 |number=8 |pages=707–710 |date=February 1966 |url=|bibcode=1966SPhD...10..707L }} 2. ^{{Cite journal |last1=Navarro |first1=Gonzalo |doi=10.1145/375360.375365 |title=A guided tour to approximate string matching |journal=ACM Computing Surveys |volume=33 |issue=1 |pages=31–88 |year=2001 |url=http://repositorio.uchile.cl/bitstream/handle/2250/126168/Navarro_Gonzalo_Guided_tour.pdf|citeseerx=10.1.1.452.6317 }} 3. ^{{Citation | title=Receptive multilingualism: linguistic analyses, language policies, and didactic concepts |author1=Jan D. ten Thije |author2=Ludger Zeevaert | publisher=John Benjamins Publishing Company, 2007 | isbn=978-90-272-1926-8 | url=https://books.google.com/books?id=8gIEN068J3gC&q=Levenshtein#v=snippet&q=Levenshtein&f=false | quote=... Assuming that intelligibility is inversely related to linguistic distance ... the content words the percentage of cognates (related directly or via a synonym) ... lexical relatedness ... grammatical relatedness ...|date=2007-01-01 }} 4. ^{{citation |first=Robert A. |last=Wagner |first2=Michael J. |last2=Fischer |author2-link=Michael J. Fischer |title=The String-to-String Correction Problem |journal=Journal of the ACM |volume=21 |issue=1 |year=1974 |pages=168–173 |doi= 10.1145/321796.321811}} 5. ^{{Citation |title=Fast, memory efficient Levenshtein algorithm |first=Sten |last=Hjelmqvist |date=26 Mar 2012 |url=http://www.codeproject.com/Articles/13525/Fast-memory-efficient-Levenshtein-algorithm}} 6. ^{{cite web | url=https://bitbucket.org/clearer/iosifovich/ | title=Clearer / Iosifovich}} 7. ^{{cite journal|last=Hirschberg|first=D. S.|authorlink=Dan Hirschberg|title=A linear space algorithm for computing maximal common subsequences|journal=Communications of the ACM|volume=18|issue=6|year=1975|pages=341–343|doi=10.1145/360825.360861|mr=0375829|url=http://www.ics.uci.edu/~dan/pubs/p341-hirschberg.pdf|type=Submitted manuscript|bibcode=1985CACM...28...22S|citeseerx=10.1.1.348.4774}} 8. ^{{cite web | url=https://bitbucket.org/clearer/iosifovich/ | title=Clearer / Iosifovich}} 9. ^{{cite conference |last1=Andoni |first1=Alexandr |first2=Robert |last2=Krauthgamer |first3=Krzysztof |last3=Onak |title=Polylogarithmic approximation for edit distance and the asymmetric query complexity |conference=IEEE Symp. Foundations of Computer Science (FOCS) |year=2010 |citeseerx=10.1.1.208.2079 |arxiv=1005.4033|bibcode=2010arXiv1005.4033A }} 10. ^{{cite conference |last1=Backurs |first1=Arturs |first2=Piotr |last2=Indyk |title= Edit Distance Cannot Be Computed in Strongly Subquadratic Time (unless SETH is false) |conference=Forty-Seventh Annual ACM on Symposium on Theory of Computing (STOC) |year=2015 | arxiv=1412.0348|bibcode=2014arXiv1412.0348B }} External links{{Wikibooks| Algorithm implementation|Strings/Levenshtein distance|Levenshtein distance}}
5 : String similarity measures|Dynamic programming|Articles with example pseudocode|Quantitative linguistics|Similarity and distance measures |
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
随便看 |
|
开放百科全书收录14589846条英语、德语、日语等多语种百科知识,基本涵盖了大多数领域的百科知识,是一部内容自由、开放的电子版国际百科全书。