Class: Highscore::Keywords

Inherits:
Object
  • Object
show all
Includes:
Enumerable
Defined in:
lib/highscore/keywords.rb

Overview

keywords that were found in content

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeKeywords

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

Parameters:

  • content

    String

  • wordlist

    Highscore::Wordlist

  • pattern (defaults to: /\w+/)

    Regex

Returns:

  • Highscore::Keywords



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

Parameters:

  • keyword

    Highscore::Keyword

Returns:

  • Highscore::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

#eachObject

Enumerable



79
80
81
# File 'lib/highscore/keywords.rb', line 79

def each
  @keywords.each {|keyword| yield keyword[1] }
end

#firstObject

get the keyword with the highest rank

Returns:

  • Highscore::Keyword



93
94
95
# File 'lib/highscore/keywords.rb', line 93

def first
  sort.first
end

#lastObject

get the keyword with the lowest rank

Returns:

  • Highscore::Keyword



100
101
102
# File 'lib/highscore/keywords.rb', line 100

def last
  sort.reverse.first
end

#lengthObject

number of Keywords given

Returns:

  • Fixnum



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

Returns:

  • Highscore::Keywords



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

#rankObject

ranks the keywords

Returns:

  • Array



33
34
35
# File 'lib/highscore/keywords.rb', line 33

def rank
  sort
end

#sortObject

sort

Returns:

  • Array



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

Returns:

  • Float



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

Parameters:

  • n (defaults to: 10)

    Fixnum

Returns:

  • Array



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