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.



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

def initialize
  @options = Dictionary.defaults.dup
end

Class Attribute Details

.codeObject (readonly)

Returns the value of attribute code.



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

def code
  @code
end

.defaultsObject (readonly)

Returns the value of attribute defaults.



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

def defaults
  @defaults
end

.keysObject (readonly)

Returns the value of attribute keys.



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

def keys
  @keys
end

.modesObject (readonly)

Returns the value of attribute modes.



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

def modes
  @modes
end

Instance Attribute Details

#optionsObject (readonly)

Returns the value of attribute options.



94
95
96
# File 'lib/anystyle/parser/dictionary.rb', line 94

def options
  @options
end

Instance Method Details

#[](key) ⇒ Object



104
105
106
# File 'lib/anystyle/parser/dictionary.rb', line 104

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

#[]=(key, value) ⇒ Object



108
109
110
# File 'lib/anystyle/parser/dictionary.rb', line 108

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

#closeObject



165
166
167
168
169
170
171
172
173
174
# File 'lib/anystyle/parser/dictionary.rb', line 165

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

  @db = nil
end

#config(&block) ⇒ Object



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

def config(&block)
  block[options]
end

#createObject



112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
# File 'lib/anystyle/parser/dictionary.rb', line 112

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

  else
    # nothing
  end
end

#openObject



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
159
160
161
# File 'lib/anystyle/parser/dictionary.rb', line 133

def open
  case options[:mode]
  when :kyoto
    at_exit { Anystyle.dictionary.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.dictionary.close }
    @db = Redis.new(options)

    if options[:namespace] && defined?(Redis::Namespace)
      @db = Redis::Namespace.new options[:namespace], :redis => @db
    end

    populate unless populated?

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

  @db
end

#open?Boolean

Returns:

  • (Boolean)


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

def open?() !!@db end

#pathObject



176
177
178
179
180
181
182
183
184
185
# File 'lib/anystyle/parser/dictionary.rb', line 176

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

#populated?Boolean

Returns:

  • (Boolean)


187
188
189
# File 'lib/anystyle/parser/dictionary.rb', line 187

def populated?
  !!self['__created_at']
end

#truncateObject



128
129
130
131
# File 'lib/anystyle/parser/dictionary.rb', line 128

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