Class: Highscore::Keywords
- Inherits:
-
Object
- Object
- Highscore::Keywords
- Includes:
- Enumerable
- Defined in:
- lib/highscore/keywords.rb
Overview
keywords that were found in content
Class Method Summary collapse
-
.find_keywords(content, wordlist, pattern = /\w+/) ⇒ Object
find keywords in a piece of content.
Instance Method Summary collapse
-
#<<(keyword) ⇒ Object
add new keywords.
-
#each ⇒ Object
Enumerable.
-
#first ⇒ Object
get the keyword with the highest rank.
-
#initialize ⇒ Keywords
constructor
init a new keyword collection.
-
#last ⇒ Object
get the keyword with the lowest rank.
-
#length ⇒ Object
number of Keywords given.
-
#merge!(other) ⇒ Object
merge in another keyword list, operates on self.
-
#rank ⇒ Object
ranks the keywords.
-
#sort ⇒ Object
sort.
-
#sum(n) ⇒ Object
Returns the sum of the weight of the top n keywords.
-
#top(n = 10) ⇒ Object
get the top n keywords.
Constructor Details
#initialize ⇒ Keywords
init a new keyword collection
26 27 28 |
# File 'lib/highscore/keywords.rb', line 26 def initialize @keywords = {} end |
Class Method Details
.find_keywords(content, wordlist, pattern = /\w+/) ⇒ Object
find keywords in a piece of content
14 15 16 17 18 19 20 21 22 |
# File 'lib/highscore/keywords.rb', line 14 def self.find_keywords content, wordlist, pattern=/\w+/ keywords = content.to_s.scan(pattern).flatten if not wordlist.nil? and wordlist.respond_to? :filter keywords = wordlist.filter(keywords) end keywords.sort end |
Instance Method Details
#<<(keyword) ⇒ Object
add new keywords
57 58 59 60 61 62 63 64 65 |
# File 'lib/highscore/keywords.rb', line 57 def <<(keyword) if @keywords.has_key?(keyword.text) @keywords[keyword.text].weight += keyword.weight else @keywords[keyword.text] = keyword end @keywords end |
#each ⇒ Object
Enumerable
79 80 81 |
# File 'lib/highscore/keywords.rb', line 79 def each @keywords.each {|keyword| yield keyword[1] } end |
#first ⇒ Object
get the keyword with the highest rank
93 94 95 |
# File 'lib/highscore/keywords.rb', line 93 def first sort.first end |
#last ⇒ Object
get the keyword with the lowest rank
100 101 102 |
# File 'lib/highscore/keywords.rb', line 100 def last sort.reverse.first end |
#length ⇒ Object
number of Keywords given
86 87 88 |
# File 'lib/highscore/keywords.rb', line 86 def length @keywords.length end |
#merge!(other) ⇒ Object
merge in another keyword list, operates on self
115 116 117 118 119 120 121 |
# File 'lib/highscore/keywords.rb', line 115 def merge!(other) other.each do |keyword| self << keyword end self end |
#rank ⇒ Object
ranks the keywords
33 34 35 |
# File 'lib/highscore/keywords.rb', line 33 def rank sort end |
#sort ⇒ Object
sort
70 71 72 73 74 75 |
# File 'lib/highscore/keywords.rb', line 70 def sort sorted = @keywords.sort {|a,b| a[1] <=> b[1] } # convert Array from sort back to Array of Keyword objects sorted.collect {|x| x[1]} end |
#sum(n) ⇒ Object
Returns the sum of the weight of the top n keywords
107 108 109 110 |
# File 'lib/highscore/keywords.rb', line 107 def sum(n) top = rank[0..(n - 1)] top.map(&:weight).inject { |sum,weight| sum + weight } end |
#top(n = 10) ⇒ Object
get the top n keywords
41 42 43 44 45 46 47 48 49 50 51 |
# File 'lib/highscore/keywords.rb', line 41 def top n = 10 tops = rank[0..(n - 1)] sum_all = sum(n) # set percentage values tops.each do |keyword| keyword.percent = keyword.weight * 100 / sum_all end tops end |