Sha256: dbaaabc9137964c096756c3615fc16a0beba1ed0d2d54f9931079a9904191725

Contents?: true

Size: 1.58 KB

Versions: 1

Compression:

Stored size: 1.58 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

1 entries across 1 versions & 1 rubygems

Version Path
makit-0.0.36 lib/makit/indexer.rb