文書Documentation
Plugins
Extend Kotoshu with custom dictionary types, custom suggestion strategies, document-format plugin gems, and the personal dictionary.
Two registries and one plugin hook: Kotoshu is extended by registration, not by patching.
拡張Extend
Custom suggestion strategies
A strategy is a class that subclasses
BaseStrategy and implements
one method, generate(context),
returning a SuggestionSet. The context carries the misspelled word, the
dictionary, and limits. Register it with
Kotoshu.register_suggestion_algorithm
and it becomes available by name.
require "kotoshu"
# Illustrative — a minimal strategy, not a useful one.
# Real strategies score candidates against context.dictionary;
# BaseStrategy supplies ranking helpers like create_suggestion_set.
class SuffixStrategy < Kotoshu::Suggestions::Strategies::BaseStrategy
def generate(context) # the one required method
word = context.word
candidates = %w[hello help held hell].select { |c| c.end_with?(word[-2..]) }
create_suggestion_set(candidates, original_word: word)
end
end
Kotoshu.register_suggestion_algorithm(:suffix, SuffixStrategy)Built-in strategies
| Strategy | Role | In default pipeline |
|---|---|---|
edit_distance | Levenshtein candidates, boosted by frequency tiers | yes |
phonetic | Sounds-alike candidates | yes |
keyboard_proximity | Adjacent-key typos, per keyboard layout (QWERTY, QWERTZ, AZERTY, JCUKEN) | yes |
ngram | Character n-gram similarity | yes |
symspell | SymSpell-style high-speed candidate lookup | optional |
semantic | Embedding-based reranking of candidates | optional — needs onnxruntime |
The default four run composed as a composite strategy; your registered
strategies join the same vocabulary of names when you configure
suggestion_algorithms.
Custom dictionary types
Dictionary backends are pluggable the same way. A custom class subclasses
Dictionary::Base, implements
lookup(word) (and optionally
words and
add_word), and is registered
for use as a dictionary_type:
Kotoshu.register_dictionary_type(:json, MyJsonDictionary)The built-in types and when to pick each are listed in Configuration.
Personal dictionary
For adding words rather than algorithms, the personal dictionary needs no
code: kotoshu personal add WORD
writes to ~/.config/kotoshu/personal.dic
and applies to every subsequent check.
Document-format plugins
Format-specific parsers (Markdown, AsciiDoc, and beyond) are plugin gems, not
core. A parser responds to from_string(text, language_code:)
and registers itself from a file at lib/kotoshu_plugin/document/format.rb
via Kotoshu::Documents.register.
Kotoshu auto-discovers installed plugin gems, so callers get format support
without configuration.
For worked examples — remote and database-backed dictionaries, composite strategies, testing extensions — see the plugin development guide in the gem repository.