Module: ProperCaser

Defined in:
lib/proper_caser.rb,
lib/proper_caser/version.rb

Constant Summary collapse

VERSION =
"0.0.1"

Class Method Summary collapse

Class Method Details

.best_guess(query) ⇒ Object



6
7
8
# File 'lib/proper_caser.rb', line 6

def self.best_guess(query)
  capitalization_counts(query).first[0]
end

.capitalization_counts(query) ⇒ Object



10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
# File 'lib/proper_caser.rb', line 10

def self.capitalization_counts(query)
  regexp = Regexp.new(query, 'i')

  matches = Hash.new(0)

  # search google for the quoted query
  Google::Search::Web.new(query: %("#{query}")).each do |result|
    text = Nokogiri::HTML(result.content).content.gsub(/\s+/, ' ')

    # match the content against our query regexp
    text.scan(regexp).each do |match|
      # and increment the count for each match
      matches[match] += 1
    end
  end

  Hash[ matches.sort_by(&:last).reverse ]
end