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



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

Parameters:

  • content

    String

  • wordlist

    Highscore::Wordlist

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

    Regex

Returns:

  • Highscore::Keywords



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

Parameters:

  • keyword

    String

Returns:

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

#eachObject

Enumerable



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

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

#firstObject

get the keyword with the highest rank

Returns:

  • Highscore::Keyword



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

def first
  sort.first
end

#lastObject

get the keyword with the lowest rank

Returns:

  • Highscore::Keyword



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

def last
  sort.reverse.first
end

#lengthObject

number of Keywords given

Returns:

  • Fixnum



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

Returns:

  • Highscore::Keywords



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

#rankObject

ranks the keywords and removes keywords that have a low ranking or are blacklisted

Returns:

  • Array



40
41
42
# File 'lib/highscore/keywords.rb', line 40

def rank
  sort
end

#sortObject

sort

Returns:

  • Array



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

Parameters:

  • n (defaults to: 10)

    Fixnum

Returns:

  • Array



48
49
50
# File 'lib/highscore/keywords.rb', line 48

def top n = 10
  rank[0..(n - 1)]
end