14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
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
|
# File 'lib/detergent/node_scorer.rb', line 14
def score(node)
return node.readify_score unless node.readify_score.nil?
return 0 unless node.element?
score = 0
tag = node.name.downcase
score += 100 if tag == 'article'
score += 50 if tag == 'main'
score += 25 if node['role'].to_s.downcase == 'main'
paragraph_count = node.xpath('.//p').count
score += paragraph_count * 5
begin
text_content = node.xpath('.//text()').map(&:text).join
text_length = text_content.strip.length
score += (text_length / 100).to_i rescue StandardError
text_length = 0
end
link_count = node.xpath('.//a').count
if text_length > 0
link_density = (link_count.to_f / (text_length / 100.0))
score -= (link_density * 10).to_i end
media_count = node.xpath('.//img | .//picture | .//figure').count
score += media_count * 10
begin
long_paragraphs = node.xpath('.//p').select { |p| p.text.strip.length > 100 }.count
score += long_paragraphs * 15
rescue StandardError
end
score += node.xpath('.//blockquote').count * 10
score += node.xpath('.//ul | .//ol').count * 5
classes = node['class'].to_s.downcase
ids = node['id'].to_s.downcase
score -= 50 if classes.include?('sidebar') || ids.include?('sidebar')
score -= 50 if classes.include?('comment') || ids.include?('comment')
score -= 70 if classes.include?('ad') || ids.include?('ad')
score -= 70 if classes.include?('social') || ids.include?('social')
node.readify_score = [score, 0].max end
|