Class: AnyStyle::Dictionary

Inherits:
Object
  • Object
show all
Defined in:
lib/anystyle/dictionary.rb,
lib/anystyle/dictionary/gdbm.rb,
lib/anystyle/dictionary/redis.rb,
lib/anystyle/dictionary/marshal.rb

Direct Known Subclasses

GDBM, Marshal, Redis

Defined Under Namespace

Classes: GDBM, Marshal, Redis

Class Attribute Summary collapse

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options) ⇒ Dictionary

Returns a new instance of Dictionary.



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

def initialize(options)
  @options = options
end

Class Attribute Details

.adaptersObject (readonly)

Returns the value of attribute adapters.



18
19
20
# File 'lib/anystyle/dictionary.rb', line 18

def adapters
  @adapters
end

.codeObject (readonly)

Returns the value of attribute code.



18
19
20
# File 'lib/anystyle/dictionary.rb', line 18

def code
  @code
end

.defaultsObject (readonly)

Returns the value of attribute defaults.



18
19
20
# File 'lib/anystyle/dictionary.rb', line 18

def defaults
  @defaults
end

.tagsObject (readonly)

Returns the value of attribute tags.



18
19
20
# File 'lib/anystyle/dictionary.rb', line 18

def tags
  @tags
end

Instance Attribute Details

#dbObject (readonly)

Returns the value of attribute db.



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

def db
  @db
end

#optionsObject (readonly)

Returns the value of attribute options.



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

def options
  @options
end

Class Method Details

.create(options = {}) ⇒ Object



20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
# File 'lib/anystyle/dictionary.rb', line 20

def create(options = {})
  return options if options.is_a?(Dictionary)

  options = defaults.merge(options || {})
  adapter = options.delete :adapter

  case adapter.to_sym
  when :memory, :hash
    new options
  when :gdbm
    require 'anystyle/dictionary/gdbm'
    Dictionary::GDBM.new options
  when :lmdb
    require 'anystyle/dictionary/lmdb'
    Dictionary::LMDB.new options
  when :redis
    require 'anystyle/dictionary/redis'
    Dictionary::Redis.new options
  when :marshal, :ruby
    require 'anystyle/dictionary/marshal'
    Dictionary::Marshal.new options
  else
    raise ArgumentError, "unknown adapter: #{adapter}"
  end
end

.instanceObject



46
47
48
# File 'lib/anystyle/dictionary.rb', line 46

def instance
  Thread.current['anystyle_dictionary'] ||= create.open
end

Instance Method Details

#closeObject



64
65
66
# File 'lib/anystyle/dictionary.rb', line 64

def close
  @db = nil
end

#empty?Boolean

Returns:

  • (Boolean)


76
77
78
# File 'lib/anystyle/dictionary.rb', line 76

def empty?
  db.empty?
end

#get(key) ⇒ Object Also known as: []



80
81
82
# File 'lib/anystyle/dictionary.rb', line 80

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

#openObject



57
58
59
60
61
62
# File 'lib/anystyle/dictionary.rb', line 57

def open
  @db = {} unless open?
  self
ensure
  populate! if empty?
end

#open?Boolean

Returns:

  • (Boolean)


72
73
74
# File 'lib/anystyle/dictionary.rb', line 72

def open?
  not db.nil?
end

#populate!Object



110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
# File 'lib/anystyle/dictionary.rb', line 110

def populate!
  require 'zlib'

  File.open(options[:source], 'rb') do |file|
    mode = 0

    Zlib::GzipReader.new(file, encoding: 'UTF-8').each do |line|
      line.strip!

      case line
      when /^#! (\w+)/i
        mode = Dictionary.code[$1.to_sym]
      when /^#/
        # skip comments
      else
        key = line.split(/\s+(\d+\.\d+)\s*$/)[0]
        put key, get(key) | mode
      end
    end
  end
end

#put(key, value) ⇒ Object Also known as: []=



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

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

#tag_counts(keys) ⇒ Object



99
100
101
102
103
104
105
106
107
108
# File 'lib/anystyle/dictionary.rb', line 99

def tag_counts(keys)
  counts = Dictionary.tags.map { 0 }
  keys.each do |key|
    value = get(key)
    Dictionary.tags.each.with_index do |tag, idx|
      counts[idx] += 1 if (value & Dictionary.code[tag] > 0)
    end if value > 0
  end
  counts
end

#tags(key) ⇒ Object



91
92
93
94
95
96
97
# File 'lib/anystyle/dictionary.rb', line 91

def tags(key)
  value = get key

  Dictionary.tags.map { |tag|
    (value & Dictionary.code[tag] > 0) ? 'T' : 'F'
  }
end

#truncateObject



68
69
70
# File 'lib/anystyle/dictionary.rb', line 68

def truncate
  close
end