Class: CharTree::Dictionary

Inherits:
Object
  • Object
show all
Defined in:
lib/char_tree/dictionary.rb

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(file = false) ⇒ Dictionary

Returns a new instance of Dictionary.



4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
# File 'lib/char_tree/dictionary.rb', line 4

def initialize(file = false)
  @callbacks       = {}
  @f               = false
  @first           = nil
  @size          = 0
  @n             = 0
  @results       = []
  @searching      = false
  @use_set        = false
  @set_chars      = {}
  @pattern        = '*'

  if file
    @file = file
  else
    root = File.expand_path('../../..', __FILE__)
    @file = "#{root}/tmp/dictionary.bda"
  end
  pattern
end

Class Method Details

.event(name, method_name) ⇒ Object



62
63
64
65
# File 'lib/char_tree/dictionary.rb', line 62

def self.event(name, method_name)
    $callbacks[name] = [] unless $callbacks[name]
    $callbacks[name] << method_name
end

Instance Method Details

#add(word) ⇒ Object

Words



70
71
72
73
# File 'lib/char_tree/dictionary.rb', line 70

def add(word)
  return unless @f
  add_char(encode(word).split(""), @first)
end

#closeObject



49
50
51
52
53
# File 'lib/char_tree/dictionary.rb', line 49

def close
  @f.close if @f
  @f = false
  @first = nil
end

#createObject



34
35
36
37
38
39
40
41
# File 'lib/char_tree/dictionary.rb', line 34

def create
  return if @f
  delete
  @f = File.new(@file, "w+b")
  n = create_node
  write n
  close
end

#deleteObject



30
31
32
# File 'lib/char_tree/dictionary.rb', line 30

def delete
  File.delete(@file) if File.file?(@file)
end

#empty?Boolean

Returns:

  • (Boolean)


55
56
57
# File 'lib/char_tree/dictionary.rb', line 55

def empty?
  File.zero?(@file)
end

#file_pathObject

File management



26
27
28
# File 'lib/char_tree/dictionary.rb', line 26

def file_path
  @file
end

#next_resultObject



136
137
138
# File 'lib/char_tree/dictionary.rb', line 136

def next_result
  @results.shift
end

#no_use_setObject



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

def no_use_set
  @use_set = false
end

#openObject



43
44
45
46
47
# File 'lib/char_tree/dictionary.rb', line 43

def open
  return if @f
@f = File.new(@file, "r+b")
first
end

#pattern(pattern = '*') ⇒ Object



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

def pattern(pattern = '*')
  @pattern = "^#{pattern}$"
  @pattern = @pattern.gsub('*', '.*')
  @pattern = @pattern.gsub('?', '.')
    @pattern = @pattern.gsub('CH', '8')
    @pattern = @pattern.gsub('LL', '1')
    @pattern = @pattern.gsub('RR', '4')
end

#results?Boolean

Returns:

  • (Boolean)


128
129
130
# File 'lib/char_tree/dictionary.rb', line 128

def results?
  @results.count > 0
end

#search(min = 1, max = 999) ⇒ Object



109
110
111
112
113
# File 'lib/char_tree/dictionary.rb', line 109

def search(min = 1, max = 999)
  return unless @f
  return if searching?
  decode(search_next_chars(@first, '', min, max))
end

#search_all(min = 1, max = 999) ⇒ Object



115
116
117
118
119
120
121
# File 'lib/char_tree/dictionary.rb', line 115

def search_all(min = 1, max = 999)
  return unless @f
  return if searching?
  @searching = true
  search_next_chars(@first, '', min, max)
  @searching = false
end

#searching?Boolean

Results

Returns:

  • (Boolean)


124
125
126
# File 'lib/char_tree/dictionary.rb', line 124

def searching?
  @searching
end

#set(chars = []) ⇒ Object



88
89
90
91
92
93
94
95
96
97
# File 'lib/char_tree/dictionary.rb', line 88

def set(chars = [])
  use_set
  @set_chars = {}
  chars.each do |c|
      c = '8' if (c  == 'CH')
      c = '1' if (c  == 'LL')
      c = '4' if (c  == 'RR')
    @set_chars[c] = @set_chars[c] ? @set_chars[c] + 1 : 1
  end
end

#total_resultsObject



132
133
134
# File 'lib/char_tree/dictionary.rb', line 132

def total_results
  @results.count
end

#use_setObject

Search



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

def use_set
  @use_set = true
end

#use_set?Boolean

Returns:

  • (Boolean)


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

def use_set?
  @use_set
end