Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(436)

Side by Side Diff: pkg/analysis_services/lib/src/correction/levenshtein.dart

Issue 414273002: New 'use similar' fixes. (Closed) Base URL: https://dart.googlecode.com/svn/branches/bleeding_edge/dart
Patch Set: Created 6 years, 4 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
(Empty)
1 library levenshtein;
2
3 import 'dart:math';
4
5 /// Levenshtein algorithm implementation based on:
6 /// http://en.wikipedia.org/wiki/Levenshtein_distance#Iterative_with_two_matrix_ rows
7 ///
8 /// Implementation: https://github.com/conradkleinespel/levenshtein-dart
9 int getLevenshteinDistance(String s, String t, {bool caseSensitive: true}) {
10 if (!caseSensitive) {
11 s = s.toLowerCase();
12 t = t.toLowerCase();
13 }
14
15 if (s == t) {
16 return 0;
17 }
18 if (s.length == 0) {
19 return t.length;
20 }
21 if (t.length == 0) {
22 return s.length;
23 }
24
25 List<int> v0 = new List<int>.filled(t.length + 1, 0);
26 List<int> v1 = new List<int>.filled(t.length + 1, 0);
27
28 for (int i = 0; i < t.length + 1; i < i++) {
29 v0[i] = i;
30 }
31
32 for (int i = 0; i < s.length; i++) {
33 v1[0] = i + 1;
34
35 for (int j = 0; j < t.length; j++) {
36 int cost = (s[i] == t[j]) ? 0 : 1;
37 v1[j + 1] = min(v1[j] + 1, min(v0[j + 1] + 1, v0[j] + cost));
38 }
39
40 for (int j = 0; j < t.length + 1; j++) {
41 v0[j] = v1[j];
42 }
43 }
44
45 return v1[t.length];
46 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698