Class Tx::Index
In: tx.rb
Parent: Object

A class to access an index.

Methods

Included Modules

Enumerable

External Aliases

num_keys -> size

Attributes

encoding  [R]  Encoding object specified in new. nil in Ruby 1.8.

Public Class methods

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.

Public Instance methods

common_prefix_search(str, pos= 0, len= -1)

Alias for search_prefixes

Iterates over all words in the index.

Returns error log of index loading.

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.)

include?(str, pos= 0, len= -1)

Alias for include

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

Returns the number of words in the index.

predictive_search(str, pos= 0, len= -1, limit= 0)

Alias for search_expansions

Returns operation log of index loading.

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.

Enumerates all words in the index which are prefixes of str. str is also returned if str itself is in the index.

e.g. index.search_prefixes("hoge") #=> ["ho", "hoge"]

If pos and len are specified, searches str[pos, len] instead of whole str. (Negative pos is not supported.)

Returns all words in the index as an Array.

[Validate]