Class: Kabosu::Dictionary

Inherits:
Object
  • Object
show all
Defined in:
lib/kabosu.rb

Overview

── Dictionary: keyword API and management ──

Constant Summary collapse

DEFAULT_CONFIG_PATH =
File.expand_path("kabosu/resources/sudachi.json", __dir__).freeze

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

._newObject



69
# File 'lib/kabosu.rb', line 69

alias_method :_new, :new

.install(edition = "core", version: nil) ⇒ Object



100
101
102
# File 'lib/kabosu.rb', line 100

def install(edition = "core", version: nil)
  dict_manager.install(edition, version: version)
end

.listObject



108
109
110
# File 'lib/kabosu.rb', line 108

def list
  dict_manager.installed
end

.new(config: nil, system_dict: nil, user_dicts: nil) ⇒ Object



71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
# File 'lib/kabosu.rb', line 71

def new(config: nil, system_dict: nil, user_dicts: nil)
  unless config.nil? || config.is_a?(String)
    raise ArgumentError, "config must be a String or nil"
  end
  unless system_dict.nil? || system_dict.is_a?(String)
    raise ArgumentError, "system_dict must be a String or nil"
  end
  unless user_dicts.nil? || user_dicts.is_a?(Array)
    raise ArgumentError, "user_dicts must be an Array<String> or nil"
  end
  if user_dicts&.any? { !_1.is_a?(String) }
    raise ArgumentError, "user_dicts must contain only String values"
  end

  if config.nil? && system_dict.nil?
    raise ArgumentError, "either config or system_dict is required"
  end

  # Default to the sudachi.json bundled with this gem when only
  # system_dict is given. sudachi.rs's own default config path is
  # captured via env!("CARGO_MANIFEST_DIR") and doesn't exist on
  # consumers' machines once the gem is precompiled and shipped.
  config ||= DEFAULT_CONFIG_PATH

  _new(config, system_dict, user_dicts)
rescue RuntimeError => e
  raise map_dictionary_init_error(e, config: config, system_dict: system_dict)
end

.path(edition: nil) ⇒ Object



104
105
106
# File 'lib/kabosu.rb', line 104

def path(edition: nil)
  dict_manager.find(edition: edition)
end

Instance Method Details

#_createObject



130
# File 'lib/kabosu.rb', line 130

alias_method :_create, :create

#_lookupObject



131
# File 'lib/kabosu.rb', line 131

alias_method :_lookup, :lookup

#create(**options) ⇒ Object

Raises:

  • (ArgumentError)


133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
# File 'lib/kabosu.rb', line 133

def create(**options)
  unknown = options.keys - %i[mode fields debug projection]
  raise ArgumentError, "unknown keyword(s): #{unknown.join(', ')}" unless unknown.empty?

  mode = options.fetch(:mode, MODE_C)
  fields = options.fetch(:fields, nil)
  debug = options.fetch(:debug, false)
  projection = options.fetch(:projection, nil)

  unless fields.nil? || fields.is_a?(Array)
    raise ArgumentError, "fields must be an Array<String|Symbol> or nil"
  end
  if fields&.any? { !(_1.is_a?(String) || _1.is_a?(Symbol)) }
    raise ArgumentError, "fields must contain only String or Symbol values"
  end
  unless debug == true || debug == false
    raise ArgumentError, "debug must be true or false"
  end

  unless projection.nil?
    raise NotImplementedError, "projection is not supported yet"
  end

  mode_str = Kabosu.__send__(:normalize_mode, mode)
  _create(mode_str, fields, debug)
end

#lookup(text) ⇒ Object



160
161
162
163
164
165
166
167
# File 'lib/kabosu.rb', line 160

def lookup(text)
  unless text.is_a?(String)
    raise ArgumentError, "text must be a String"
  end
  MorphemeList.new(_lookup(text))
rescue RuntimeError => e
  raise LookupError.new(e.message), cause: e
end