Emoji: implement full-qualifier using combinations

This implements fully_qualify_emoji/1, which will return the
fully-qualified version of an emoji if it knows of one, or return the
emoji unmodified if not.
This code generates combinations per emoji: for each FE0F, all possible
combinations of the character being removed or staying will be
generated. This is made as an attempt to find all partially-qualified
and unqualified versions of a fully-qualified emoji.

I have found *no cases* for which this would be a problem, after
browsing the entire emoji list in emoji-test.txt. This is safe, and,
sadly, most likely the sanest too.
This commit is contained in:
Hélène 2022-07-25 16:20:12 +02:00
parent d39f803bdd
commit 01d396585e
No known key found for this signature in database
GPG key ID: A215F2E9F1589D62

View file

@ -137,4 +137,49 @@ defmodule Pleroma.Emoji do
end end
def is_unicode_emoji?(_), do: false def is_unicode_emoji?(_), do: false
# FE0F is the emoji variation sequence. It is used for fully-qualifying
# emoji, and that includes emoji combinations.
# This code generates combinations per emoji: for each FE0F, all possible
# combinations of the character being removed or staying will be generated.
# This is made as an attempt to find all partially-qualified and unqualified
# versions of a fully-qualified emoji.
# I have found *no cases* for which this would be a problem, after browsing
# the entire emoji list in emoji-test.txt. This is safe, and, sadly, most
# likely sane too.
emoji_qualification_map =
emojis
|> Enum.filter(&String.contains?(&1, "\uFE0F"))
|> Enum.map(fn emoji ->
combinate = fn x, combinate ->
case x do
[] ->
[[]]
["\uFE0F" | tail] ->
combinate.(tail, combinate)
|> Enum.flat_map(fn x -> [x, ["\uFE0F" | x]] end)
[codepoint | tail] ->
combinate.(tail, combinate)
|> Enum.map(fn x -> [codepoint | x] end)
end
end
unqualified_list =
emoji
|> String.codepoints()
|> combinate.(combinate)
|> Enum.map(&List.to_string/1)
{emoji, unqualified_list}
end)
for {qualified, unqualified_list} <- emoji_qualification_map do
for unqualified <- unqualified_list do
def fully_qualify_emoji(unquote(unqualified)), do: unquote(qualified)
end
end
def fully_qualify_emoji(emoji), do: emoji
end end