lib/makit/indexer.rb in makit-0.0.53 vs lib/makit/indexer.rb in makit-0.0.54

- old
+ new

@@ -1,56 +1,56 @@ -# 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 +# 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