Class: GraphRank::TextRank

Inherits:
Object
  • Object
show all
Defined in:
lib/graph-rank/text_rank.rb

Overview

  1. Mihalcea and P. Tarau, “TextRank: Bringing Order into Texts,”

in Proceedings of EMNLP 2004. Association for Computational Linguistics, 2004, pp. 404–411.

Direct Known Subclasses

Keywords, Sentences

Constant Summary collapse

StopWords =

Default English stop-word list.

["a", "about", "above", "above", "across", "after", "afterwards", "again", "against", "all", "almost", "alone", "along", "already", "also","although","always","am","among", "amongst", "amoungst", "amount",  "an", "and", "another", "any","anyhow","anyone","anything","anyway", "anywhere", "are", "around", "as",  "at", "back","be","became", "because","become","becomes", "becoming", "been", "before", "beforehand", "behind", "being", "below", "beside", "besides", "between", "beyond", "bill", "both", "bottom","but", "by", "call", "can", "cannot", "cant", "co", "con", "could", "couldnt", "cry", "de", "describe", "detail", "do", "done", "down", "due", "during", "each", "eg", "eight", "either", "eleven","else", "elsewhere", "empty", "enough", "etc", "even", "ever", "every", "everyone", "everything", "everywhere", "except", "few", "fifteen", "fify", "fill", "find", "fire", "first", "five", "for", "former", "formerly", "forty", "found", "four", "from", "front", "full", "further", "get", "give", "go", "had", "has", "hasnt", "have", "he", "hence", "her", "here", "hereafter", "hereby", "herein", "hereupon", "hers", "herself", "him", "himself", "his", "how", "however", "hundred", "ie", "if", "in", "inc", "indeed", "interest", "into", "is", "it", "its", "itself", "keep", "last", "latter", "latterly", "least", "less", "ltd", "made", "many", "may", "me", "meanwhile", "might", "mill", "mine", "more", "moreover", "most", "mostly", "move", "much", "must", "my", "myself", "name", "namely", "neither", "never", "nevertheless", "next", "nine", "no", "nobody", "none", "noone", "nor", "not", "nothing", "now", "nowhere", "of", "off", "often", "on", "once", "one", "only", "onto", "or", "other", "others", "otherwise", "our", "ours", "ourselves", "out", "over", "own","part", "per", "perhaps", "please", "put", "rather", "re", "same", "see", "seem", "seemed", "seeming", "seems", "serious", "several", "she", "should", "show", "side", "since", "sincere", "six", "sixty", "so", "some", "somehow", "someone", "something", "sometime", "sometimes", "somewhere", "still", "such", "system", "take", "ten", "than", "that", "the", "their", "them", "themselves", "then", "thence", "there", "thereafter", "thereby", "therefore", "therein", "thereupon", "these", "they", "thickv", "thin", "third", "this", "those", "though", "three", "through", "throughout", "thru", "thus", "to", "together", "too", "top", "toward", "towards", "twelve", "twenty", "two", "un", "under", "until", "up", "upon", "us", "very", "via", "was", "we", "well", "were", "what", "whatever", "when", "whence", "whenever", "where", "whereafter", "whereas", "whereby", "wherein", "whereupon", "wherever", "whether", "which", "while", "whither", "who", "whoever", "whole", "whom", "whose", "why", "will", "with", "within", "without", "would", "yet", "you", "your", "yours", "yourself", "yourselves", "the"]

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(ngram_size = 3, damping = nil, convergence = nil) ⇒ TextRank

Initialize with ngram size, damping and convergence.



13
14
15
16
17
# File 'lib/graph-rank/text_rank.rb', line 13

def initialize(ngram_size=3, damping=nil, convergence=nil)
  @ranking = GraphRank::PageRank.new(damping, convergence)
  @stop_words = StopWords
  @ngram_size = ngram_size
end

Instance Attribute Details

#stop_wordsObject

Allow configuration of stop words.



10
11
12
# File 'lib/graph-rank/text_rank.rb', line 10

def stop_words
  @stop_words
end

Instance Method Details

#build_graphObject

Build the graph from the features.



39
40
41
# File 'lib/graph-rank/text_rank.rb', line 39

def build_graph
  raise 'Must be implemented in subclass.'
end

#calculate_rankingObject

Calculate the PageRank ranking.



44
45
46
# File 'lib/graph-rank/text_rank.rb', line 44

def calculate_ranking
  @ranking.calculate
end

#filter_featuresObject

Return only features of interest.



34
35
36
# File 'lib/graph-rank/text_rank.rb', line 34

def filter_features
  @features
end

#get_featuresObject

Return the features (keyword, sentence, etc.)



29
30
31
# File 'lib/graph-rank/text_rank.rb', line 29

def get_features
  raise 'Must be implemented in subclass.'
end

#run(text) ⇒ Object

Add text and return PageRank.



20
21
22
23
24
25
26
# File 'lib/graph-rank/text_rank.rb', line 20

def run(text)
  @text = text
  get_features
  filter_features
  build_graph
  calculate_ranking
end