Class | Tx::Index |
In: |
tx.rb
|
Parent: | Object |
A class to access an index.
num_keys | -> | size |
encoding | [R] | Encoding object specified in new. nil in Ruby 1.8. |
Loads an index from a file named file_name. The index file can be generated with Tx::Builder.
e.g. index = Tx::Index.new("test.index")
Ruby 1.9 only: You can specify Encoding object as second parameter. It is used as encoding of result of search_prefixes, etc. Default is Encoding.default_internal (if it‘s nil, Encoding::UTF_8). Note that offsets (pos, len) returned by and given to Tx::Index is in byte unit, not character unit, even if encoding is specified.
Replaces all occurences of strings in the index in str using return value of the given block. Block is called with the string found and its position.
e.g.
index.gsub("hogefugabar"){ |s, i| s.upcase } #=> "HOGEfugaBAR"
Returns true if str is in the index.
e.g. index.include("hoge") #=> true if "hoge" is in the index
If pos and len are specified, searches str[pos, len] instead of whole str. (Negative pos is not supported.)
Searches the longest word in the index which is a prefix of str and returns the length of the word. If str itself is in the index, returns str.length. If no such word is found, returns -1.
e.g. index.longest_prefix("hoge") #=> 2 if "ho" is in the index
If pos and len are specified, searches str[pos, len] instead of whole str. (Negative pos is not supported.)
If match_prefix is true, searches a word which has the longest prefix in common with str and returns the length of the common prefix.
e.g. index.longest_prefix("hoge", 0, -1, true) #=> 3 if "hoga" is in the index
Finds all occurences of strings in the index from str, and returns Array of pair of the string and its position.
e.g. index.scan("hogefugabar") #=> [["hoge", 0], ["bar", 8]]
If block is given, call it for each string and its position.
e.g.
index.scan("hogefugabar"){ |s, i| p [s, i] } #=> ["hoge", 0] ["bar", 8]
Enumerates all words in the index which begin with str. str is also returned if str itself is in the index.
e.g. index.search_expansions("hoge") #=> ["hoge", "hogeee", "hogeshi"]
If pos and len are specified, searches str[pos, len] instead of whole str. (Negative pos is not supported.)
If limit is non-zero, returns at most limit words.