string_similarity
Dice 계수를 기반으로 두 문자열 간의 유사도를 찾습니다. 이는 보통 레벤슈타인 거리보다 더 좋습니다.
Dice 계수를 기반으로 두 문자열 간의 유사도를 찾습니다. 이는 보통 레벤슈타인 거리보다 더 좋습니다.
^1.11.0^1.16.7아래는 영문 원문 스냅샷입니다. 최신 내용은 GitHub에서 확인하세요.
A fast, dependency-free Dart package for measuring the similarity between two strings with the Sørensen–Dice coefficient.
Use it for fuzzy search, spelling suggestions, duplicate detection, and ranking the closest match from a list.
0.0 (completely different) to 1.0 (identical)dart pub add string_similarity
Then import the package:
import 'package:string_similarity/string_similarity.dart';
final score = 'healed'.similarityTo('sealed');
print(score); // 0.8
The equivalent static method is:
final score = StringSimilarity.compareTwoStrings('healed', 'sealed');
final result = 'healed'.bestMatch([
'edward',
'sealed',
'theatre',
]);
print(result.bestMatch.target); // sealed
print(result.bestMatch.rating); // 0.8
print(result.bestMatchIndex); // 1
Every candidate and its score remain available in result.ratings.
The equivalent static method is:
final result = StringSimilarity.findBestMatch(
'healed',
['edward', 'sealed', 'theatre'],
);
The package compares adjacent character pairs (bigrams) using the
Sørensen–Dice coefficient.
Whitespace is removed before comparison. Letter case and diacritics are kept,
so Cote, cote, and côte are different values.
Normalize both strings before comparing them:
final score = 'DART'.toLowerCase().similarityTo('dart'.toLowerCase());
print(score); // 1.0
| API | Returns | Description |
|---|---|---|
value.similarityTo(other) |
double |
Compares two nullable strings. |
value.bestMatch(targets) |
BestMatch |
Rates every nullable target and selects the best one. |
StringSimilarity.compareTwoStrings(first, second) |
double |
Static alternative to similarityTo. |
StringSimilarity.findBestMatch(value, targets) |
BestMatch |
Static alternative to bestMatch. |
BestMatch provides:
ratings: every target and its scorebestMatch: the highest-rated targetbestMatchIndex: the winning target's original index| Input | Result |
|---|---|
Two identical values, including null or '' |
1.0 |
Only one value is null or empty |
0.0 |
| Different values where either has fewer than two characters | 0.0 |
| Several targets share the highest score | The first one wins |
| The target list is empty | Throws ArgumentError |
Licensed under the permissive MIT License.