diff --git a/config.ini.dist b/config.ini.dist index c761d2c..4c57655 100644 --- a/config.ini.dist +++ b/config.ini.dist @@ -26,6 +26,7 @@ redis_url = redis://localhost:6379/0 redis_prefix = botbot- plugin_dirs = /home/skull/dev/botbot-plugins plugin_blacklist = +lang = en [Links] mode = whitelist diff --git a/ircbot/plugins/translate.py b/ircbot/plugins/translate.py new file mode 100644 index 0000000..ffcc3f5 --- /dev/null +++ b/ircbot/plugins/translate.py @@ -0,0 +1,22 @@ +import requests +import json + +from langdetect import detect, detect_langs +from ircbot import bot + + +def translate(bot, text): + api_key = bot.config['Yandex']['translate_key'] + lang = detect(text) + translate_url = "https://translate.yandex.net/api/v1.5/tr.json/translate?key={}&text={}&lang={}-{}".format(api_key, text, lang, bot.config["System"]["lang"]) + api_response = requests.get(translate_url).text + api_json = json.loads(api_response) + print(api_json) + return api_json['text'][0] + + +@bot.hook() +def message_hook(bot, channel, sender, message): + # gibberish messages are still assigned a language, however the level of confidence is always less than 0.9 + if detect(message) != bot.config["System"]["lang"] and float(str(detect_langs(message)[0])[3:]) < 0.9: + bot.message(channel, "translation: {}").format(translate(bot, message)) diff --git a/ircbot/plugins/translate_ru.py b/ircbot/plugins/translate_ru.py deleted file mode 100644 index de20f80..0000000 --- a/ircbot/plugins/translate_ru.py +++ /dev/null @@ -1,31 +0,0 @@ -import requests -import json - -from ircbot import bot - -cyrillic_latin = {u'а': 'a', u'б': 'b', u'в': 'v', u'г': 'g', - u'д': 'd', u'е': 'ye', u'ё': 'yo', u'ж': 'zh', - u'з': 'z', u'и': 'i', u'й': 'j', u'к': 'k', - u'л': 'l', u'м': 'm', u'н': 'n', u'о': 'o', - u'п': 'p', u'р': 'r', u'с': 's', u'т': 't', - u'у': 'u', u'ф': 'f', u'х': 'h', u'ц': 'ts', - u'ч': 'ch', u'ш': 'sh', u'щ': 'sch', u'ы': 'i', - u'э': 'e', u'ю': 'yu', u'я': 'ya', u' ': ' ', - u'ъ': '', u'ь': ''} - - -def translate_text(bot, text): - api_key = bot.config['Yandex']['translate_key'] - api_url = "https://translate.yandex.net/api/v1.5/tr.json/translate?key={}&text={}&lang=ru-en".format(api_key, text) - api_response = requests.get(api_url).text - api_json = json.loads(api_response) - print(api_json) - return api_json['text'][0] - - -@bot.hook() -def message_hook(bot, channel, sender, message): - if set(message) <= set(cyrillic_latin.keys()) and message.strip(): - translit = ''.join(str(c) for c in map(lambda x: cyrillic_latin[x], message)) - translate = translate_text(bot, message) - bot.message(channel, '{} <{}> ~ {}'.format(message, translit, translate)) diff --git a/requirements.txt b/requirements.txt index 631d832..b87a334 100644 --- a/requirements.txt +++ b/requirements.txt @@ -25,3 +25,4 @@ traitlets==4.0.0 urbandictionary==1.1 dnspython==1.15.0 raven==6.4.0 +langdetect==1.0.7