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 and removes keywords that have a low ranking or are blacklisted.
-
#sort ⇒ Object
sort.
-
#top(n = 10) ⇒ Object
get the top n keywords.
Constructor Details
#initialize ⇒ Keywords
init a new keyword collection
32 33 34 |
# File 'lib/highscore/keywords.rb', line 32 def initialize @keywords = {} end |
Class Method Details
.find_keywords(content, wordlist, pattern = /\w+/) ⇒ Object
find keywords in a piece of content
17 18 19 20 21 22 23 24 25 26 27 28 |
# File 'lib/highscore/keywords.rb', line 17 def self.find_keywords content, wordlist, pattern=/\w+/ keywords = content.to_s.scan(pattern).flatten keywords.delete_if do |key, value| if wordlist.kind_of? Highscore::Blacklist wordlist.include?(key.downcase) elsif wordlist.kind_of? Highscore::Whitelist not wordlist.include?(key.downcase) end end keywords.sort end |
Instance Method Details
#<<(keyword) ⇒ Object
add new keywords
56 57 58 59 60 61 62 63 64 65 66 |
# File 'lib/highscore/keywords.rb', line 56 def <<(keyword) key = Digest::SHA1.hexdigest(keyword.text) if @keywords.has_key?(key) @keywords[key].weight += keyword.weight else @keywords[key] = keyword end @keywords end |
#each ⇒ Object
Enumerable
80 81 82 |
# File 'lib/highscore/keywords.rb', line 80 def each @keywords.each {|keyword| yield keyword[1] } end |
#first ⇒ Object
get the keyword with the highest rank
94 95 96 |
# File 'lib/highscore/keywords.rb', line 94 def first sort.first end |
#last ⇒ Object
get the keyword with the lowest rank
101 102 103 |
# File 'lib/highscore/keywords.rb', line 101 def last sort.reverse.first end |
#length ⇒ Object
number of Keywords given
87 88 89 |
# File 'lib/highscore/keywords.rb', line 87 def length @keywords.length end |
#merge!(other) ⇒ Object
merge in another keyword list, operates on self
108 109 110 111 112 113 114 |
# File 'lib/highscore/keywords.rb', line 108 def merge!(other) other.each do |keyword| self << keyword end self end |
#rank ⇒ Object
ranks the keywords and removes keywords that have a low ranking or are blacklisted
40 41 42 |
# File 'lib/highscore/keywords.rb', line 40 def rank sort end |
#sort ⇒ Object
sort
71 72 73 74 75 76 |
# File 'lib/highscore/keywords.rb', line 71 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 |
#top(n = 10) ⇒ Object
get the top n keywords
48 49 50 |
# File 'lib/highscore/keywords.rb', line 48 def top n = 10 rank[0..(n - 1)] end |