Module: Odyssey

Defined in:
lib/odyssey.rb,
lib/odyssey/error.rb,
lib/odyssey/engine.rb,
lib/odyssey/version.rb,
lib/odyssey/refinements.rb

Defined Under Namespace

Modules: Refinements Classes: ArgumentError, Engine, Error, Formula

Constant Summary collapse

FORMULAS =
%w[Ari ColemanLiau FleschKincaidGl FleschKincaidRe GunningFog Smog]
DEFAULT_FORMULA =
'FleschKincaidRe'
VERSION =
"0.3.2"

Class Method Summary collapse

Class Method Details

.analyze(text, formula_name = DEFAULT_FORMULA, all_stats = false) ⇒ Object

main method



8
9
10
11
12
13
14
15
16
17
# File 'lib/odyssey.rb', line 8

def self.analyze(text, formula_name = DEFAULT_FORMULA, all_stats = false)
  formula_name ||= DEFAULT_FORMULA

  @engine = Odyssey::Engine.new(formula_name)
  score = @engine.score(text)

  return @engine.get_stats if all_stats

  score
end

.analyze_all(text) ⇒ Object



38
39
40
# File 'lib/odyssey.rb', line 38

def self.analyze_all(text)
  analyze_multi text, FORMULAS, true
end

.analyze_multi(text, formula_names, all_stats = false) ⇒ Object



19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
# File 'lib/odyssey.rb', line 19

def self.analyze_multi(text, formula_names, all_stats = false)
  raise Odyssey::ArgumentError, "You must supply at least one formula" if formula_names.empty?

  scores = {}
  @engine = Odyssey::Engine.new(formula_names[0])
  scores[formula_names[0]] = @engine.score(text)

  formula_names.drop(1).each do |formula_name|
    @engine.update_formula(formula_name)
    scores[formula_name] = @engine.score("", false)
  end

  return scores unless all_stats

  all_stats = @engine.get_stats(false)
  all_stats['scores'] = scores
  all_stats
end

.method_missing(method_name, *args, &block) ⇒ Object

run whatever method was given as if it were a shortcut to a formula



43
44
45
46
47
48
# File 'lib/odyssey.rb', line 43

def self.method_missing(method_name, *args, &block)
  #send to the main method
  formula_class = method_name.to_s.split("_").map(&:capitalize).join
  super unless Object.const_defined? formula_class
  analyze(args[0], formula_class, args[1] || false)
end