搜尋套件、儲存庫與 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.

所屬分類

原始碼儲存庫

使用此套件的專案