Class: Seo::KeywordAnalyzer
- Inherits:
-
BaseService
- Object
- BaseService
- Seo::KeywordAnalyzer
- Defined in:
- app/services/seo/keyword_analyzer.rb
Defined Under Namespace
Classes: Result
Instance Method Summary collapse
- #analyze_keywords(input_data, target_keywords) ⇒ Object
- #process(input_data, target_keywords) ⇒ Object
Methods inherited from BaseService
#initialize, #log_debug, #log_error, #log_info, #log_warning, #logger, #options, #tagged_logger
Constructor Details
This class inherits a constructor from BaseService
Instance Method Details
#analyze_keywords(input_data, target_keywords) ⇒ Object
27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 |
# File 'app/services/seo/keyword_analyzer.rb', line 27 def analyze_keywords(input_data, target_keywords) results = Hash.new { |hash, key| hash[key] = {} } total_keyword_use_count = 0 unused_keywords = [] target_keywords.each do |keyword| keyword_count = 0 input_data.each do |field, string| count = string.scan(/#{keyword}/i).size results[keyword][field] = count results[keyword][:total] = results[keyword][:total].to_i + count keyword_count += count end total_keyword_use_count += keyword_count unused_keywords << keyword if keyword_count == 0 end [results, total_keyword_use_count, unused_keywords] end |
#process(input_data, target_keywords) ⇒ Object
6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 |
# File 'app/services/seo/keyword_analyzer.rb', line 6 def process(input_data, target_keywords) return Result.new(keyword_analysis: {}, status: :skipped, total_keyword_use_count: 0, total_keyword_in_input: 0, percentage_hit_rate: 0.0, unused_keywords: []) if input_data.blank? @input_data = input_data.is_a?(String) ? { input_string: input_data } : input_data @input_data = @input_data.transform_values(&:downcase) @target_keywords = target_keywords @target_keywords = @target_keywords.split(/[,;]/) if @target_keywords.is_a?(String) @target_keywords = @target_keywords.map(&:strip).compact_blank.map(&:downcase) if @target_keywords.is_a?(Array) @keyword_analysis, total_keyword_use_count, unused_keywords = analyze_keywords(@input_data, @target_keywords) total_keyword_in_input = @target_keywords.size percentage_hit_rate = (total_keyword_use_count.to_f / total_keyword_in_input) * 100 Result.new( keyword_analysis: @keyword_analysis, status: :ok, total_keyword_use_count: total_keyword_use_count, total_keyword_in_input: total_keyword_in_input, percentage_hit_rate: percentage_hit_rate, unused_keywords: unused_keywords ) end |