2018-11-20 11:38:30 +00:00
|
|
|
import enum
|
|
|
|
from src import ModuleManager, utils
|
|
|
|
|
|
|
|
class Script(enum.Enum):
|
|
|
|
Unknown = 0
|
|
|
|
Latin = 1
|
|
|
|
Cyrillic = 2
|
|
|
|
Greek = 3
|
2018-11-20 11:44:13 +00:00
|
|
|
Armenian = 4
|
2018-11-20 12:00:47 +00:00
|
|
|
FullWidth = 5
|
2018-11-20 12:07:54 +00:00
|
|
|
Coptic = 6
|
2018-11-20 12:29:03 +00:00
|
|
|
Cherokee = 7
|
|
|
|
TaiLe = 8
|
2018-11-20 11:38:30 +00:00
|
|
|
WORD_SEPERATORS = [",", " ", "\t", "."]
|
|
|
|
|
2018-11-20 13:08:47 +00:00
|
|
|
SCORE_LENGTH = 100
|
|
|
|
|
2018-11-20 11:38:30 +00:00
|
|
|
class Module(ModuleManager.BaseModule):
|
|
|
|
def _detect_script(self, char):
|
|
|
|
point = ord(char)
|
2018-11-20 12:18:56 +00:00
|
|
|
# NULL .. LATIN SMALL LETTER TURNED H WITH FISHHOOK AND TAIL
|
|
|
|
if 0x0000 <= point <= 0x02AF:
|
2018-11-20 11:38:30 +00:00
|
|
|
return Script.Latin
|
2018-11-20 12:18:56 +00:00
|
|
|
# GREEK CAPITAL LETTER HETA .. GREEK CAPITAL REVERSED DOTTED LUNATE SIGMA SYMBOL
|
|
|
|
elif 0x0370 <= point <= 0x03ff:
|
2018-11-20 11:38:30 +00:00
|
|
|
return Script.Greek
|
2018-11-20 12:18:56 +00:00
|
|
|
# CYRILLIC CAPITAL LETTER IE WITH GRAVE .. CYRILLIC SMALL LETTER EL WITH DESCENDER
|
|
|
|
elif 0x0400 <= point <= 0x052F:
|
2018-11-20 11:38:30 +00:00
|
|
|
return Script.Cyrillic
|
2018-11-20 12:18:56 +00:00
|
|
|
# ARMENIAN CAPITAL LETTER AYB .. ARMENIAN HYPHEN
|
|
|
|
elif 0x0531 <= point <= 0x058A:
|
2018-11-20 11:44:13 +00:00
|
|
|
return Script.Armenian
|
2018-11-20 12:18:56 +00:00
|
|
|
# FULLWIDTH EXCLAMATION MARK .. FULLWIDTH RIGHT WHITE PARENTHESIS
|
|
|
|
elif 0xFF01 <= point <= 0xff60:
|
2018-11-20 12:00:47 +00:00
|
|
|
return Script.FullWidth
|
2018-11-20 12:07:54 +00:00
|
|
|
# COPTIC CAPITAL LETTER ALFA .. COPTIC MORPHOLOGICAL DIVIDER
|
|
|
|
elif 0x2C80 <= point <= 0x2CFF:
|
|
|
|
return Script.Coptic
|
2018-11-20 12:29:03 +00:00
|
|
|
# CHEROKEE LETTER A .. CHEROKEE SMALL LETTER MV
|
|
|
|
elif 0x13A0 <= point <= 0x13FD:
|
|
|
|
return Script.Cherokee
|
|
|
|
# TAI LE LETTER KA .. U+197F
|
|
|
|
elif 0x1950 <= point <= 0x197F:
|
|
|
|
return Script.TaiLe
|
2018-11-20 11:38:30 +00:00
|
|
|
return Script.Unknown
|
|
|
|
|
|
|
|
@utils.hook("received.message.channel")
|
|
|
|
def channel_message(self, event):
|
|
|
|
last_script = None
|
|
|
|
last_was_separator = False
|
|
|
|
score = 0
|
|
|
|
|
|
|
|
for char in event["message"]:
|
|
|
|
if char in WORD_SEPERATORS:
|
|
|
|
last_was_separator = True
|
|
|
|
else:
|
|
|
|
script = self._detect_script(char)
|
|
|
|
if not script == Script.Unknown:
|
|
|
|
if last_script and not script == last_script:
|
|
|
|
score += 1
|
|
|
|
if not last_was_separator:
|
|
|
|
score += 1
|
|
|
|
|
|
|
|
last_script = script
|
|
|
|
|
|
|
|
last_was_separator = False
|
2018-11-20 12:42:45 +00:00
|
|
|
|
2018-11-20 13:08:47 +00:00
|
|
|
score = score/(len(event["message"])/SCORE_LENGTH)
|
2018-11-20 12:42:45 +00:00
|
|
|
if score > 0:
|
|
|
|
self.log.trace("Message given a mixed-unicode score of %d", [score])
|