Class: Pocketsphinx::Configuration

Inherits:
Object
  • Object
show all
Defined in:
lib/pocketsphinx/configuration.rb,
lib/pocketsphinx/configuration/setting_definition.rb

Defined Under Namespace

Classes: SettingDefinition

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(ps_arg_defs) ⇒ Configuration



10
11
12
13
14
15
16
17
18
19
20
# File 'lib/pocketsphinx/configuration.rb', line 10

def initialize(ps_arg_defs)
  @ps_arg_defs = ps_arg_defs
  @setting_definitions = SettingDefinition.from_arg_defs(ps_arg_defs)

  # Sets default settings based on definitions
  @ps_config = API::Sphinxbase.cmd_ln_parse_r(nil, ps_arg_defs, 0, nil, 1)

  # Sets default grammar and language model if they are not set explicitly and
  # are present in the default search path.
  API::Pocketsphinx.ps_default_search_args(@ps_config)
end

Instance Attribute Details

#ps_configObject (readonly)

Returns the value of attribute ps_config.



5
6
7
# File 'lib/pocketsphinx/configuration.rb', line 5

def ps_config
  @ps_config
end

#setting_definitionsObject (readonly)

Returns the value of attribute setting_definitions.



6
7
8
# File 'lib/pocketsphinx/configuration.rb', line 6

def setting_definitions
  @setting_definitions
end

Class Method Details

.defaultObject



22
23
24
# File 'lib/pocketsphinx/configuration.rb', line 22

def self.default
  new(API::Pocketsphinx.ps_args)
end

Instance Method Details

#[](name) ⇒ Object

Get a configuration setting



51
52
53
54
55
56
57
58
59
60
61
62
63
64
# File 'lib/pocketsphinx/configuration.rb', line 51

def [](name)
  case find_definition(name).type
  when :integer
    API::Sphinxbase.cmd_ln_int_r(@ps_config, "-#{name}")
  when :float
    API::Sphinxbase.cmd_ln_float_r(@ps_config, "-#{name}")
  when :string
    API::Sphinxbase.cmd_ln_str_r(@ps_config, "-#{name}")
  when :boolean
    API::Sphinxbase.cmd_ln_int_r(@ps_config, "-#{name}") != 0
  when :string_list
    raise NotImplementedException
  end
end

#[]=(name, value) ⇒ Object

Set a configuration setting with type checking



67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
# File 'lib/pocketsphinx/configuration.rb', line 67

def []=(name, value)
  case find_definition(name).type
  when :integer
    raise "Configuration setting '#{name}' must be a Fixnum" unless value.respond_to?(:to_i)
    API::Sphinxbase.cmd_ln_set_int_r(@ps_config, "-#{name}", value.to_i)
  when :float
    raise "Configuration setting '#{name}' must be a Float" unless value.respond_to?(:to_i)
    API::Sphinxbase.cmd_ln_set_float_r(@ps_config, "-#{name}", value.to_f)
  when :string
    API::Sphinxbase.cmd_ln_set_str_r(@ps_config, "-#{name}", value.to_s)
  when :boolean
    API::Sphinxbase.cmd_ln_set_int_r(@ps_config, "-#{name}", value ? 1 : 0)
  when :string_list
    raise NotImplementedException
  end
end

#details(name = nil) ⇒ Object

Get details for one or all configuration settings



33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
# File 'lib/pocketsphinx/configuration.rb', line 33

def details(name = nil)
  details = [name || setting_names].flatten.map do |name|
    definition = find_definition(name)

    {
      name: name,
      type: definition.type,
      default: definition.default,
      required: definition.required?,
      value: self[name],
      info: definition.doc
    }
  end

  name ? details.first : details
end

#setting_namesObject



26
27
28
# File 'lib/pocketsphinx/configuration.rb', line 26

def setting_names
  setting_definitions.keys.sort
end