Class: GraphRank::Sentences

Inherits:
TextRank show all
Defined in:
lib/graph-rank/sentences.rb

Overview

for unsupervised sentence extraction.

Constant Summary

Constants inherited from TextRank

TextRank::StopWords

Instance Attribute Summary

Attributes inherited from TextRank

#stop_words

Instance Method Summary collapse

Methods inherited from TextRank

#calculate_ranking, #filter_features, #initialize

Constructor Details

This class inherits a constructor from GraphRank::TextRank

Instance Method Details

#build_graphObject

Build the co-occurence graph for an n-gram.



22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
# File 'lib/graph-rank/sentences.rb', line 22

def build_graph
  @features.each do |grp|
    wc = grp.word_count
    @features.each do |grp2|
      wc2 = grp2.word_count
      score = 0.0
      grp.each_word do |wrd|
        next if @stop_words.include?(wrd.to_s)
        grp2.each_word do |wrd2|
          score += 1 if wrd.stem == wrd2.stem
        end
      end
      score /= (Math.log(wc) + Math.log(wc2))
      @ranking.add(grp.id, grp2.id, score)
    end
  end
end

#get_featuresObject

Stem stop words!



15
16
17
18
19
# File 'lib/graph-rank/sentences.rb', line 15

def get_features
  @section = section(@text)
  .apply(:chunk,:segment,:tokenize)
  @features = @section.groups
end

#run(text, n = 4) ⇒ Object



40
41
42
43
44
45
46
# File 'lib/graph-rank/sentences.rb', line 40

def run(text,n=4)
  rankings = super(text)
  rankings = rankings[0..n].map { |x|x[0] }
  @section.groups.select do |grp|
    rankings.include?(grp.id)
  end.map(&:to_s)
end