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.

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.options[:path] # => the database file
Dictionary.instance.options[:source] # => the (zipped) dictionary file

Class Attribute Summary collapse

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeDictionary

Returns a new instance of Dictionary.



56
57
58
# File 'lib/anystyle/parser/dictionary.rb', line 56

def initialize
  @options = Dictionary.defaults.dup
end

Class Attribute Details

.codeObject (readonly)

Returns the value of attribute code.



50
51
52
# File 'lib/anystyle/parser/dictionary.rb', line 50

def code
  @code
end

.defaultsObject (readonly)

Returns the value of attribute defaults.



50
51
52
# File 'lib/anystyle/parser/dictionary.rb', line 50

def defaults
  @defaults
end

.keysObject (readonly)

Returns the value of attribute keys.



50
51
52
# File 'lib/anystyle/parser/dictionary.rb', line 50

def keys
  @keys
end

.modeObject (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

#optionsObject (readonly)

Returns the value of attribute options.



54
55
56
# File 'lib/anystyle/parser/dictionary.rb', line 54

def options
  @options
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

#closeObject



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

def close
  @db.close if @db.respond_to?(:close)
  @db = nil
end

#createObject



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

#openObject



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

Returns:

  • (Boolean)


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

def open?; !!@db; end

#pathObject



114
115
116
# File 'lib/anystyle/parser/dictionary.rb', line 114

def path
  options[:path]
end

#truncateObject



83
84
85
86
# File 'lib/anystyle/parser/dictionary.rb', line 83

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