Class: Atlasq::Util::WordMap

Inherits:
Object
  • Object
show all
Defined in:
lib/atlasq/util/word_map.rb

Overview

This data structure provides partial match support for words in sentences.

For example, if we take a country like “Bonaire, Sint Eustatius and Saba” the searches “Bonaire”, “Sint Eustatius” and “saba bonaire” should all match while the searches “bonaire france” and “saba bolivia” should not because they include words not found in the original sentence.

Instance Method Summary collapse

Constructor Details

#initialize(index:) ⇒ WordMap

Returns a new instance of WordMap.

Parameters:



16
17
18
# File 'lib/atlasq/util/word_map.rb', line 16

def initialize(index:)
  @index = index
end

Instance Method Details

#search(search_term) ⇒ Array<String>

Search for ids that include all of the search term words.

Parameters:

Returns:

  • (Array<String>)

    list of ids



24
25
26
27
28
29
30
31
# File 'lib/atlasq/util/word_map.rb', line 24

def search(search_term)
  search_term
    .then(&Util::String.method(:word_split))
    .uniq
    .map { |word| @index.fetch(word, []) }
    .inject(&:intersection)
    .sort
end