Class: Insulter

Inherits:
Object
  • Object
show all
Defined in:
lib/urmum/insulter.rb

Constant Summary collapse

INSULT_BEGINNINGS =
[
  "You're",            
  "Your face is",
  "Your mum is", 
  "Your mum's face is" 
]
FALLBACK =
"That's what she said"
EMPTY_RESPONSE =
"Bring it on!"

Instance Method Summary collapse

Constructor Details

#initializeInsulter

Returns a new instance of Insulter.



13
14
15
# File 'lib/urmum/insulter.rb', line 13

def initialize 
  @tagger = EngTagger.new
end

Instance Method Details

#insult_with(string) ⇒ Object



17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
# File 'lib/urmum/insulter.rb', line 17

def insult_with(string)
  return EMPTY_RESPONSE if string.nil? || string.empty? || string =~ /^\s*$/ #just whitespace

  # Try and match against the beginning of the string. If so, get the next beginning from the array
  regexp = Regexp.new(%{^(?<start>#{INSULT_BEGINNINGS.join("|")}) (?<article>an|a) (?<thing>.*)})
  if matches = string.capitalize.match(regexp)
    index = INSULT_BEGINNINGS.index(matches[:start]) + 1
    if index < INSULT_BEGINNINGS.length
      return "#{INSULT_BEGINNINGS[index]} #{matches[:article]} #{matches[:thing]}"
    else
      return FALLBACK
    end
  end
  tagged_string = @tagger.add_tags(string)
  nouns = @tagger.get_nouns(tagged_string)

  if nouns.empty?
    FALLBACK
  else
    thing = nouns.keys.last
    "You're a #{thing}"
  end

end