aurelian-ruby-ahocorasick 0.3.3 → 0.4.1

Sign up to get free protection for your applications and to get access to all the features.
@@ -75,7 +75,7 @@ rb_kwt_init(VALUE self)
75
75
  * Document-method: make
76
76
  * call-seq: make
77
77
  *
78
- * It freezes the current KeywordTree. After this point, the tree will not accept any new entries.
78
+ * It freezes the current KeywordTree.
79
79
  *
80
80
  * ==== Note: This method is called internally by search
81
81
  *
@@ -93,14 +93,20 @@ rb_kwt_make(VALUE self)
93
93
  struct kwt_struct_data *kwt_data;
94
94
  KeywordTree(self, kwt_data);
95
95
 
96
- ac_prep( kwt_data->tree );
97
- kwt_data->is_frozen = 1;
98
- return self;
96
+ if(kwt_data->is_frozen == 1)
97
+ return Qtrue;
98
+
99
+ if(ac_prep( kwt_data->tree ) == 1) {
100
+ kwt_data->is_frozen = 1;
101
+ return Qtrue;
102
+ }
103
+
104
+ rb_raise(rb_eRuntimeError, "Cannot freeze the tree");
99
105
  }
100
106
 
101
107
  /*
102
- * Document-method: search
103
- * call-seq: search
108
+ * Document-method: find_all
109
+ * call-seq: find_all
104
110
  *
105
111
  * Search the current tree.
106
112
  *
@@ -142,7 +148,8 @@ rb_kwt_find_all(int argc, VALUE *argv, VALUE self)
142
148
  KeywordTree(self, kwt_data);
143
149
  // freeze the tree, if not already
144
150
  if(kwt_data->is_frozen == 0) {
145
- ac_prep( kwt_data->tree );
151
+ if(ac_prep( kwt_data->tree ) == 0)
152
+ rb_raise(rb_eRuntimeError, "Cannot freeze the tree");
146
153
  kwt_data->is_frozen = 1;
147
154
  }
148
155
  // prepare the return value
@@ -165,7 +172,8 @@ rb_kwt_find_all(int argc, VALUE *argv, VALUE self)
165
172
  rb_ary_push( v_results, v_result );
166
173
  free(result);
167
174
  }
168
- // TODO: maybe the Tree can be re-opened to add new items to dictionary
175
+ // reopen the tree
176
+ kwt_data->is_frozen= 0;
169
177
  return v_results;
170
178
  }
171
179
 
@@ -170,6 +170,13 @@ describe KeywordTree do
170
170
  lambda{kwt << "foo"}.should raise_error(RuntimeError)
171
171
  end
172
172
 
173
+ it "should work to add entries on the tree after a search" do
174
+ kwt= KeywordTree.from_file File.dirname(__FILE__) + "/data/dict0.txt"
175
+ kwt.find_all("foo-bar not found")
176
+ kwt.add_string("not found")
177
+ kwt.find_all("foo-bar not found").size.should == 1
178
+ end
179
+
173
180
  end
174
181
 
175
182
  describe "Benchmarks. Loading from a file" do
@@ -187,7 +194,7 @@ describe KeywordTree do
187
194
  load_time= Time.now
188
195
  results= k.find_all( File.read( File.dirname(__FILE__) + "/data/melville-moby_dick.txt" ) )
189
196
  puts "\n%d words re-loaded in %s seconds.\nGot %d results in %s seconds" % [k.size, (load_time - start), results.size, (Time.now-load_time)]
190
- (Time.now-load_time).should < 1.2
197
+ (Time.now-load_time).should < 1.3
191
198
  end
192
199
  end
193
200
 
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: aurelian-ruby-ahocorasick
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.3.3
4
+ version: 0.4.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Aurelian Oancea