Class: Anystyle::Parser::Dictionary
- Inherits:
-
Object
- Object
- Anystyle::Parser::Dictionary
- 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.[: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.[:source] # => the zipped dictionary file
Dictionary.instance.[:cabinet] # => the database file (kyoto)
Dictionary.instance.[:path] # => the database socket (redis)
Dictionary.instance.[:host] # => dictionary host (redis)
Dictionary.instance.[:part] # => dictionary port (redis)
Class Attribute Summary collapse
-
.code ⇒ Object
readonly
Returns the value of attribute code.
-
.defaults ⇒ Object
readonly
Returns the value of attribute defaults.
-
.keys ⇒ Object
readonly
Returns the value of attribute keys.
-
.modes ⇒ Object
readonly
Returns the value of attribute modes.
Instance Attribute Summary collapse
-
#options ⇒ Object
readonly
Returns the value of attribute options.
Instance Method Summary collapse
- #[](key) ⇒ Object
- #[]=(key, value) ⇒ Object
- #close ⇒ Object
- #config(&block) ⇒ Object
- #create ⇒ Object
-
#initialize ⇒ Dictionary
constructor
A new instance of Dictionary.
- #open ⇒ Object
- #open? ⇒ Boolean
- #path ⇒ Object
- #populated? ⇒ Boolean
- #truncate ⇒ Object
Constructor Details
#initialize ⇒ Dictionary
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
.code ⇒ Object (readonly)
Returns the value of attribute code.
91 92 93 |
# File 'lib/anystyle/parser/dictionary.rb', line 91 def code @code end |
.defaults ⇒ Object (readonly)
Returns the value of attribute defaults.
91 92 93 |
# File 'lib/anystyle/parser/dictionary.rb', line 91 def defaults @defaults end |
.keys ⇒ Object (readonly)
Returns the value of attribute keys.
91 92 93 |
# File 'lib/anystyle/parser/dictionary.rb', line 91 def keys @keys end |
.modes ⇒ Object (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
#options ⇒ Object (readonly)
Returns the value of attribute options.
94 95 96 |
# File 'lib/anystyle/parser/dictionary.rb', line 94 def @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 |
#close ⇒ Object
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[] end |
#create ⇒ Object
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 [: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 |
#open ⇒ Object
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 [: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() if [:namespace] && defined?(Redis::Namespace) @db = Redis::Namespace.new [:namespace], :redis => @db end populate unless populated? else @db = Hash.new(0) populate end @db end |
#open? ⇒ Boolean
163 |
# File 'lib/anystyle/parser/dictionary.rb', line 163 def open?() !!@db end |
#path ⇒ Object
176 177 178 179 180 181 182 183 184 185 |
# File 'lib/anystyle/parser/dictionary.rb', line 176 def path case [:mode] when :kyoto [:cabinet] || [:path] when :redis [:path] || .values_at(:host, :port).join(':') else 'hash' end end |
#populated? ⇒ Boolean
187 188 189 |
# File 'lib/anystyle/parser/dictionary.rb', line 187 def populated? !!self['__created_at'] end |
#truncate ⇒ Object
128 129 130 131 |
# File 'lib/anystyle/parser/dictionary.rb', line 128 def truncate close File.unlink(path) if File.exists?(path) end |