搜索插件、代码仓与 APP 分类

插件详情v2.2.0

string_similarity

根据Dice系数,查找两个字符串之间的相似度,该方法通常比Levenshtein距离更优。

AndroidiOSWebmacOSWindowsLinux
近 30 天下载
8万
喜欢数
129
Pub 分数
160

安装插件

flutter pub add string_similarity

插件介绍

基于插件公开信息、社区数据与项目使用情况整理的选型参考。

string_similarity 包使用 Dice 系数计算两个字符串之间的相似度,这种方法通常比 Levenshtein 距离更准确且更快。它非常适合用于模糊匹配、拼写纠正和文本数据中的重复检测任务。该算法通过评估字符串之间的共享双字符组来生成一个从 0(无匹配)到 1(完全匹配)的相似度评分。这个轻量级工具支持所有主要平台,包括 Android、iOS、Web、macOS、Windows 和 Linux,适用于需要高效字符串比较的跨平台应用程序。

适合场景

  • 用户输入中的模糊搜索
  • 检测近似重复的文本条目
  • 自动更正建议
  • 数据去重
  • 跨来源匹配产品名称
  • 拼写纠正

主要特点

  • 使用 Dice 系数进行准确度计算
  • 与Levenshtein相比,性能更快
  • 跨平台兼容性
  • 简单的 API,具有清晰的相似度评分
  • 处理不区分大小写的比较
  • 支持非英文字符

适合对象

  • 文本处理应用
  • 搜索和自动补全功能
  • 数据清洗工作流
  • 面向用户的输入验证
  • 需要快速字符串匹配的应用程序

使用建议

  • 最适合短到中等长度的字符串
  • 未针对超长文本进行优化
  • 不支持部分单词匹配
  • 仅限字符串到字符串的比较
  • 没有内置的停用词或词干提取处理

原始 README

以下为关联源码仓库的 README 原文快照,最新内容请访问源码仓库。

string_similarity

pub package license: MIT

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.

✨ Why string_similarity?

  • Simple extension methods and static methods
  • Scores from 0.0 (completely different) to 1.0 (identical)
  • Best-match search with individual ratings and the winning index
  • No runtime dependencies
  • Works on every platform supported by Dart and Flutter

📦 Installation

dart pub add string_similarity

Then import the package:

import 'package:string_similarity/string_similarity.dart';

🚀 Compare two strings

final score = 'healed'.similarityTo('sealed');

print(score); // 0.8

The equivalent static method is:

final score = StringSimilarity.compareTwoStrings('healed', 'sealed');

🏆 Find the best match

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'],
);

🧠 How it works

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.

🔡 Case-insensitive comparison

Normalize both strings before comparing them:

final score = 'DART'.toLowerCase().similarityTo('dart'.toLowerCase());

print(score); // 1.0

🧩 Available methods

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 score
  • bestMatch: the highest-rated target
  • bestMatchIndex: the winning target's original index

⚙️ Behavior and edge cases

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

🔎 Comparison details

  • Whitespace is ignored, but punctuation is not.
  • Comparison is case-sensitive and diacritic-sensitive.
  • The algorithm works on UTF-16 code units rather than grapheme clusters, so some emoji and combined Unicode characters may not behave like visible characters.
  • Sørensen–Dice measures shared bigrams. It is not a drop-in replacement for edit-distance algorithms such as Levenshtein distance.

📄 License

Licensed under the permissive MIT License.

所属分类

源码仓库

使用此插件的项目