Skip to content

Commit 43057b2

Browse files
committed
Update example
1 parent f37e391 commit 43057b2

File tree

1 file changed

+17
-7
lines changed

1 file changed

+17
-7
lines changed

README.md

Lines changed: 17 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -144,15 +144,25 @@ It can also be used for keyboard typing auto-correction. Here the cost of substi
144144

145145
```python
146146
from strsimpy.weighted_levenshtein import WeightedLevenshtein
147-
from strsimpy.weighted_levenshtein import CharacterSubstitutionInterface
148147

149-
class CharacterSubstitution(CharacterSubstitutionInterface):
150-
def cost(self, c0, c1):
151-
if c0=='t' and c1=='r':
152-
return 0.5
153-
return 1.0
154148

155-
weighted_levenshtein = WeightedLevenshtein(CharacterSubstitution())
149+
def insertion_cost(char):
150+
return 1.0
151+
152+
153+
def deletion_cost(char):
154+
return 1.0
155+
156+
157+
def substitution_cost(char_a, char_b):
158+
if char_a == 't' and char_b == 'r':
159+
return 0.5
160+
return 1.0
161+
162+
weighted_levenshtein = WeightedLevenshtein(
163+
substitution_cost_fn=substitution_cost,
164+
insertion_cost_fn=insertion_cost,
165+
deletion_cost_fn=deletion_cost)
156166
print(weighted_levenshtein.distance('String1', 'String2'))
157167

158168
```

0 commit comments

Comments
 (0)