Class: Kotoshu::Suggestions::Strategies::SemanticStrategy
- Inherits:
-
BaseStrategy
- Object
- BaseStrategy
- Kotoshu::Suggestions::Strategies::SemanticStrategy
- Defined in:
- lib/kotoshu/suggestions/strategies/semantic_strategy.rb
Overview
Semantic strategy using FastText ONNX embeddings.
Provides embedding-based spell correction for:
- Typos: Re-ranks edit-distance candidates by semantic similarity
- Real-word errors: Detects when valid words are used incorrectly in context
This strategy works alongside other strategies (EditDistance, Phonetic, etc.) to provide comprehensive spell checking with semantic awareness.
Instance Attribute Summary collapse
-
#language_code ⇒ String
readonly
Language code (ISO 639-1).
-
#model ⇒ Embeddings::OnnxRuntimeModel
readonly
The ONNX model.
-
#search ⇒ Embeddings::SimilaritySearch
readonly
The similarity search.
-
#vocabulary ⇒ Embeddings::Vocabulary
readonly
The vocabulary.
Attributes inherited from BaseStrategy
Instance Method Summary collapse
-
#find_similar_words(word, k: 10) ⇒ Array<Hash>
Find semantically similar words.
-
#generate(context) ⇒ SuggestionSet
Generate suggestions using semantic similarity.
-
#handles?(_context) ⇒ Boolean
Check if this strategy should handle the context.
-
#initialize(language_code:, cache: nil, preload_embeddings: false, max_context_window: 5, min_semantic_similarity: 0.5, semantic_boost_weight: 0.3, **config) ⇒ SemanticStrategy
constructor
Create a new semantic strategy.
-
#semantic_similarity(word1, word2) ⇒ Float?
Compute semantic similarity between two words.
-
#to_s ⇒ String
(also: #inspect)
String representation.
Methods inherited from BaseStrategy
#calculate_ngram_similarity, #create_suggestion, #create_suggestion_set, #enabled?, #generate_ngrams, #get_config, #has_config?, #max_results, #priority
Constructor Details
#initialize(language_code:, cache: nil, preload_embeddings: false, max_context_window: 5, min_semantic_similarity: 0.5, semantic_boost_weight: 0.3, **config) ⇒ SemanticStrategy
Create a new semantic strategy.
47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 |
# File 'lib/kotoshu/suggestions/strategies/semantic_strategy.rb', line 47 def initialize(language_code:, cache: nil, preload_embeddings: false, max_context_window: 5, min_semantic_similarity: 0.5, semantic_boost_weight: 0.3, **config) super( name: :semantic, min_semantic_similarity: min_semantic_similarity, semantic_boost_weight: semantic_boost_weight, max_context_window: max_context_window, **config ) @language_code = language_code @max_context_window = max_context_window @min_semantic_similarity = min_semantic_similarity @semantic_boost_weight = semantic_boost_weight # Initialize embedding components (cache, ) end |
Instance Attribute Details
#language_code ⇒ String (readonly)
Returns Language code (ISO 639-1).
27 28 29 |
# File 'lib/kotoshu/suggestions/strategies/semantic_strategy.rb', line 27 def language_code @language_code end |
#model ⇒ Embeddings::OnnxRuntimeModel (readonly)
Returns The ONNX model.
33 34 35 |
# File 'lib/kotoshu/suggestions/strategies/semantic_strategy.rb', line 33 def model @model end |
#search ⇒ Embeddings::SimilaritySearch (readonly)
Returns The similarity search.
36 37 38 |
# File 'lib/kotoshu/suggestions/strategies/semantic_strategy.rb', line 36 def search @search end |
#vocabulary ⇒ Embeddings::Vocabulary (readonly)
Returns The vocabulary.
30 31 32 |
# File 'lib/kotoshu/suggestions/strategies/semantic_strategy.rb', line 30 def vocabulary @vocabulary end |
Instance Method Details
#find_similar_words(word, k: 10) ⇒ Array<Hash>
Find semantically similar words.
122 123 124 125 126 |
# File 'lib/kotoshu/suggestions/strategies/semantic_strategy.rb', line 122 def find_similar_words(word, k: 10) return [] unless @search @search.find_nearest(word, k: k, exclude_self: false) end |
#generate(context) ⇒ SuggestionSet
Generate suggestions using semantic similarity.
Handles two cases:
- Word not in vocabulary (typo): Re-ranks edit-distance candidates
- Word in vocabulary (real-word error): Finds semantically similar alternatives
74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 |
# File 'lib/kotoshu/suggestions/strategies/semantic_strategy.rb', line 74 def generate(context) word = context.word # Ensure embeddings are loaded return SuggestionSet.empty unless @search # Case 1: Word not in vocabulary (typo) unless @vocabulary.include?(word) return generate_for_typo(context) end # Case 2: Real-word error detection # Find semantically similar words that might be correct in context generate_for_real_word_error(context) end |
#handles?(_context) ⇒ Boolean
Check if this strategy should handle the context.
Semantic strategy handles:
- Words not in vocabulary (for typo re-ranking)
- Words in vocabulary (for real-word error detection)
98 99 100 101 102 103 104 |
# File 'lib/kotoshu/suggestions/strategies/semantic_strategy.rb', line 98 def handles?(_context) return false unless enabled? return false unless @search && @vocabulary # Handle all words - we filter in generate() true end |
#semantic_similarity(word1, word2) ⇒ Float?
Compute semantic similarity between two words.
111 112 113 114 115 |
# File 'lib/kotoshu/suggestions/strategies/semantic_strategy.rb', line 111 def semantic_similarity(word1, word2) return nil unless @search @search.similarity(word1, word2) end |
#to_s ⇒ String Also known as: inspect
String representation.
131 132 133 |
# File 'lib/kotoshu/suggestions/strategies/semantic_strategy.rb', line 131 def to_s "SemanticStrategy(language: #{@language_code}, vocab_size: #{@vocabulary&.size || 0}, loaded: #{@search && true})" end |