パッケージ、リポジトリ、アプリカテゴリを検索

パッケージ詳細v2.2.0

string_similarity

Dice係数に基づいて、2つの文字列間の類似度を計算します。これは、レーベンシュタイン距離よりも優れています。

AndroidiOSWebmacOSWindowsLinux
30日間ダウンロード
8万
いいね数
129
Pub ポイント
160

パッケージをインストール

flutter pub add string_similarity

パッケージ概要

公開情報に基づく選定ガイドです。参考としてご利用ください。

string_similarity パッケージは、Dice係数を使用して2つの文字列間の類似度を計算します。この方法は、Levenshtein距離よりも正確で高速な場合が多くあります。曖昧な一致、タイポ補正、テキストデータにおける重複検出などのタスクに最適です。アルゴリズムは文字列間の共有ビグラムを評価し、0(一致なし)から1(完全一致)までの類似度スコアを生成します。この軽量ユーティリティは、Android、iOS、Web、macOS、Windows、Linuxなどすべての主要プラットフォームをサポートしており、効率的な文字列比較が必要なクロスプラットフォームアプリケーションに非常に有用です。

用途

  • ユーザー入力における曖昧検索
  • 類似する重複テキストエントリの検出
  • 自動修正の提案
  • データの重複削除
  • 複数のソース間での製品名の一致
  • スペルの修正

主な機能

  • 正確性にDice係数を使用する
  • レーベンシュタインと比較して高速なパフォーマンス
  • クロスプラットフォーム互換性
  • 明確な類似スコアを備えたシンプルな 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.

カテゴリー

ソースリポジトリ

このパッケージを使用しているプロジェクト