class Dogeify

Constants

ADJECTIVES
EMOTIONS
VERSION

Attributes

tagger[R]

Public Class Methods

new() click to toggle source
# File lib/dogeify.rb, line 8
def initialize
  @tagger = EngTagger.new
end

Public Instance Methods

process(str, options = {}) click to toggle source
# File lib/dogeify.rb, line 12
def process(str, options = {})
  # Parse sentences.
  sentences = str.downcase.split(/[\.!?]+/).map(&:strip)

  sentences = sentences.map do |sentence|
    # Ignore any provided patterns.
    sentence = ignore_patterns(sentence, options[:ignore]) if options[:ignore]
    
    # Select just the nouns.
    tagged_sentence = tagger.add_tags(sentence)
    phrases = tagger.get_nouns(tagged_sentence).keys rescue []

    # Prefix nouns with adjectives, and convert to sentences.
    phrases.map! { |phrase| correct_spelling(phrase) }
    phrases.map! { |phrase| "#{adjective} #{phrase}." }

    # Append a word or phrase describing emotion.
    phrases << "#{emotional_summary}."

    phrases.join(' ')
  end

  sentences.join(' ')
end

Private Instance Methods

adjective() click to toggle source
# File lib/dogeify.rb, line 58
def adjective
  ADJECTIVES[adjective_offset]
end
adjective_offset() click to toggle source
# File lib/dogeify.rb, line 62
def adjective_offset
  @adjective_offset ||= -1
  @adjective_offset = (@adjective_offset + 1) % ADJECTIVES.size
end
correct_spelling(word) click to toggle source
# File lib/dogeify.rb, line 41
def correct_spelling(word)
  word.dup.tap do |word|
    word.gsub!(/er$/, 'ar')                    # super => supar
    word.gsub!(/ph/, 'f')                      # phone => fone
    word.gsub!(/cious/, 'shus')                # delicious => delishus, deliciousness => delishusness
    word.gsub!(/([^s])tion(s$)?/, '\1shun\2')  # emotion => emoshun, emotions => emoshuns, emotionless => emoshunless, question (unchanged)
    word.gsub!(/stion$/, 'schun')              # question => queschun, potion (unchanged)
    word.gsub!(/dog([^e]|\b)/, 'doge\1')       # dog => doge, dogs => doges, underdog => underdoge, doge (unchanged)
  end
end
emotional_summary() click to toggle source
# File lib/dogeify.rb, line 67
def emotional_summary
  EMOTIONS[emotional_summary_offset]
end
emotional_summary_offset() click to toggle source
# File lib/dogeify.rb, line 71
def emotional_summary_offset
  @emotional_summary_offset ||= -1
  @emotional_summary_offset = (@emotional_summary_offset + 1) % EMOTIONS.size
end
ignore_patterns(sentence, patterns) click to toggle source
# File lib/dogeify.rb, line 52
def ignore_patterns(sentence, patterns)
  sentence.dup.tap do |sentence|
    Array(patterns).map { |pattern| sentence.gsub!(pattern, '') }
  end
end