Class: Hits::Hits

Inherits:
Object
  • Object
show all
Defined in:
lib/hits/hits.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(graph, use_weights = true) ⇒ Hits

Returns a new instance of Hits.



8
9
10
11
12
13
14
15
16
17
# File 'lib/hits/hits.rb', line 8

def initialize(graph, use_weights = true)
  @graph = graph
  @use_weights = use_weights
  @hub_scores = {}
  @authority_scores = {}
  @graph.each_vertex do |vertex|
    @hub_scores[vertex] = 1.0
    @authority_scores[vertex] = 1.0
  end
end

Instance Attribute Details

#authority_scoresObject (readonly)

Returns the value of attribute authority_scores.



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

def authority_scores
  @authority_scores
end

#hub_scoresObject (readonly)

Returns the value of attribute hub_scores.



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

def hub_scores
  @hub_scores
end

Instance Method Details

#compute_hits(iterations = 25) ⇒ Object



19
20
21
22
23
24
25
26
27
28
29
30
# File 'lib/hits/hits.rb', line 19

def compute_hits(iterations = 25)
  (1..iterations).each do
    @graph.each_vertex do |vertex|
      authority_score = @graph.in_links(vertex).inject(0.0) { |sum, vertex| sum + @hub_scores[vertex] } if @graph.in_links(vertex)
      hub_score = @graph.out_links(vertex).inject(0.0) { |sum, vertex| sum + @authority_scores[vertex] } if @graph.out_links(vertex)
      @authority_scores[vertex] = authority_score || 0.0
      @hub_scores[vertex] = hub_score || 0.0
    end
    normalize_scores
  end
  apply_weighting if @use_weights
end

#top_authority_scores(how_many = 5) ⇒ Object



36
37
38
# File 'lib/hits/hits.rb', line 36

def top_authority_scores(how_many=5)
  @authority_scores.sort_by { |k,v| v }.map { |v| v[0] }.reverse.first(how_many)
end

#top_hub_scores(how_many = 5) ⇒ Object



32
33
34
# File 'lib/hits/hits.rb', line 32

def top_hub_scores(how_many=5)
  @hub_scores.sort_by { |k,v| v }.map { |v| v[0] }.reverse.first(how_many)
end