Class: Anystyle::Parser::Dictionary

Inherits:
Object
  • Object
show all
Includes:
Singleton
Defined in:
lib/anystyle/parser/dictionary.rb

Overview

Dictionary is a Singleton object that provides a key-value store of the Anystyle Parser dictionary required for feature elicitation. This dictionary acts essentially like a Ruby Hash object, but because of the dictionary’s size it is not efficient to keep the entire dictionary in memory at all times. For that reason, Dictionary creates a persistent data store on disk using Kyoto Cabinet; if Kyoto Cabinet is not installed a Ruby Hash is used as a fall-back.

Starting with version 0.1.0 Redis support was added. If you would like to use Redis as the dictionary data store you can do so by installing ‘redis’ gem (and optionally the ‘hiredis’ gem).

The database will be automatically created from the dictionary file using the best available DBM the first time it is accessed. Once database file exists, the database will be restored from file. Therefore, if you make changes to the dictionary file, you will have to delete the old database file for a new one to be created.

Database creation in Kyoto-Cabinet mode requires write permissions. By default, the database will be created in the support directory of the Parser; if you have installed the gem version of the Parser, you may not have write permissions, but you can change the path in the Dictionary’s options.

## Configuration

To set the database mode:

Dictionary.instance.options[:mode] # => the database mode

For a list of database modes available in your environment consult:

Dictionary.modes # => [:kyoto, :redis, :hash]

Further options include:

Dictionary.instance.options[:source] # => the zipped dictionary file
Dictionary.instance.options[:cabinet] # => the database file (kyoto)
Dictionary.instance.options[:path] # => the database socket (redis)
Dictionary.instance.options[:host] # => dictionary host (redis)
Dictionary.instance.options[:part] # => dictionary port (redis)

Class Attribute Summary collapse

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeDictionary

Returns a new instance of Dictionary.



92
93
94
# File 'lib/anystyle/parser/dictionary.rb', line 92

def initialize
  @options = Dictionary.defaults.dup
end

Class Attribute Details

.codeObject (readonly)

Returns the value of attribute code.



87
88
89
# File 'lib/anystyle/parser/dictionary.rb', line 87

def code
  @code
end

.defaultsObject (readonly)

Returns the value of attribute defaults.



87
88
89
# File 'lib/anystyle/parser/dictionary.rb', line 87

def defaults
  @defaults
end

.keysObject (readonly)

Returns the value of attribute keys.



87
88
89
# File 'lib/anystyle/parser/dictionary.rb', line 87

def keys
  @keys
end

.modesObject (readonly)

Returns the value of attribute modes.



87
88
89
# File 'lib/anystyle/parser/dictionary.rb', line 87

def modes
  @modes
end

Instance Attribute Details

#optionsObject (readonly)

Returns the value of attribute options.



90
91
92
# File 'lib/anystyle/parser/dictionary.rb', line 90

def options
  @options
end

Instance Method Details

#[](key) ⇒ Object



96
97
98
# File 'lib/anystyle/parser/dictionary.rb', line 96

def [](key)
  db[key.to_s].to_i
end

#[]=(key, value) ⇒ Object



100
101
102
# File 'lib/anystyle/parser/dictionary.rb', line 100

def []=(key, value)
  db[key.to_s] = value
end

#closeObject



158
159
160
161
162
163
164
165
166
167
# File 'lib/anystyle/parser/dictionary.rb', line 158

def close
  case
  when @db.respond_to?(:close)
    @db.close
  when @db.respond_to?(:quit)
    @db.quit
  end

  @db = nil
end

#createObject



104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
# File 'lib/anystyle/parser/dictionary.rb', line 104

def create
  case options[:mode]
  when :kyoto
    truncate
    @db = KyotoCabinet::DB.new
    unless @db.open(path, KyotoCabinet::DB::OWRITER | KyotoCabinet::DB::OCREATE)
      raise DatabaseError, "failed to create cabinet file #{path}: #{@db.error}"
    end
    populate
    close

  when :redis
    @db ||= Redis.new(options)
    populate
    close

  else
    # nothing
  end
end

#openObject



130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
# File 'lib/anystyle/parser/dictionary.rb', line 130

def open
  case options[:mode]
  when :kyoto
    at_exit { ::Anystyle::Parser::Dictionary.instance.close }

    create unless File.exists?(path)

    @db = KyotoCabinet::DB.new
    unless @db.open(path, KyotoCabinet::DB::OREADER)
      raise DictionaryError, "failed to open cabinet file #{path}: #{@db.error}"
    end

  when :redis
    at_exit { ::Anystyle::Parser::Dictionary.instance.close }
    @db = Redis.new(options)

    populate if @db.dbsize.zero?

  else
    @db = Hash.new(0)
    populate
  end

  @db
end

#open?Boolean

Returns:

  • (Boolean)


156
# File 'lib/anystyle/parser/dictionary.rb', line 156

def open?; !!@db; end

#pathObject



169
170
171
172
173
174
175
176
177
178
# File 'lib/anystyle/parser/dictionary.rb', line 169

def path
  case options[:mode]
  when :kyoto
    options[:cabinet] || options[:path]
  when :redis
    options[:path] || options.values_at(:host, :port).join(':')
  else
    'hash'
  end
end

#truncateObject



125
126
127
128
# File 'lib/anystyle/parser/dictionary.rb', line 125

def truncate
  close
  File.unlink(path) if File.exists?(path)
end