Class: CGDocumentGraph

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(document, stopWordsDoc) ⇒ CGDocumentGraph

Returns a new instance of CGDocumentGraph.



8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
# File 'lib/DocumentGraph.rb', line 8

def initialize(document, stopWordsDoc)
  @document = document
  @stopWords = stopWordsDoc
  @adjacencyGraph = RGL::DirectedAdjacencyGraph.new()
  @wordArray = []

  #Write the non-stop words into a word array.
  for i in 0 ... @document.docArray.length() - 1
    word = @document.docArray[i]
    if word != nil
      word = CGWordOps.removePunctuation(word)
      if !@stopWords.docArray.include?(word.downcase)
        @wordArray << word
      end
    end
  end

  for i in 0 ... @wordArray.length() - 2
    word1 = @wordArray[i]
    word2 = @wordArray[i+1]
	
    @adjacencyGraph.add_edge(word1, word2)
  end
end

Instance Attribute Details

#adjacencyGraphObject

Returns the value of attribute adjacencyGraph.



91
92
93
# File 'lib/DocumentGraph.rb', line 91

def adjacencyGraph
  @adjacencyGraph
end

Instance Method Details

#buildKeywords(delta, thresh) ⇒ Object



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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
# File 'lib/DocumentGraph.rb', line 33

def buildKeywords(delta, thresh)
  verts = @adjacencyGraph.vertices()
  num_verts = @adjacencyGraph.size()
  q_array = [];
  for i in 0 .. (num_verts - 1)
    w = verts[i]
    n = @adjacencyGraph.out_degree(w)
    if(n != 0)
      q = Array.new(num_verts,0)
      adjVerts = @adjacencyGraph.adjacent_vertices(w)
      for j in 0 ... adjVerts.size() - 1
        v = adjVerts[j]
        index = verts.index(v)
        if (index != nil)
          q[index] = 1.0/n
        end
      end
      q_array << q
    else
      q = Array.new(num_verts, 1.0/num_verts)
      q_array << q
    end
  end

  prM = Matrix.rows(q_array)

  prArray = Array.new(num_verts, 1.0/num_verts);
  pr = Matrix.columns([prArray])

  deltaArray = Array.new(num_verts, (1.0-delta)/num_verts)
  deltaM = Matrix.columns([deltaArray])

  for i in 0 .. 10
    pr = deltaM + delta * prM * pr
  end

  prArray = (pr.column_vectors[0].to_a)

  prHash = Hash.new()
  for i in 0 .. num_verts - 1
    prHash[prArray[i]] = verts[i]
  end

  sortedHash = prHash.sort()
  sortedHash = sortedHash.reverse()

  if(thresh >= num_verts)
    thresh = num_verts - 1
  end
  keywords = Array.new()

  for i in 0 .. thresh
    keywords << (sortedHash[i])[1]
  end

  keywords
end