class HomeOfPhone::Base

Constants

DEFAULT_RHYME_BOOST
DEFAULT_SIMILARITY_OPTIONS

Public Class Methods

new(preload_phonemes: true) click to toggle source
# File lib/home_of_phone/base.rb, line 11
def initialize(preload_phonemes: true)
  @phonemes_loaded = false
  load_phonemes if preload_phonemes
end

Public Instance Methods

inspect() click to toggle source
# File lib/home_of_phone/base.rb, line 20
def inspect
  to_s
end
load_phonemes() click to toggle source
# File lib/home_of_phone/base.rb, line 24
def load_phonemes
  phonemes
  !phonemes.nil?
end
similarity(word1, word2, options=DEFAULT_SIMILARITY_OPTIONS) click to toggle source
# File lib/home_of_phone/base.rb, line 29
def similarity(word1, word2, options=DEFAULT_SIMILARITY_OPTIONS)
  phonemes1 = get_word_phonemes(word1)
  phonemes2 = get_word_phonemes(word2)
  unless options[:use_phoneme_accents]
    phonemes1 = HomeOfPhone::Tools.remove_phoneme_accents(phonemes1)
    phonemes2 = HomeOfPhone::Tools.remove_phoneme_accents(phonemes2)
  end

  lev_dist = HomeOfPhone::Tools.levenshtein_distance(phonemes1, phonemes2)
  max_dist = [phonemes1.length, phonemes2.length].max
  raw_score = (max_dist.to_f - lev_dist.to_f) / max_dist.to_f
  rhyme_boost = options[:use_rhyme_boost] && HomeOfPhone::Tools.rhyme_ish(phonemes1, phonemes2) ? DEFAULT_RHYME_BOOST : 1.0

  final_score = [(raw_score * rhyme_boost), 1.0].min
  final_int_score = (final_score * 100).to_int
  final_int_score
end
to_s() click to toggle source
# File lib/home_of_phone/base.rb, line 16
def to_s
  "#<HomeOfPhone::Base phomes_loaded=#{@phonemes_loaded}>"
end

Private Instance Methods

get_word_phonemes(word) click to toggle source
# File lib/home_of_phone/base.rb, line 58
def get_word_phonemes(word)
  word_key = word.upcase
  raise HomeOfPhone::NoPhonemesError, "Can't find phonemes for #{word}" unless phonemes.key?(word_key)
  phonemes[word_key]
end
phonemes() click to toggle source
# File lib/home_of_phone/base.rb, line 49
def phonemes
  @phonemes ||= begin
    require_relative "phonemes"
    cmu_dict = HomeOfPhone::Phonemes.cmu_dict
    @phonemes_loaded = true
    cmu_dict
  end
end