Method: LOLspeak::Tranzlator#translate_word
- Defined in:
- lib/lolspeak.rb
#translate_word(word, &filter) ⇒ Object
Translates a single word into LOLspeak. By default, the result is in all lower case:
translator.translate_word("Hi") -> "oh hai"
If a block is given the word may be transformed. You could use this to upper case or XML encode the result. This example upper cases the result:
translator.translate_word("hi") { |w| w.upcase } -> "OH HAI"
If heuristics are off, then only words in the dictionary are translated. If heuristics are on, then words not in the dictionary may be translated using standard LOLspeak heuristics, such as “*tion” -> “*shun”.
:call-seq:
translate_word(word) -> String
translate_word(word) { |word| transform } -> String
71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 |
# File 'lib/lolspeak.rb', line 71 def translate_word(word, &filter) word = word.downcase lol_word = @dictionary[word] if lol_word.nil? lol_word = @dictionary[word.gsub("’", "'")] end if lol_word.nil? and word.match(/(.*)([\’\']\w+)$/) prefix, suffix = $1, $2 lol_word = @dictionary[prefix] lol_word += suffix if !lol_word.nil? end if lol_word.nil? and @try_heuristics and !@heuristics_exclude.member?(word) if (word =~ /(.*)tion(s?)$/) lol_word = "#{$1}shun#{$2}" elsif (word =~ /(.*)ed$/) lol_word = "#{$1}d" elsif (word =~ /(.*)ing$/) lol_word = "#{$1}in" elsif (word =~ /(.*)ss$/) lol_word = "#{$1}s" elsif (word =~ /(.*)er$/) lol_word = "#{$1}r" elsif (word !~ /ous$/) and (word =~ /^([0-9A-Za-z_]+)s$/) lol_word = "#{$1}z" end if (word =~ /ph/) lol_word = word.dup if lol_word.nil? lol_word.gsub!(/ph/, 'f') end if !lol_word.nil? @translated_heuristics[word] = lol_word end end if lol_word.nil? lol_word = word else @traced_words[word] = lol_word end if !filter.nil? lol_word = filter.call(lol_word) end return lol_word end |