class AnyStyle::Dictionary

Attributes

adapters[R]
code[R]
defaults[R]
tags[R]
db[R]
options[R]

Public Class Methods

create(options = {}) click to toggle source
   # File lib/anystyle/dictionary.rb
20 def create(options = {})
21   return options if options.is_a?(Dictionary)
22 
23   options = defaults.merge(options || {})
24   adapter = options.delete :adapter
25 
26   case adapter.to_sym
27   when :memory, :hash
28     new options
29   when :gdbm
30     require 'anystyle/dictionary/gdbm'
31     Dictionary::GDBM.new options
32   when :lmdb
33     require 'anystyle/dictionary/lmdb'
34     Dictionary::LMDB.new options
35   when :redis
36     require 'anystyle/dictionary/redis'
37     Dictionary::Redis.new options
38   when :marshal, :ruby
39     require 'anystyle/dictionary/marshal'
40     Dictionary::Marshal.new options
41   else
42     raise ArgumentError, "unknown adapter: #{adapter}"
43   end
44 end
instance() click to toggle source
   # File lib/anystyle/dictionary.rb
46 def instance
47   Thread.current['anystyle_dictionary'] ||= create.open
48 end
new(options) click to toggle source
   # File lib/anystyle/dictionary.rb
53 def initialize(options)
54   @options = options
55 end

Public Instance Methods

[](key)
Alias for: get
[]=(key, value)
Alias for: put
close() click to toggle source
   # File lib/anystyle/dictionary.rb
64 def close
65   @db = nil
66 end
empty?() click to toggle source
   # File lib/anystyle/dictionary.rb
76 def empty?
77   db.empty?
78 end
get(key) click to toggle source
   # File lib/anystyle/dictionary.rb
80 def get(key)
81   db[key.to_s].to_i
82 end
Also aliased as: []
open() click to toggle source
   # File lib/anystyle/dictionary.rb
57 def open
58   @db = {} unless open?
59   self
60 ensure
61   populate! if empty?
62 end
open?() click to toggle source
   # File lib/anystyle/dictionary.rb
72 def open?
73   not db.nil?
74 end
populate!() click to toggle source
    # File lib/anystyle/dictionary.rb
110 def populate!
111   require 'zlib'
112 
113   File.open(options[:source], 'rb') do |file|
114     mode = 0
115 
116     Zlib::GzipReader.new(file, encoding: 'UTF-8').each do |line|
117       line.strip!
118 
119       case line
120       when /^#! (\w+)/i
121         mode = Dictionary.code[$1.to_sym]
122       when /^#/
123         # skip comments
124       else
125         key = line.split(/\s+(\d+\.\d+)\s*$/)[0]
126         put key, get(key) | mode
127       end
128     end
129   end
130 end
put(key, value) click to toggle source
   # File lib/anystyle/dictionary.rb
84 def put(key, value)
85   db[key.to_s] = value.to_i
86 end
Also aliased as: []=
tag_counts(keys) click to toggle source
    # File lib/anystyle/dictionary.rb
 99 def tag_counts(keys)
100   counts = Dictionary.tags.map { 0 }
101   keys.each do |key|
102     value = get(key)
103     Dictionary.tags.each.with_index do |tag, idx|
104       counts[idx] += 1 if (value & Dictionary.code[tag] > 0)
105     end if value > 0
106   end
107   counts
108 end
tags(key) click to toggle source
   # File lib/anystyle/dictionary.rb
91 def tags(key)
92   value = get key
93 
94   Dictionary.tags.map { |tag|
95     (value & Dictionary.code[tag] > 0) ? 'T' : 'F'
96   }
97 end
truncate() click to toggle source
   # File lib/anystyle/dictionary.rb
68 def truncate
69   close
70 end