Module: Kotoshu::Defaults

Defined in:
lib/kotoshu/defaults.rb

Overview

Sensible defaults for Kotoshu configuration.

Provides auto-detection of system dictionaries and fallback to bundled dictionaries, ensuring Kotoshu works out of the box.

Constant Summary collapse

SYSTEM_DICTIONARY_PATHS =

Standard system dictionary paths.

[
  "/usr/share/dict/words",
  "/usr/share/dict/web2",
  "/usr/share/dict/web2a",
  "/usr/dict/words"
].freeze
BUNDLED_DICTIONARY_PATHS =

Bundled dictionary paths (relative to gem root).

[
  "dictionaries/unix_words/words",
  "dictionaries/unix_words/web2",
  "dictionaries/unix_words/web2a"
].freeze

Class Method Summary collapse

Class Method Details

.bundled_dictionary_pathString?

Get path to bundled dictionary.

Returns:

  • (String, nil)

    Path to bundled dictionary or nil



39
40
41
42
43
44
# File 'lib/kotoshu/defaults.rb', line 39

def bundled_dictionary_path
  BUNDLED_DICTIONARY_PATHS.find do |path|
    full_path = File.expand_path("../../#{path}", __dir__)
    File.exist?(full_path)
  end
end

.configureConfiguration

Configure Kotoshu with sensible defaults.

Returns:



74
75
76
77
78
79
80
81
82
83
# File 'lib/kotoshu/defaults.rb', line 74

def configure
  default_dictionary

  Configuration::Builder.build do |c|
    c.dictionary_type = :plain_text
    c.language = "en-US"
    c.max_suggestions = 10
    c.case_sensitive = false
  end
end

.default_dictionaryDictionary::Base

Get default dictionary.

Tries system dictionary first, then bundled dictionary, then falls back to an empty custom dictionary.

Returns:



52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
# File 'lib/kotoshu/defaults.rb', line 52

def default_dictionary
  # Try system dictionary
  system_path = detect_system_dictionary
  return Dictionary::PlainText.new(system_path, language_code: "en") if system_path

  # Try bundled dictionary
  bundled_path = bundled_dictionary_path
  if bundled_path
    full_path = File.expand_path("../../#{bundled_path}", __dir__)
    return Dictionary::PlainText.new(full_path, language_code: "en")
  end

  # Fall back to minimal dictionary with common words
  Dictionary::PlainText.from_words(
    %w[the and for are but not you all any can had has him his how her its now our our was what],
    language_code: "en"
  )
end

.detect_system_dictionaryString?

Detect system dictionary.

Returns:

  • (String, nil)

    Path to system dictionary or nil



30
31
32
33
34
# File 'lib/kotoshu/defaults.rb', line 30

def detect_system_dictionary
  SYSTEM_DICTIONARY_PATHS.find do |path|
    File.exist?(path)
  end
end