Module: Ast::Merge::JaccardSimilarity
- Included in:
- TokenMatchRefiner
- Defined in:
- lib/ast/merge/jaccard_similarity.rb
Overview
Jaccard set-similarity utilities for fuzzy matching of text nodes.
Provides token extraction and Jaccard index computation, reusable across format-specific mergers (Markdown lists, TOML blocks, etc.).
Constant Summary collapse
- DEFAULT_STOPWORDS =
Common English stopwords excluded from token matching.
%w[ a an and are as at be but by for from has have in is it its of on or so that the their then there they this to up was will with ].to_set.freeze
- DEFAULT_MIN_TOKEN_LENGTH =
Minimum word length for a token to be considered significant.
3
Class Method Summary collapse
-
.extract_tokens(text, stopwords: DEFAULT_STOPWORDS, min_length: DEFAULT_MIN_TOKEN_LENGTH) ⇒ Set<String>
Extract significant tokens from text for set-based comparison.
-
.jaccard(a, b) ⇒ Float
Compute Jaccard similarity index between two sets.
Class Method Details
.extract_tokens(text, stopwords: DEFAULT_STOPWORDS, min_length: DEFAULT_MIN_TOKEN_LENGTH) ⇒ Set<String>
Extract significant tokens from text for set-based comparison.
Extracts words of at least min_length characters, lowercased,
with stopwords removed.
70 71 72 73 74 75 76 |
# File 'lib/ast/merge/jaccard_similarity.rb', line 70 def extract_tokens(text, stopwords: DEFAULT_STOPWORDS, min_length: DEFAULT_MIN_TOKEN_LENGTH) text.to_s .downcase .scan(/[[:alpha:]][[:alnum:]_-]{#{min_length - 1},}/) .reject { |t| stopwords.include?(t) } .to_set end |
.jaccard(a, b) ⇒ Float
Compute Jaccard similarity index between two sets.
J(A,B) = |A ∩ B| / |A ∪ B|
Returns 0.0 if either set is empty.
87 88 89 90 91 |
# File 'lib/ast/merge/jaccard_similarity.rb', line 87 def jaccard(a, b) return 0.0 if a.empty? || b.empty? (a & b).size.to_f / (a | b).size end |