Permalink
Browse files

added more method comments and types

  • Loading branch information...
1 parent c68013e commit e96d07a822f154289373c5ecf28c38a0182b830b @domnikl committed Feb 20, 2012
@@ -6,8 +6,10 @@ module Highscore
# blacklisted words to be ignored in the resulting keywords
#
class Blacklist < Wordlist
+
# load default file
#
+ # @return Highscore::Blacklist
def self.load_default_file
file_path = File.join(File.dirname(__FILE__), %w{.. blacklist.txt})
self.load_file(file_path)
View
@@ -5,6 +5,8 @@ module Highscore
class Content
attr_reader :content
+ # @param content String
+ # @param wordlist Highscore::Wordlist
def initialize(content, wordlist = nil)
@content = content
@whitelist = @blacklist = nil
@@ -29,12 +31,15 @@ def initialize(content, wordlist = nil)
# configure ranking
#
+ # @param block
def configure(&block)
instance_eval(&block)
end
# set emphasis options to rank the content
#
+ # @param key Symbol
+ # @param value Object
def set(key, value)
@emphasis[key.to_sym] = value.to_f
end
@@ -68,6 +73,8 @@ def wordlist
# weight a single text keyword
#
+ # @param text String
+ # @return Float
def weight(text)
weight = @emphasis[:multiplier]
@@ -84,11 +91,19 @@ def weight(text)
weight
end
+ # weight the vowels on a text
+ #
+ # @param text String
+ # @return Float
def vowels(text)
percent = text.vowels.length / text.length.to_f
percent * @emphasis[:vowels]
end
+ # weight the consonants on a text
+ #
+ # @param text String
+ # @return Float
def consonants(text)
percent = text.consonants.length / text.length.to_f
percent * @emphasis[:consonants]
View
@@ -6,17 +6,24 @@ class Keyword
attr_accessor :weight, :text
# init a keyword
+ #
+ # @param text String
+ # @param weight Float
def initialize(text, weight)
@text = text
@weight = weight.to_f
end
# sort keywords
+ #
+ # @param other Highscore::Keyword
def <=>(other)
other.weight <=> @weight
end
# get the string
+ #
+ # @return String
def to_s
@text
end
View
@@ -9,6 +9,10 @@ class Keywords
include Enumerable
# find keywords in a piece of content
+ #
+ # @param content String
+ # @param wordlist Highscore::Wordlist
+ # @return Highscore::Keywords
def self.find_keywords content, wordlist
keywords = content.to_s.scan(/\w+/)
keywords.delete_if do |x|
@@ -35,21 +39,23 @@ def initialize
# ranks the keywords and removes keywords that have a low ranking
# or are blacklisted
#
- # :call-seq:
- # rank -> array
- #
+ # @return Array
def rank
sort
end
# get the top n keywords
#
+ # @param n Fixnum
+ # @return Array
def top n = 10
rank[0..(n - 1)]
end
# add new keywords
#
+ # @param keyword String
+ # @return Highscore::Keywords
def <<(keyword)
key = Digest::SHA1.hexdigest(keyword.text)
@@ -58,9 +64,13 @@ def <<(keyword)
else
@keywords[key] = keyword
end
+
+ @keywords
end
# sort
+ #
+ # @return Array
def sort
sorted = @keywords.sort {|a,b| a[1] <=> b[1] }
@@ -70,26 +80,34 @@ def sort
# Enumerable
#
- def each &block
+ def each
@keywords.each {|keyword| yield keyword[1] }
end
# number of Keywords given
+ #
+ # @return Fixnum
def length
@keywords.length
end
# get the keyword with the highest rank
+ #
+ # @return Highscore::Keyword
def first
sort.first
end
# get the keyword with the lowest rank
+ #
+ # @return Highscore::Keyword
def last
sort.reverse.first
end
# merge in another keyword list, operates on self
+ #
+ # @return Highscore::Keywords
def merge!(other)
other.each do |keyword|
self << keyword
View
@@ -1,11 +1,14 @@
-# monkey patch to define a keywords method on arbitrary strings
+# monkey patch to call custom methods on arbitrary strings
#
class String
# get keywords from the string
#
- def keywords(blacklist = nil, &block)
- content = Highscore::Content.new(self, blacklist)
+ # @param wordlist Highscore::Wordlist
+ # @param block Block
+ # @return Highscore::Keywords
+ def keywords(wordlist = nil, &block)
+ content = Highscore::Content.new(self, wordlist)
if block_given?
content.configure do
@@ -17,11 +20,15 @@ def keywords(blacklist = nil, &block)
end
# get all vowels from a string
+ #
+ # @return String
def vowels
gsub(/[^aeiou]/, '')
end
# get all consonants from a string
+ #
+ # @return String
def consonants
gsub(/[aeiou]/, '')
end
View
@@ -5,13 +5,18 @@ class Wordlist
include Enumerable
# load a file of keywords
+ #
+ # @param file_path String
+ # @return Highscore::Wordlist
def self.load_file file_path
words = File.read(file_path).split(' ')
self.load(words)
end
# load a file or array of words
#
+ # @param data String Array
+ # @return Highscore::Wordlist
def self.load(data)
if data.instance_of?(String)
words = data.split(' ')
@@ -28,34 +33,42 @@ def self.load(data)
attr_reader :words
+ # @param words Array
def initialize(words = [])
@words = words
end
# iterate over words
#
def each
- @words.each {|x| yield x }
+ @words.each {|word| yield word }
end
# count of ignored words
+ #
+ # @return Fixnum
def length
@words.length
end
# get an array of blacklisted words
#
+ # @return Array
def to_a
@words.to_a
end
# does the blacklist contain this keyword?
#
+ # @param keyword String
+ # @return true/false
def include? keyword
@words.include? keyword
end
# add a new word to the blacklist
+ #
+ # @param word String
def <<(word)
@words << word
end

0 comments on commit e96d07a

Please sign in to comment.