Detect language for incoming posts

Signed-off-by: marcin mikołajczak <git@mkljczk.pl>
This commit is contained in:
marcin mikołajczak 2022-11-03 23:31:36 +01:00
parent 4442fc40e6
commit 85105e0049
2 changed files with 12 additions and 3 deletions

View file

@ -8,6 +8,7 @@ defmodule Pleroma.Web.ActivityPub.Transmogrifier do
"""
alias Pleroma.Activity
alias Pleroma.EctoType.ActivityPub.ObjectValidators
alias Pleroma.Language.LanguageDetector
alias Pleroma.Maps
alias Pleroma.Object
alias Pleroma.Object.Containment
@ -23,6 +24,7 @@ defmodule Pleroma.Web.ActivityPub.Transmogrifier do
alias Pleroma.Workers.TransmogrifierWorker
import Ecto.Query
import Pleroma.Web.CommonAPI.Utils, only: [get_valid_language: 1]
import Pleroma.Web.Utils.Guards, only: [not_empty_string: 1]
require Logger
@ -1100,8 +1102,9 @@ defp maybe_add_content_map(object), do: object
def maybe_add_language(object) do
language =
get_language_from_context(object) |> Pleroma.Web.CommonAPI.Utils.get_valid_language() ||
get_language_from_content_map(object) |> Pleroma.Web.CommonAPI.Utils.get_valid_language()
get_language_from_context(object) |> get_valid_language() ||
get_language_from_content_map(object) |> get_valid_language() ||
get_language_from_content(object) |> get_valid_language()
if language do
Map.put(object, "language", language)
@ -1133,4 +1136,10 @@ defp get_language_from_content_map(%{"contentMap" => content_map, "content" => s
end
defp get_language_from_content_map(_), do: nil
defp get_language_from_content(%{"summary" => summary, "content" => content}) do
LanguageDetector.detect("#{summary} #{content}")
end
defp get_language_from_content(_), do: nil
end

View file

@ -14,5 +14,5 @@ def missing_dependencies, do: []
def configured?, do: true
@impl Provider
def detect(text), do: "fr"
def detect(_text), do: "fr"
end