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



11
12
13
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 11

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

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

  @emphasis = {
    :multiplier => 1.0,
    :upper_case => 3.0,
    :long_words => 2.0,
    :short_words_threshold => 2,
    :bonus_multiplier => 3.0,
    :bonus_list => bonuslist,
    :long_words_threshold => 15,
    :vowels => 0,
    :consonants => 0,
    :ignore_short_words => true,
    :ignore_case => false,
    :ignore => nil,
    :word_pattern => /\p{Word}+/u,
    :stemming => false
  }

end

Instance Attribute Details

#contentObject (readonly)

Returns the value of attribute content.



6
7
8
# File 'lib/highscore/content.rb', line 6

def content
  @content
end

#language_wordlistsObject

Returns the value of attribute language_wordlists.



7
8
9
# File 'lib/highscore/content.rb', line 7

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
# 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 |word|
    keyword = extract_keyword(word)
    keywords << keyword unless keyword.nil?
  end

  keywords
end

#languageObject

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

> done via whatlanguage gem

Returns:

  • Symbol



107
108
109
110
# File 'lib/highscore/content.rb', line 107

def language
  wl = WhatLanguage.new(:all)
  wl.language(@content)
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



95
96
97
98
99
100
101
# File 'lib/highscore/content.rb', line 95

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