pagerank 0.1.0 → 0.2.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: fea318e5bdfcea114471f1aeb5223f87435138b2
4
- data.tar.gz: d1f3dfa1c3805cbcc231bbda0fa52623cd74f5f5
3
+ metadata.gz: e6d3c814c981107ab386ee67e4fe040f7a946d52
4
+ data.tar.gz: 319147d0b0ec24842f96abc3a82b97e4bc2b65d3
5
5
  SHA512:
6
- metadata.gz: 587e8f947ab8fc65f65bc9a7cfa041773846ef2537bb31727f1f28edf9f86d326d225d73692ba65e4e7151aeb434bee9bf464994bf9ef144067cd500272e046b
7
- data.tar.gz: a4e95f8bdf963cdfbb2c816b1389797e10bcb330cae254104bcd19b6a1f9bf6fcb925e4786af85b5d94137174687d4dd611a1ae4f53eb2532192aee852f11e95
6
+ metadata.gz: 9d30de7e5d3a408896f24897ec0f1f9627d71085bd1a70eedbafeee23edcc8160e59c09cad43f0e7aea88488d670e7dbd3631f951b01a3fd865f2728e59040be
7
+ data.tar.gz: f6e9feb4d7cbb1aa66ab887f7a986305dc79c1678e944c6b957434688456ace4d067f11f308c89bc11ab7eb617db8d1973ffabd62c5cac5f830456ff1f51eddc
data/lib/pagerank.rb CHANGED
@@ -1,4 +1,5 @@
1
1
  require "pagerank/version"
2
+ require "pagerank/pagerank"
2
3
 
3
4
  module Pagerank
4
5
  # Your code goes here...
@@ -0,0 +1,72 @@
1
+ =begin
2
+ u : webpage
3
+ F_u : the set of pages u points to
4
+ B_u : the set of pages that point to u
5
+ N_u : |F_u|
6
+ c : use for normalization
7
+ R(u): c * \sum_{v} \in{B_u} R(v)/N_v
8
+ =end
9
+
10
+ module PageRank
11
+
12
+ class Main
13
+ attr_reader :nodes, :follow_links, :score
14
+
15
+ # creat instance
16
+ def initialize
17
+ @nodes = []
18
+ @score = {}
19
+ @follow_links = {}
20
+ @back_links = {}
21
+ @iterates = 0
22
+ end
23
+
24
+ # divide among initial value
25
+ def initial_value
26
+ @nodes.each do |node|
27
+ @score[node] = 1.0 / @nodes.size
28
+ end
29
+ end
30
+
31
+ # add node into nodes and edge into follow_links
32
+ def add from, to
33
+ @nodes << from unless @nodes.include?(from)
34
+ @nodes << to unless @nodes.include?(to)
35
+ @follow_links[from] ||= []
36
+ @follow_links[from] << to
37
+ @back_links[to] ||= []
38
+ @back_links[to] << from
39
+ end
40
+
41
+ # calculate pagerank
42
+ def calculate
43
+ if @score == {}
44
+ initial_value
45
+ end
46
+
47
+ @score.each do |from, value|
48
+ if @back_links[from]
49
+ score = @score[from]
50
+ @back_links[from].each do |to|
51
+ score += @score[to]
52
+ end
53
+ @score[from] = score
54
+ else
55
+ @score[from] = 0
56
+ end
57
+ end
58
+
59
+ @iterates += 1
60
+ # return true if convergence?
61
+ return calculate unless @iterates == 1
62
+ return true
63
+ end
64
+
65
+ # check convergence
66
+ def convergence? convergence=0.01
67
+ return (convergence < 1.0)
68
+ end
69
+
70
+ end
71
+
72
+ end
@@ -1,3 +1,3 @@
1
1
  module Pagerank
2
- VERSION = "0.1.0"
2
+ VERSION = "0.2.0"
3
3
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: pagerank
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.0
4
+ version: 0.2.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - himkt
@@ -70,6 +70,7 @@ files:
70
70
  - bin/console
71
71
  - bin/setup
72
72
  - lib/pagerank.rb
73
+ - lib/pagerank/pagerank.rb
73
74
  - lib/pagerank/version.rb
74
75
  - pagerank.gemspec
75
76
  homepage: https://github.com/himkt/pagerank