rambling-trie 0.0.1
Sign up to get free protection for your applications and to get access to all the features.
- data/lib/rambling-trie.rb +3 -0
- data/lib/trie.rb +17 -0
- data/lib/trie_node.rb +64 -0
- metadata +47 -0
data/lib/trie.rb
ADDED
@@ -0,0 +1,17 @@
|
|
1
|
+
module Rambling
|
2
|
+
class Trie < TrieNode
|
3
|
+
def initialize(filename = nil)
|
4
|
+
super(nil)
|
5
|
+
|
6
|
+
@filename = filename
|
7
|
+
add_all_nodes if filename
|
8
|
+
end
|
9
|
+
|
10
|
+
private
|
11
|
+
def add_all_nodes
|
12
|
+
File.open(@filename) do |file|
|
13
|
+
file.readlines.each { |word| add_branch_from(word.chomp) }
|
14
|
+
end
|
15
|
+
end
|
16
|
+
end
|
17
|
+
end
|
data/lib/trie_node.rb
ADDED
@@ -0,0 +1,64 @@
|
|
1
|
+
module Rambling
|
2
|
+
class TrieNode
|
3
|
+
attr_reader :letter, :children
|
4
|
+
|
5
|
+
def initialize(word)
|
6
|
+
@letter = nil
|
7
|
+
@is_terminal = false
|
8
|
+
@children = {}
|
9
|
+
|
10
|
+
unless word.nil?
|
11
|
+
@letter = word.slice!(0)
|
12
|
+
@is_terminal = word.empty?
|
13
|
+
add_branch_from(word)
|
14
|
+
end
|
15
|
+
|
16
|
+
end
|
17
|
+
|
18
|
+
def terminal=(terminal)
|
19
|
+
@is_terminal = terminal
|
20
|
+
end
|
21
|
+
|
22
|
+
def terminal?
|
23
|
+
@is_terminal
|
24
|
+
end
|
25
|
+
|
26
|
+
def [](key)
|
27
|
+
@children[key]
|
28
|
+
end
|
29
|
+
|
30
|
+
def has_key?(key)
|
31
|
+
@children.has_key?(key)
|
32
|
+
end
|
33
|
+
|
34
|
+
def add_branch_from(word)
|
35
|
+
unless word.empty?
|
36
|
+
first_letter = word.slice(0)
|
37
|
+
|
38
|
+
if @children.has_key?(first_letter)
|
39
|
+
word.slice!(0)
|
40
|
+
@children[first_letter].add_branch_from(word)
|
41
|
+
else
|
42
|
+
@children[first_letter] = TrieNode.new(word)
|
43
|
+
end
|
44
|
+
end
|
45
|
+
end
|
46
|
+
|
47
|
+
def has_branch_tree?(word)
|
48
|
+
return true if word.empty?
|
49
|
+
passes_condition(word) { |node, sliced_word| node.has_branch_tree?(sliced_word) }
|
50
|
+
end
|
51
|
+
|
52
|
+
def is_word?(word)
|
53
|
+
return true if word.empty? and terminal?
|
54
|
+
passes_condition(word) { |node, sliced_word| node.is_word?(sliced_word) }
|
55
|
+
end
|
56
|
+
|
57
|
+
private
|
58
|
+
def passes_condition(word, &block)
|
59
|
+
first_letter = word.slice!(0)
|
60
|
+
return block.call(@children[first_letter], word) if @children.has_key?(first_letter)
|
61
|
+
false
|
62
|
+
end
|
63
|
+
end
|
64
|
+
end
|
metadata
ADDED
@@ -0,0 +1,47 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: rambling-trie
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.1
|
5
|
+
prerelease:
|
6
|
+
platform: ruby
|
7
|
+
authors:
|
8
|
+
- Rambling Labs
|
9
|
+
autorequire:
|
10
|
+
bindir: bin
|
11
|
+
cert_chain: []
|
12
|
+
date: 2012-02-04 00:00:00.000000000Z
|
13
|
+
dependencies: []
|
14
|
+
description: A custom trie implementation
|
15
|
+
email: development@ramblinglabs.com
|
16
|
+
executables: []
|
17
|
+
extensions: []
|
18
|
+
extra_rdoc_files: []
|
19
|
+
files:
|
20
|
+
- lib/rambling-trie.rb
|
21
|
+
- lib/trie.rb
|
22
|
+
- lib/trie_node.rb
|
23
|
+
homepage: http://rubygems.org/gems/rambling-trie
|
24
|
+
licenses: []
|
25
|
+
post_install_message:
|
26
|
+
rdoc_options: []
|
27
|
+
require_paths:
|
28
|
+
- lib
|
29
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
30
|
+
none: false
|
31
|
+
requirements:
|
32
|
+
- - ! '>='
|
33
|
+
- !ruby/object:Gem::Version
|
34
|
+
version: '0'
|
35
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
36
|
+
none: false
|
37
|
+
requirements:
|
38
|
+
- - ! '>='
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: '0'
|
41
|
+
requirements: []
|
42
|
+
rubyforge_project:
|
43
|
+
rubygems_version: 1.8.10
|
44
|
+
signing_key:
|
45
|
+
specification_version: 3
|
46
|
+
summary: Rambling Trie
|
47
|
+
test_files: []
|