Emoji: split qualification variation into a module

This commit is contained in:
Hélène 2022-07-26 01:38:59 +02:00
parent fb3f6e1975
commit b99f5d6183
No known key found for this signature in database
GPG Key ID: A215F2E9F1589D62
2 changed files with 43 additions and 33 deletions

View File

@ -9,6 +9,7 @@ defmodule Pleroma.Emoji do
""" """
use GenServer use GenServer
alias Pleroma.Emoji.Combinations
alias Pleroma.Emoji.Loader alias Pleroma.Emoji.Loader
require Logger require Logger
@ -138,42 +139,10 @@ def is_unicode_emoji?(unquote(emoji)), do: true
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 = emoji_qualification_map =
emojis emojis
|> Enum.filter(&String.contains?(&1, "\uFE0F")) |> Enum.filter(&String.contains?(&1, "\uFE0F"))
|> Enum.map(fn emoji -> |> Combinations.variate_emoji_qualification()
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 {qualified, unqualified_list} <- emoji_qualification_map do
for unqualified <- unqualified_list do for unqualified <- unqualified_list do

View File

@ -0,0 +1,41 @@
# Pleroma: A lightweight social networking server
# Copyright © 2017-2022 Pleroma Authors <https://pleroma.social/>
# SPDX-License-Identifier: AGPL-3.0-only
defmodule Pleroma.Emoji.Combinations do
# 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.
defp qualification_combinations([]), do: [[]]
defp qualification_combinations(["\uFE0F" | tail]) do
tail
|> qualification_combinations()
|> Enum.flat_map(fn x -> [x, ["\uFE0F" | x]] end)
end
defp qualification_combinations([codepoint | tail]) do
tail
|> qualification_combinations()
|> Enum.map(fn x -> [codepoint | x] end)
end
def variate_emoji_qualification(emoji) when is_binary(emoji) do
emoji
|> String.codepoints()
|> qualification_combinations()
|> Enum.map(&List.to_string/1)
end
def variate_emoji_qualification(emoji) when is_list(emoji) do
emoji
|> Enum.map(fn emoji -> {emoji, variate_emoji_qualification(emoji)} end)
end
end