Class: SiteClassifier::Extractor

Inherits:
Object
  • Object
show all
Includes:
HTTParty
Defined in:
lib/site_classifier/extractor.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(url, tags, word_hash, description, lang) ⇒ Extractor

Returns a new instance of Extractor.



7
8
9
10
11
12
13
# File 'lib/site_classifier/extractor.rb', line 7

def initialize(url, tags, word_hash, description, lang)
  @url = url
  @tags = tags
  @description = description
  @word_frequency = word_hash
  @lang = lang.downcase
end

Instance Attribute Details

#descriptionObject

Returns the value of attribute description.



5
6
7
# File 'lib/site_classifier/extractor.rb', line 5

def description
  @description
end

#langObject

Returns the value of attribute lang.



5
6
7
# File 'lib/site_classifier/extractor.rb', line 5

def lang
  @lang
end

#tagsObject

Returns the value of attribute tags.



5
6
7
# File 'lib/site_classifier/extractor.rb', line 5

def tags
  @tags
end

#urlObject

Returns the value of attribute url.



5
6
7
# File 'lib/site_classifier/extractor.rb', line 5

def url
  @url
end

#word_frequencyObject

Returns the value of attribute word_frequency.



5
6
7
# File 'lib/site_classifier/extractor.rb', line 5

def word_frequency
  @word_frequency
end

Class Method Details

.debug(string) ⇒ Object



15
16
17
18
19
# File 'lib/site_classifier/extractor.rb', line 15

def self.debug(string)
  if SiteClassifier.configuration.debug?
    puts "#{Time.now.to_i} - #{string}"
  end
end

.parse_site(url = "") ⇒ Object



87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
# File 'lib/site_classifier/extractor.rb', line 87

def self.parse_site(url = "")
  return if url == "" || url.nil?

  debug("getting #{url}")
  html = Nokogiri::HTML(self.get(url).parsed_response)

  tags = []
  description = nil
  word_hash = {}
  page_lang = "auto"

  begin
    page_lang = html.search("html").first["lang"].to_s.slice(0..1)
    debug("found lang in html tag - #{page_lang}")
  rescue
  end

  begin
    page_lang = html.search("html").first["xml:lang"].to_s.slice(0..1)
    debug("found lang in html tag (xml:lang) - #{page_lang}")
  rescue
  end

  begin
    tags = html.search('meta[name="keywords"]').first["content"].split(",").collect(&:strip).collect(&:downcase)
    debug("Tags - #{tags.inspect}")
  rescue
    debug("no tags found")
  end

  if tags.empty?
    begin
      tags = html.search('meta[property="keywords"]').first["content"].split(",").collect(&:strip).collect(&:downcase)
      debug("Tags - #{tags.inspect}")
    rescue
      debug("no tags found")
    end
  end
  begin
    description = html.search('meta[name="description"]').first["content"]
    debug("Decription meta found")
  rescue
  end

  if description.nil?
    begin
      description = html.search('meta[property="og:description"]').first["content"]
      debug("Facebook og:description found")
    rescue
    end
  end

  if description.nil?
    begin
      description = html.search('meta[name="og:description"]').first["content"]
      debug("Facebook og:description found")
    rescue
    end
  end

  if tags.empty?
    debug("no tags, parsing body")
    word_hash = Hash.new(0)
    all_text = []
    # all_text = html.search("p").collect {|p| p.text.strip }.collect {|text| text.split.collect(&:strip)}.flatten.reject {|word| word.size < 4}
    # debug("p's extracts - #{all_text.inspect}")
    if all_text.empty?
      all_text = html.search("div").collect {|p| p.text.strip }.collect {|text| text.split.collect(&:strip)}.flatten.reject {|word| word.size < 4}
      debug("divs extracts - #{all_text.inspect}")
    end
    all_text += description.to_s.split

    all_text.flatten.each do |word|
      word_hash[word] += 1
    end
    debug("final word hash - #{word_hash.inspect}")
    word_hash.reject! {|k,v| v < 3 || k.size == 1 || k.include?(".") || k.include?("'") || k.include?("(") || k.include?(":") || k.include?("]")}
    
  end
  self.new(url, tags, word_hash, description, page_lang)
end

Instance Method Details

#most_significantObject

Extract most significant tags



31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
# File 'lib/site_classifier/extractor.rb', line 31

def most_significant
  most_sig = []
  # if !description.nil?
  #   if tags.any?
  #     most_sig = tags.select {|tag| self.description.downcase.include?(tag)}.collect {|tag| tag.singularize }
  #   else
  #     most_sig = word_frequency.keys.select {|tag| self.description.downcase.include?(tag)}.collect {|tag| tag.singularize }
  #   end
  # end

  description.to_s.split.each do |word|
    self.word_frequency[word] ||= 0
    self.word_frequency[word] += 1
  end

  if most_sig.empty?
    most_sig = self.word_frequency.reject {|k,v| v < 3}.keys
    most_sig.flatten!
  end

  if description && tags.any?
    tags.each do |tag|
      if description.include?(tag)
        most_sig << tag.singularize
      end
    end
  end
  
  most_sig.uniq!

  self.validate_lang

  if SiteClassifier.translate_tags?
    begin
      if self.lang == "auto"
        @lang = EasyTranslate.detect(most_sig.first, key: SiteClassifier.configuration.google_translate_api_key)
      end
      EasyTranslate.translate(most_sig, from: self.lang, to: :en, key: SiteClassifier.configuration.google_translate_api_key)
    rescue
      return most_sig
    end
  else
    return most_sig
  end
end

#to_hashObject



77
78
79
80
81
82
83
84
85
# File 'lib/site_classifier/extractor.rb', line 77

def to_hash
  {
    most_significant: most_significant,
    language: self.lang,
    url: url,
    tags: tags,
    description: description
  }
end

#validate_langObject

Normalize site language



22
23
24
25
26
27
28
# File 'lib/site_classifier/extractor.rb', line 22

def validate_lang
  if EasyTranslate::LANGUAGES.keys.include?(@lang)
    @lang
  else
    self.lang = "auto"
  end
end