Class: CharTree::Dictionary
- Inherits:
-
Object
- Object
- CharTree::Dictionary
- Defined in:
- lib/char_tree/dictionary.rb
Class Method Summary collapse
Instance Method Summary collapse
-
#add(word) ⇒ Object
Words.
- #close ⇒ Object
- #create ⇒ Object
- #delete ⇒ Object
- #empty? ⇒ Boolean
-
#file_path ⇒ Object
File management.
-
#initialize(file = false) ⇒ Dictionary
constructor
A new instance of Dictionary.
- #next_result ⇒ Object
- #no_use_set ⇒ Object
- #open ⇒ Object
- #pattern(pattern = '*') ⇒ Object
- #results? ⇒ Boolean
- #search(min = 1, max = 999) ⇒ Object
- #search_all(min = 1, max = 999) ⇒ Object
-
#searching? ⇒ Boolean
Results.
- #set(chars = []) ⇒ Object
- #total_results ⇒ Object
-
#use_set ⇒ Object
Search.
- #use_set? ⇒ Boolean
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.('../../..', __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 |
#close ⇒ Object
49 50 51 52 53 |
# File 'lib/char_tree/dictionary.rb', line 49 def close @f.close if @f @f = false @first = nil end |
#create ⇒ Object
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 |
#delete ⇒ Object
30 31 32 |
# File 'lib/char_tree/dictionary.rb', line 30 def delete File.delete(@file) if File.file?(@file) end |
#empty? ⇒ Boolean
55 56 57 |
# File 'lib/char_tree/dictionary.rb', line 55 def empty? File.zero?(@file) end |
#file_path ⇒ Object
File management
26 27 28 |
# File 'lib/char_tree/dictionary.rb', line 26 def file_path @file end |
#next_result ⇒ Object
136 137 138 |
# File 'lib/char_tree/dictionary.rb', line 136 def next_result @results.shift end |
#no_use_set ⇒ Object
80 81 82 |
# File 'lib/char_tree/dictionary.rb', line 80 def no_use_set @use_set = false end |
#open ⇒ Object
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
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
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_results ⇒ Object
132 133 134 |
# File 'lib/char_tree/dictionary.rb', line 132 def total_results @results.count end |
#use_set ⇒ Object
Search
76 77 78 |
# File 'lib/char_tree/dictionary.rb', line 76 def use_set @use_set = true end |
#use_set? ⇒ Boolean
84 85 86 |
# File 'lib/char_tree/dictionary.rb', line 84 def use_set? @use_set end |