Module: EmojiTranslate
- Defined in:
- lib/emoji_translate.rb,
lib/emoji_translate/version.rb
Constant Summary collapse
- VERSION =
"1.0.0"
Class Method Summary collapse
- .clean_up_word(word) ⇒ Object
- .find_by_keyword(keyword) ⇒ Object
- .get_all_possibilities(word) ⇒ Object
- .load_emojis ⇒ Object
- .transform_to_array(text) ⇒ Object
- .translate(text) ⇒ Object
- .translate_line(line) ⇒ Object
- .translate_word(word) ⇒ Object
Class Method Details
.clean_up_word(word) ⇒ Object
12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 |
# File 'lib/emoji_translate.rb', line 12 def self.clean_up_word(word) # Save punctuation from the start and the end of the word begin_punctuation = '' end_punctuation = '' while !word.empty? && !(word[0] =~ /\W/).nil? begin_punctuation += word[0] word = word[1..-1] end while !word.empty? && !(word[word.length - 1] =~ /\W/).nil? end_punctuation = word[word.length - 1] + end_punctuation word = word[0..-2] end [begin_punctuation, word, end_punctuation] end |
.find_by_keyword(keyword) ⇒ Object
66 67 68 69 70 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 |
# File 'lib/emoji_translate.rb', line 66 def self.find_by_keyword(keyword) self.load_emojis # First, check if it's a common keyword if %w{i you}.include? keyword return '😊' elsif keyword == 'she' return '💁' elsif keyword == 'he' return '💁♂️' elsif %w{we they}.include? keyword return '👩👩👦👦' elsif %w{am is are}.include? keyword return '👉' elsif keyword == 'and' return '➕' end possibilities = self.get_all_possibilities(keyword) # Try to find the matching emoji name emoji = @emojis.find do |emoji| emoji_name, _ = emoji possibilities.include? emoji_name end return emoji[1]['char'] unless emoji.nil? # If no matching name was found, search by keywords emoji = @emojis.find do |emoji| _, emoji_data = emoji !(emoji_data['keywords'] & possibilities).empty? end return emoji[1]['char'] unless emoji.nil? end |
.get_all_possibilities(word) ⇒ Object
39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 |
# File 'lib/emoji_translate.rb', line 39 def self.get_all_possibilities(word) # It could be singular word in plural form possible_singular = nil possible_singular = word[0..-2] if word.length > 2 && word.end_with?('s') # It could be a plural word in singular form possible_plural = word.length > 1 ? word + 's' : nil # It could be different verbed form possible_verbed_simple = nil possible_verbed_vowel = nil possible_verbed_doubled = nil if word.end_with?('ing') verb = word.chomp 'ing' possible_verbed_simple = verb # starting -> start possible_verbed_vowel = verb + 'e' # dancing -> dance possible_verbed_doubled = verb[0..-2] # beginning -> begin end [ word, possible_singular, possible_plural, possible_verbed_simple, possible_verbed_vowel, possible_verbed_doubled ].compact end |
.load_emojis ⇒ Object
30 31 32 33 34 35 36 37 |
# File 'lib/emoji_translate.rb', line 30 def self.load_emojis if @emojis.nil? file_path = File.join(File.dirname(__FILE__), '../assets/emojis.json') file = File.read file_path @emojis = JSON.parse file end @emojis end |
.transform_to_array(text) ⇒ Object
8 9 10 |
# File 'lib/emoji_translate.rb', line 8 def self.transform_to_array(text) text.split ' ' unless text.to_s.empty? end |
.translate(text) ⇒ Object
124 125 126 127 128 129 130 131 |
# File 'lib/emoji_translate.rb', line 124 def self.translate(text) self.load_emojis lines = text.lines emojified = lines.map { |line| self.translate_line line } emojified.join("\n") end |
.translate_line(line) ⇒ Object
115 116 117 118 119 120 121 122 |
# File 'lib/emoji_translate.rb', line 115 def self.translate_line(line) self.load_emojis processed_text = self.transform_to_array line emojified = processed_text.map { |word| self.translate_word word } emojified.join(' ') end |
.translate_word(word) ⇒ Object
101 102 103 104 105 106 107 108 109 110 111 112 113 |
# File 'lib/emoji_translate.rb', line 101 def self.translate_word(word) self.load_emojis begin_punctuation, word, end_punctuation = self.clean_up_word(word) downcased = word.downcase return begin_punctuation + end_punctuation if word.empty? emoji = self.find_by_keyword downcased result = emoji.nil? ? word : emoji begin_punctuation + result + end_punctuation end |