comparison libervia/backend/plugins/plugin_exp_lang_detect.py @ 4071:4b842c1fb686

refactoring: renamed `sat` package to `libervia.backend`
author Goffi <goffi@goffi.org>
date Fri, 02 Jun 2023 11:49:51 +0200
parents sat/plugins/plugin_exp_lang_detect.py@c23cad65ae99
children 0d7bb4df2343
comparison
equal deleted inserted replaced
4070:d10748475025 4071:4b842c1fb686
1 #!/usr/bin/env python3
2
3
4 # SAT plugin to detect language (experimental)
5 # Copyright (C) 2009-2021 Jérôme Poisson (goffi@goffi.org)
6
7 # This program is free software: you can redistribute it and/or modify
8 # it under the terms of the GNU Affero General Public License as published by
9 # the Free Software Foundation, either version 3 of the License, or
10 # (at your option) any later version.
11
12 # This program is distributed in the hope that it will be useful,
13 # but WITHOUT ANY WARRANTY; without even the implied warranty of
14 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 # GNU Affero General Public License for more details.
16
17 # You should have received a copy of the GNU Affero General Public License
18 # along with this program. If not, see <http://www.gnu.org/licenses/>.
19
20 from libervia.backend.core.i18n import _, D_
21 from libervia.backend.core.constants import Const as C
22 from libervia.backend.core.log import getLogger
23
24 log = getLogger(__name__)
25 from libervia.backend.core import exceptions
26
27 try:
28 from langid.langid import LanguageIdentifier, model
29 except ImportError:
30 raise exceptions.MissingModule(
31 'Missing module langid, please download/install it with "pip install langid")'
32 )
33
34 identifier = LanguageIdentifier.from_modelstring(model, norm_probs=False)
35
36
37 PLUGIN_INFO = {
38 C.PI_NAME: "Language detection plugin",
39 C.PI_IMPORT_NAME: "EXP-LANG-DETECT",
40 C.PI_TYPE: "EXP",
41 C.PI_PROTOCOLS: [],
42 C.PI_DEPENDENCIES: [],
43 C.PI_MAIN: "LangDetect",
44 C.PI_HANDLER: "no",
45 C.PI_DESCRIPTION: _("""Detect and set message language when unknown"""),
46 }
47
48 CATEGORY = D_("Misc")
49 NAME = "lang_detect"
50 LABEL = D_("language detection")
51 PARAMS = """
52 <params>
53 <individual>
54 <category name="{category_name}">
55 <param name="{name}" label="{label}" type="bool" value="true" />
56 </category>
57 </individual>
58 </params>
59 """.format(
60 category_name=CATEGORY, name=NAME, label=_(LABEL)
61 )
62
63
64 class LangDetect(object):
65 def __init__(self, host):
66 log.info(_("Language detection plugin initialization"))
67 self.host = host
68 host.memory.update_params(PARAMS)
69 host.trigger.add("message_received", self.message_received_trigger)
70 host.trigger.add("sendMessage", self.message_send_trigger)
71
72 def add_language(self, mess_data):
73 message = mess_data["message"]
74 if len(message) == 1 and list(message.keys())[0] == "":
75 msg = list(message.values())[0].strip()
76 if msg:
77 lang = identifier.classify(msg)[0]
78 mess_data["message"] = {lang: msg}
79 return mess_data
80
81 def message_received_trigger(self, client, message_elt, post_treat):
82 """ Check if source is linked and repeat message, else do nothing """
83
84 lang_detect = self.host.memory.param_get_a(
85 NAME, CATEGORY, profile_key=client.profile
86 )
87 if lang_detect:
88 post_treat.addCallback(self.add_language)
89 return True
90
91 def message_send_trigger(self, client, data, pre_xml_treatments, post_xml_treatments):
92 lang_detect = self.host.memory.param_get_a(
93 NAME, CATEGORY, profile_key=client.profile
94 )
95 if lang_detect:
96 self.add_language(data)
97 return True