comparison sat/plugins/plugin_exp_lang_detect.py @ 3028:ab2696e34d29

Python 3 port: /!\ this is a huge commit /!\ starting from this commit, SàT is needs Python 3.6+ /!\ SàT maybe be instable or some feature may not work anymore, this will improve with time This patch port backend, bridge and frontends to Python 3. Roughly this has been done this way: - 2to3 tools has been applied (with python 3.7) - all references to python2 have been replaced with python3 (notably shebangs) - fixed files not handled by 2to3 (notably the shell script) - several manual fixes - fixed issues reported by Python 3 that where not handled in Python 2 - replaced "async" with "async_" when needed (it's a reserved word from Python 3.7) - replaced zope's "implements" with @implementer decorator - temporary hack to handle data pickled in database, as str or bytes may be returned, to be checked later - fixed hash comparison for password - removed some code which is not needed anymore with Python 3 - deactivated some code which needs to be checked (notably certificate validation) - tested with jp, fixed reported issues until some basic commands worked - ported Primitivus (after porting dependencies like urwid satext) - more manual fixes
author Goffi <goffi@goffi.org>
date Tue, 13 Aug 2019 19:08:41 +0200
parents 003b8b4b56a7
children 9d0df638c8b4
comparison
equal deleted inserted replaced
3027:ff5bcb12ae60 3028:ab2696e34d29
1 #!/usr/bin/env python2 1 #!/usr/bin/env python3
2 # -*- coding: utf-8 -*- 2 # -*- coding: utf-8 -*-
3 3
4 # SAT plugin to detect language (experimental) 4 # SAT plugin to detect language (experimental)
5 # Copyright (C) 2009-2019 Jérôme Poisson (goffi@goffi.org) 5 # Copyright (C) 2009-2019 Jérôme Poisson (goffi@goffi.org)
6 6
26 26
27 try: 27 try:
28 from langid.langid import LanguageIdentifier, model 28 from langid.langid import LanguageIdentifier, model
29 except ImportError: 29 except ImportError:
30 raise exceptions.MissingModule( 30 raise exceptions.MissingModule(
31 u'Missing module langid, please download/install it with "pip install langid")' 31 'Missing module langid, please download/install it with "pip install langid")'
32 ) 32 )
33 33
34 identifier = LanguageIdentifier.from_modelstring(model, norm_probs=False) 34 identifier = LanguageIdentifier.from_modelstring(model, norm_probs=False)
35 35
36 36
43 C.PI_MAIN: "LangDetect", 43 C.PI_MAIN: "LangDetect",
44 C.PI_HANDLER: "no", 44 C.PI_HANDLER: "no",
45 C.PI_DESCRIPTION: _("""Detect and set message language when unknown"""), 45 C.PI_DESCRIPTION: _("""Detect and set message language when unknown"""),
46 } 46 }
47 47
48 CATEGORY = D_(u"Misc") 48 CATEGORY = D_("Misc")
49 NAME = u"lang_detect" 49 NAME = "lang_detect"
50 LABEL = D_(u"language detection") 50 LABEL = D_("language detection")
51 PARAMS = """ 51 PARAMS = """
52 <params> 52 <params>
53 <individual> 53 <individual>
54 <category name="{category_name}"> 54 <category name="{category_name}">
55 <param name="{name}" label="{label}" type="bool" value="true" /> 55 <param name="{name}" label="{label}" type="bool" value="true" />
61 ) 61 )
62 62
63 63
64 class LangDetect(object): 64 class LangDetect(object):
65 def __init__(self, host): 65 def __init__(self, host):
66 log.info(_(u"Language detection plugin initialization")) 66 log.info(_("Language detection plugin initialization"))
67 self.host = host 67 self.host = host
68 host.memory.updateParams(PARAMS) 68 host.memory.updateParams(PARAMS)
69 host.trigger.add("MessageReceived", self.MessageReceivedTrigger) 69 host.trigger.add("MessageReceived", self.MessageReceivedTrigger)
70 host.trigger.add("sendMessage", self.MessageSendTrigger) 70 host.trigger.add("sendMessage", self.MessageSendTrigger)
71 71
72 def addLanguage(self, mess_data): 72 def addLanguage(self, mess_data):
73 message = mess_data["message"] 73 message = mess_data["message"]
74 if len(message) == 1 and message.keys()[0] == "": 74 if len(message) == 1 and list(message.keys())[0] == "":
75 msg = message.values()[0] 75 msg = list(message.values())[0]
76 lang = identifier.classify(msg)[0] 76 lang = identifier.classify(msg)[0]
77 mess_data["message"] = {lang: msg} 77 mess_data["message"] = {lang: msg}
78 return mess_data 78 return mess_data
79 79
80 def MessageReceivedTrigger(self, client, message_elt, post_treat): 80 def MessageReceivedTrigger(self, client, message_elt, post_treat):