Class: PMS::Index

Inherits:
Object
  • Object
show all
Includes:
I18n
Defined in:
lib/pms/index.rb

Constant Summary collapse

TOKEN_RE =
%r{\w+}
DEFAULT_LSI =
0.05

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(input, options = {}) ⇒ Index

Returns a new instance of Index.



39
40
41
42
43
44
# File 'lib/pms/index.rb', line 39

def initialize(input, options = {})
  @input = input.respond_to?(:each) ? input : input.is_a?(String) ?
    input.each_line : raise(ArgumentError, 'input must implement #each')

  build_index(options)
end

Instance Attribute Details

#entriesObject (readonly)

Returns the value of attribute entries.



37
38
39
# File 'lib/pms/index.rb', line 37

def entries
  @entries
end

#indexObject (readonly)

Returns the value of attribute index.



37
38
39
# File 'lib/pms/index.rb', line 37

def index
  @index
end

#inputObject (readonly)

Returns the value of attribute input.



37
38
39
# File 'lib/pms/index.rb', line 37

def input
  @input
end

Instance Method Details

#doc(doc_num) ⇒ Object Also known as: []



74
75
76
# File 'lib/pms/index.rb', line 74

def doc(doc_num)
  documents([doc_num]).first
end

#doc_nums(token) ⇒ Object Also known as: results



61
62
63
# File 'lib/pms/index.rb', line 61

def doc_nums(token)
  doc_nums_with_positions(token).keys
end

#doc_nums_with_positions(token) ⇒ Object Also known as: results_with_positions



46
47
48
49
50
51
52
53
54
55
56
57
# File 'lib/pms/index.rb', line 46

def doc_nums_with_positions(token)
  case token
    when String
      index[mangle_token(token)]
    when Regexp
      index.each_with_object({}) { |(key, value), hash|
        hash.update(value) { |_, old, new| old | new } if key =~ token
      }
    else
      raise TypeError, "String or Regexp expected, got #{token.class}"
  end.each_value(&:compact!)
end

#documents(doc_nums = default = true) ⇒ Object Also known as: matches



67
68
69
70
# File 'lib/pms/index.rb', line 67

def documents(doc_nums = default = true)
  @documents ||= get_documents
  default ? @documents : @documents.values_at(*doc_nums)
end