File: functions.md

package info (click to toggle)
python-jellyfish 1.2.0-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 1,052 kB
  • sloc: python: 580; makefile: 39; sh: 3
file content (173 lines) | stat: -rw-r--r-- 6,601 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
# Functions

Jellyfish provides a variety of functions for string comparison, phonetic encoding, and stemming.

## String Comparison

These methods are all measures of the difference (aka edit distance) between two strings.

### Levenshtein Distance

``` python
def levenshtein_distance(s1: str, s2: str)
```

Compute the Levenshtein distance between s1 and s2.

Levenshtein distance represents the number of insertions, deletions, and substitutions required to change one word to another.

For example: ``levenshtein_distance('berne', 'born') == 2`` representing the transformation of the first e to o and the deletion of the second e.

See the [Levenshtein distance article at Wikipedia](http://en.wikipedia.org/wiki/Levenshtein_distance) for more details.

### Damerau-Levenshtein Distance

``` python
def damerau_levenshtein_distance(s1: str, s2: str)
```

Compute the Damerau-Levenshtein distance between s1 and s2.

A modification of Levenshtein distance, Damerau-Levenshtein distance counts transpositions (such as ifsh for fish) as a single edit.

Where ``levenshtein_distance('fish', 'ifsh') == 2`` as it would require a deletion and an insertion,
though ``damerau_levenshtein_distance('fish', 'ifsh') == 1`` as this counts as a transposition.

See the [Damerau-Levenshtein distance article at Wikipedia](http://en.wikipedia.org/wiki/Damerau-Levenshtein_distance) for more details.

### Hamming Distance

``` python
def hamming_distance(s1: str, s2: str)
```

Compute the Hamming distance between s1 and s2.

Hamming distance is the measure of the number of characters that differ between two strings.

Typically Hamming distance is undefined when strings are of different length, but this implementation
considers extra characters as differing.  For example ``hamming_distance('abc', 'abcd') == 1``.

See the [Hamming distance article at Wikipedia](http://en.wikipedia.org/wiki/Hamming_distance) for more details.

### Jaccard Similarity

``` python
def jaccard_similarity(s1: str, s2: str, ngram_size: Optional[int] = None) -> float
```

Compute the Jaccard index between s1 and s2.

The Jaccard index between two sets is defined as the number of elements of the intersection divided by the number of elements of the union of the two sets. The elements of the sets are ngrams (the substrings of length `ngram_size`) or words if `ngram_size` is `None`. The strings are split by whitespace.

The Jaccard index does not consider order of words/ngrams. Hence "hello world" and "world hello" have a Jaccard similarity of 1.

### Jaro Similarity

``` python
def jaro_similarity(s1: str, s2: str)
```

Compute the Jaro similarity between s1 and s2.

Jaro distance is a string-edit distance that gives a floating point response in [0,1] where 0 represents two completely dissimilar strings and 1 represents identical strings.

!!! warning

    Prior to 0.8.1 this function was named jaro_distance.  It was removed in 1.0.

### Jaro-Winkler Similarity

``` python
def jaro_winkler_similarity(s1: str, s2: str)
```

Compute the Jaro-Winkler similarity between s1 and s2.

Jaro-Winkler is a modification/improvement to Jaro distance, like Jaro it gives a floating point response in [0,1] where 0 represents two completely dissimilar strings and 1 represents identical strings.

!!! warning

    Prior to 0.8.1 this function was named jaro_winkler.  That name is still available, but is no longer recommended.
    It will be replaced in 1.0 with a correct version.

See the [Jaro-Winkler distance article at Wikipedia](http://en.wikipedia.org/wiki/Jaro-Winkler_distance) for more details.

### Match Rating Approach (comparison)

``` python
def match_rating_comparison(s1, s2)
```

Compare s1 and s2 using the match rating approach algorithm, returns ``True`` if strings are considered equivalent or ``False`` if not.  Can also return ``None`` if s1 and s2 are not comparable (length differs by more than 3).

The Match rating approach algorithm is an algorithm for determining whether or not two names are
pronounced similarly.  Strings are first encoded using :py:func:`match_rating_codex` then compared according to the MRA algorithm.

See the [Match Rating Approach article at Wikipedia](http://en.wikipedia.org/wiki/Match_rating_approach) for more details.

## Phonetic Encoding

These algorithms convert a string to a normalized phonetic encoding, converting a word to a representation of its pronunciation.  Each takes a single string and returns a coded representation.


### American Soundex

``` python
def soundex(s: str)
```

Calculate the American Soundex of the string s.

Soundex is an algorithm to convert a word (typically a name) to a four digit code in the form 
'A123' where 'A' is the first letter of the name and the digits represent similar sounds.

For example ``soundex('Ann') == soundex('Anne') == 'A500'`` and
``soundex('Rupert') == soundex('Robert') == 'R163'``.

See the [Soundex article at Wikipedia](http://en.wikipedia.org/wiki/Soundex) for more details.


### Metaphone

``` python
def metaphone(s: str)
```

Calculate the metaphone code for the string s.

The metaphone algorithm was designed as an improvement on Soundex.  It transforms a word into a
string consisting of '0BFHJKLMNPRSTWXY' where '0' is pronounced 'th' and 'X' is a '[sc]h' sound.

For example ``metaphone('Klumpz') == metaphone('Clumps') == 'KLMPS'``.

See the [Metaphone article at Wikipedia](http://en.wikipedia.org/wiki/Metaphone) for more details.


### NYSIIS

``` python
def nysiis(s: str)
```

Calculate the NYSIIS code for the string s.

The NYSIIS algorithm is an algorithm developed by the New York State Identification and Intelligence System.  It transforms a word into a phonetic code.  Like soundex and metaphone it is primarily intended for use on names (as they would be pronounced in English).

For example ``nysiis('John') == nysiis('Jan') == JAN``.

See the [NYSIIS article at Wikipedia](http://en.wikipedia.org/wiki/New_York_State_Identification_and_Intelligence_System) for more details.

### Match Rating Approach (codex)

``` python
def match_rating_codex(s: str)
```

Calculate the match rating approach value (also called PNI) for the string s.

The Match rating approach algorithm is an algorithm for determining whether or not two names are
pronounced similarly.  The algorithm consists of an encoding function (similar to soundex or nysiis)
which is implemented here as well as :py:func:`match_rating_comparison` which does the actual comparison.

See the [Match Rating Approach article at Wikipedia](http://en.wikipedia.org/wiki/Match_rating_approach) for more details.