Sha256: cda30dacbefaa830395f6417c2e1ac499d306e61fd06fa2fef0f3ed4d8a83543

Contents?: true

Size: 1.51 KB

Versions: 12

Compression:

Stored size: 1.51 KB

Contents

# frozen_string_literal: true

# This module provides classes for the Makit gem.
module Makit
  # This class provide methods for indexing objects.
  #
  class Indexer
    attr_accessor :keywords_index # Hash of string key to string[] of keyword
    attr_accessor :protoc_json_serializer

    def initialize
      @keywords_index = Hash.new
      @protoc_json_serializer = Makit::Serializer::new(Makit::Proto3Formats::JSON)
    end

    def index(key, item)
      # item must be serializable to json
      keywords = []
      hash = JSON.parse(item.to_json)
      hash.each do |key, value|
        value = value.to_s.downcase
        if (value.length >= 3 && !keywords.include?(value))
          keywords << value
        end
      end
      keywords.each do |keyword|
        if !@keywords_index.key?(keyword)
          @keywords_index[keyword] = []
        end
        @keywords_index[keyword] << key unless @keywords_index[keyword].include?(key)
      end
    end

    def search(query)
      keys = []
      # todo, remove terms less that length of 3
      terms = query.downcase.split(" ").reject { |term| term.length < 3 }
      keywords_index.each do |key, value| #{|kvp|
        if (get_match_count(terms, value) == terms.length)
          keys << key
        end
      end
      keys
    end

    def get_match_count(terms, keywords)
      match_count = 0
      terms.each { |term|
        if (keywords.include?(term))
          match_count += 1
        end
      }
      return match_count
    end
  end # class Indexer
end # module Makit

Version data entries

12 entries across 12 versions & 1 rubygems

Version Path
makit-0.0.57 lib/makit/indexer.rb
makit-0.0.53 lib/makit/indexer.rb
makit-0.0.52 lib/makit/indexer.rb
makit-0.0.51 lib/makit/indexer.rb
makit-0.0.50 lib/makit/indexer.rb
makit-0.0.49 lib/makit/indexer.rb
makit-0.0.47 lib/makit/indexer.rb
makit-0.0.46 lib/makit/indexer.rb
makit-0.0.45 lib/makit/indexer.rb
makit-0.0.44 lib/makit/indexer.rb
makit-0.0.42 lib/makit/indexer.rb
makit-0.0.41 lib/makit/indexer.rb