Class: Highscore::Content

Inherits:
Object
  • Object
show all
Defined in:
lib/highscore/content.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(content, wordlist = nil) ⇒ Content

Returns a new instance of Content.

Parameters:

  • content

    String

  • wordlist (defaults to: nil)

    Highscore::Wordlist



14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
# File 'lib/highscore/content.rb', line 14

def initialize(content, wordlist = nil)
  @content = content
  @whitelist = @blacklist = nil
  @language_wordlists = {}

  if wordlist.nil?
    @blacklist = Highscore::Blacklist.load_default_file
  elsif wordlist.kind_of? Highscore::Blacklist
    @blacklist = wordlist
  else
    @whitelist = wordlist
  end

  @emphasis = {
    :multiplier => 1.0,
    :upper_case => 3.0,
    :long_words => 2.0,
    :long_words_threshold => 15,
    :vowels => 0,
    :consonants => 0,
    :ignore_short_words => true,
    :ignore_case => false,
    :word_pattern => /\p{Word}+/u,
    :stemming => false
  }
  
  if RUBY_VERSION =~ /^1\.8/
    @emphasis[:word_pattern] = /\w+/
  end
end

Instance Attribute Details

#contentObject (readonly)

Returns the value of attribute content.



9
10
11
# File 'lib/highscore/content.rb', line 9

def content
  @content
end

#language_wordlistsObject

Returns the value of attribute language_wordlists.



10
11
12
# File 'lib/highscore/content.rb', line 10

def language_wordlists
  @language_wordlists
end

Instance Method Details

#add_wordlist(language_wordlist, language) ⇒ Object

add another wordlist, given a language

Parameters:

  • language_wordlist

    Highscore::Wordlist

  • language

    String

Raises:

  • (ArgumentError)


64
65
66
67
# File 'lib/highscore/content.rb', line 64

def add_wordlist(language_wordlist, language)
  raise ArgumentError, "Not a valid Wordlist" unless language_wordlist.kind_of? Highscore::Wordlist
  language_wordlists[language.to_sym] = language_wordlist
end

#configure(&block) ⇒ Object

configure ranking

Parameters:

  • block


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

def configure(&block)
  instance_eval(&block)
end

#keywords(opts = {}) ⇒ Object

get the ranked keywords

Returns:

  • Highscore::Keywords



72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
# File 'lib/highscore/content.rb', line 72

def keywords(opts = {})
  used_wordlist = nil
  if opts[:lang]
    used_wordlist = language_wordlists[opts[:lang].to_sym]
  else
    used_wordlist = wordlist
  end
    
  @emphasis[:stemming] = use_stemming?

  keywords = Keywords.new

  Keywords.find_keywords(processed_content, used_wordlist, word_pattern).each do |text|
    text = text.to_s
    text = text.stem if @emphasis[:stemming]

    if not (text.match(/^[\d]+(\.[\d]+){0,1}$/) or text.length <= 2)
      keywords << Highscore::Keyword.new(text, weight(text))
    elsif allow_short_words
      keywords << Highscore::Keyword.new(text, weight(text))
    end
  end

  keywords
end

#languageObject

guess the language of the content and return a symbol for it

> done via whatlanguage gem

Returns:

  • Symbol



113
114
115
# File 'lib/highscore/content.rb', line 113

def language
  @content.language
end

#set(key, value) ⇒ Object

set emphasis options to rank the content

Parameters:

  • key

    Symbol

  • value

    Object



56
57
58
# File 'lib/highscore/content.rb', line 56

def set(key, value)
  @emphasis[key.to_sym] = value
end

#wordlistObject

get the used wordlist

Returns:

  • Highscore::Wordlist



101
102
103
104
105
106
107
# File 'lib/highscore/content.rb', line 101

def wordlist
  unless @whitelist.nil?
    @whitelist
  else
    @blacklist
  end
end