Skip to content

Commit

Permalink
Add wtf module (detect language of a text)
Browse files Browse the repository at this point in the history
  • Loading branch information
aluminiumgeek committed Mar 29, 2016
1 parent 82bcd32 commit 4dd44cf
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 0 deletions.
47 changes: 47 additions & 0 deletions modules/user_wtf.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
import json
import requests


def main(bot, *args, **kwargs):
"""
wtf [text]
Detect language of a text or show avialable languages if no text given.
See also: tr
"""

mashape_key = getattr(bot.settings, 'mashape_key', None)
if not mashape_key:
return 'Mashape key is not provided'
return detect_lang(' '.join(args), mashape_key)


def detect_lang(text, mashape_key):
google_langs =\
{ 'af':'Afrikaans','sq':'Albanian','am':'Amharic','ar':'Arabic','hy':'Armenian','az':'Azerbaijani','eu':'Basque'
, 'be':'Belarusian','bn':'Bengali','bh':'Bihari','bg':'Bulgarian','my':'Burmese','ca':'Catalan','chr':'Cherokee'
, 'zh':'Chinese','zh-CN':'Chinese_simplified','zh-TW':'Chinese_traditional','hr':'Croatian','cs':'Czech','da':'Danish'
, 'dv':'Dhivehi','nl':'Dutch','en':'English','eo':'Esperanto','et':'Estonian','tl':'Filipino','fi':'Finnish'
, 'fr':'French','gl':'Galician','ka':'Georgian','de':'German','el':'Greek','gn':'Guarani','gu':'Gujarati','iw':'Hebrew'
, 'hi':'Hindi','hu':'Hungarian','is':'Icelandic','id':'Indonesian','iu':'Inuktitut','it':'Italian','ja':'Japanese'
, 'kn':'Kannada','kk':'Kazakh','km':'Khmer','ko':'Korean','ku':'Kurdish','ky':'Kyrgyz','lo':'Laothian','lv':'Latvian'
, 'lt':'Lithuanian','mk':'Macedonian','ms':'Malay','ml':'Malayalam','mt':'Maltese','mr':'Marathi','mn':'Mongolian'
, 'ne':'Nepali','no':'Norwegian','or':'Oriya','ps':'Pashto','fa':'Persian','pl':'Polish','pt-PT':'Portuguese'
, 'pa':'Punjabi','ro':'Romanian','ru':'Russian','sa':'Sanskrit','sr':'Serbian','sd':'Sindhi','si':'Sinhalese'
, 'sk':'Slovak','sl':'Slovenian','es':'Spanish','sw':'Swahili','sv':'Swedish','tg':'Tajik','ta':'Tamil','tl':'Tagalog'
, 'te':'Telugu','th':'Thai','bo':'Tibetan','tr':'Turkish','uk':'Ukrainian','ur':'Urdu','uz':'Uzbek','ug':'Uighur','vi':'Vietnamese'
}

if not text:
return ", ".join(["%s (%s)" %(v, k) for k, v in google_langs.items()])

headers = {'X-Mashape-Key': mashape_key}
data = requests.get('https://langdetect.p.mashape.com/language?mode=json&text=%s' % (text), headers=headers).content.decode('utf-8')
if not data:
return "Can't get data"
try:
data = json.loads(data)
probes = filter(lambda x: x['code'] != '?', data['probs'])
probes = map(lambda x: '%s (%.4f)' % (x['code'], x['score']), probes)
return 'Lang: {}\nProbabilities: {}'.format(data['lang'], ', '.join(probes))
except:
return "Can't detect this shit!"
1 change: 1 addition & 0 deletions settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,3 +12,4 @@
# Modules specific
azure_bing_api_key = '' # for `img` module
google_api_key = '' # for `g` module
mashape_key = '' # for `wtf` module

0 comments on commit 4dd44cf

Please sign in to comment.