Module: Anonymize::GoogleNews

Defined in:
lib/anonymize/google_news.rb

Class Method Summary collapse

Class Method Details

.longest_words(text) ⇒ Object



17
18
19
# File 'lib/anonymize/google_news.rb', line 17

def longest_words(text)
  strip_html(text).scan(/\w+/).uniq.sort_by(&:length)
end


5
6
7
8
9
10
11
12
13
14
15
# File 'lib/anonymize/google_news.rb', line 5

def related_news(text, char_count = text.length)
  words = longest_words(text)
  new_text = ""
  until new_text.length >= char_count
    Google::Search::News.new(:query => [words.pop, words.pop].join(' ')).each do |result|
      new_text << utf8_to_ascii(strip_html(result.content))
      break if new_text.length >= char_count
    end
  end
  new_text[0..(char_count-1)]
end

.strip_html(string) ⇒ Object



25
26
27
# File 'lib/anonymize/google_news.rb', line 25

def strip_html(string)
  string.gsub(/<[^>]+>/,'')
end

.utf8_to_ascii(string) ⇒ Object



21
22
23
# File 'lib/anonymize/google_news.rb', line 21

def utf8_to_ascii(string)
  string.encode('ascii', 'utf-8', :undef => :replace, :invalid => :replace, :replace => '')
end