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.
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 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.
Dictionary.instance.[:path] # => the database file
Dictionary.instance.[:source] # => the (zipped) dictionary file
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.
-
.mode ⇒ Object
readonly
Returns the value of attribute mode.
Instance Attribute Summary collapse
-
#options ⇒ Object
readonly
Returns the value of attribute options.
Instance Method Summary collapse
- #[](key) ⇒ Object
- #[]=(key, value) ⇒ Object
- #close ⇒ Object
- #create ⇒ Object
-
#initialize ⇒ Dictionary
constructor
A new instance of Dictionary.
- #open ⇒ Object
- #open? ⇒ Boolean
- #path ⇒ Object
- #truncate ⇒ Object
Constructor Details
#initialize ⇒ Dictionary
Returns a new instance of Dictionary.
56 57 58 |
# File 'lib/anystyle/parser/dictionary.rb', line 56 def initialize = Dictionary.defaults.dup end |
Class Attribute Details
.code ⇒ Object (readonly)
Returns the value of attribute code.
50 51 52 |
# File 'lib/anystyle/parser/dictionary.rb', line 50 def code @code end |
.defaults ⇒ Object (readonly)
Returns the value of attribute defaults.
50 51 52 |
# File 'lib/anystyle/parser/dictionary.rb', line 50 def defaults @defaults end |
.keys ⇒ Object (readonly)
Returns the value of attribute keys.
50 51 52 |
# File 'lib/anystyle/parser/dictionary.rb', line 50 def keys @keys end |
.mode ⇒ Object (readonly)
Returns the value of attribute mode.
50 51 52 |
# File 'lib/anystyle/parser/dictionary.rb', line 50 def mode @mode end |
Instance Attribute Details
#options ⇒ Object (readonly)
Returns the value of attribute options.
54 55 56 |
# File 'lib/anystyle/parser/dictionary.rb', line 54 def end |
Instance Method Details
#[](key) ⇒ Object
60 61 62 |
# File 'lib/anystyle/parser/dictionary.rb', line 60 def [](key) db[key.to_s].to_i end |
#[]=(key, value) ⇒ Object
64 65 66 |
# File 'lib/anystyle/parser/dictionary.rb', line 64 def []=(key, value) db[key.to_s] = value end |
#close ⇒ Object
109 110 111 112 |
# File 'lib/anystyle/parser/dictionary.rb', line 109 def close @db.close if @db.respond_to?(:close) @db = nil end |
#create ⇒ Object
68 69 70 71 72 73 74 75 76 77 78 79 80 81 |
# File 'lib/anystyle/parser/dictionary.rb', line 68 def create case Dictionary.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
88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 |
# File 'lib/anystyle/parser/dictionary.rb', line 88 def open create unless File.exists?(path) case Dictionary.mode when :kyoto at_exit { ::Anystyle::Parser::Dictionary.instance.close } @db = KyotoCabinet::DB.new unless @db.open(path, KyotoCabinet::DB::OREADER) raise DictionaryError, "failed to open cabinet file #{path}: #{@db.error}" end else @db = Hash.new(0) populate end @db end |
#open? ⇒ Boolean
107 |
# File 'lib/anystyle/parser/dictionary.rb', line 107 def open?; !!@db; end |
#path ⇒ Object
114 115 116 |
# File 'lib/anystyle/parser/dictionary.rb', line 114 def path [:path] end |
#truncate ⇒ Object
83 84 85 86 |
# File 'lib/anystyle/parser/dictionary.rb', line 83 def truncate close File.unlink(path) if File.exists?(path) end |