module EmojiTranslate

Constants

VERSION

Public Class Methods

clean_up_word(word) click to toggle source
# 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) click to toggle source
# 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) click to toggle source
# 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() click to toggle source
# 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) click to toggle source
# File lib/emoji_translate.rb, line 8
def self.transform_to_array(text)
  text.split ' ' unless text.to_s.empty?
end
translate(text) click to toggle source
# 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) click to toggle source
# 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) click to toggle source
# 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