Decode emoji from a sentence

I need that in the input of a sentence, I identify that if I have any emoji in that sentence I need to decode the emoji to a string, I did it this way but it is not working. Does anyone know how to help me please?

@emoji_ 127_462
  @ascii 88

  def decode(sentence) when is_nil(sentence), do: sentence

  def decode(sentence) do
    replace_emoji(sentence)
  end

  defp replace_emoji(decode) do
    char = to_charlist(decode)
    Enum.map(charlist, fn a -> emoji_into_string(a) end) |> to_string()
  end

  def emoji_into_string(c) do
    if c > @emoji_ do
      @ascii
    else
      c
    end
  end
1 Like

When you run this code, what happens? What did you expect to happen?

At a glance, this looks like it should work. This code round-trips a string correctly:

"fo😀o" |> String.to_charlist() |> to_string()
2 Likes

return the string with emoji :frowning:

This is better written as:

def decode(nil), do: nil

Why not break apart the string by using String.codepoints and then iterate on each character? I wouldn’t rely on to_charlist to do the same thing properly (but I might be wrong).

Can you give us an example of a string with which your code doesn’t return what you expect?

2 Likes

“test :grinning:”
for example this string, I need my code to encode this emoji and pass it to string

What should happen to: :family_man_woman_boy_boy:? If it should stay a single emoji then you need graphemes and not codepoints.

1 Like

the same happens for all emojis

What do you mean by this? Please provide an example of what output would be returned for this given string.

You don’t explain what “happens” or what your desired output is so its very difficult to provide any meaningful suggestions.

3 Likes

First, thanks for the feedback, I didn’t realize that it doesn’t make my text clear.

The following is:
I need a function that, when receiving an input that has an emoji, can decode it to a text:
example
Input: “test :grinning:”
Output: “test :grinning”

First, thanks for the feedback, I didn’t realize that it doesn’t make my text clear.

The following is:
I need a function that, when receiving an input that has an emoji, can decode it to a text:
example
Input: “test :grinning:”
Output: “test :grinning”

did you get it ?

You are looking for something like this - GitHub - NeelShah18/emot: Open source Emoticons and Emoji detection library: emot in elixir ?

1 Like

Also emojix looks to be a good option as well.

2 Likes

Looks like Emojix.replace/2 is what OP is looking for, need to prepend : to a shortcode in the func :

1 Like

Do I need to install any dependencies or libraries? to make it happen?

Can you go through the instructions in the link @kip provided:

If you have any specific question after trying out - may be you can post saying “I tried this and I am having specific problem doing this step or invoking this function”?

3 Likes