Created
January 29, 2018 03:18
-
-
Save someoneigna/b58c3c8746df93b5a4a7b1b8fd2d0dca to your computer and use it in GitHub Desktop.
Discord text to emotes translator.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python3 | |
''' Translates text into discord emotes. ''' | |
''' Ex: python translate_to_emotes.py "Write with joy!" ''' | |
import sys | |
import string | |
import re | |
SEPARATOR = ' ' | |
symbol_map = {'!': ':exclamation:', '?': ':question:', '*': ':asterisk:'} | |
word_emotes = {'sad': ':feelssadman:', 'joy': ':joy:', 'banana': ':banana:',\ | |
'smile': ':smile:', 'cry': ':cry:', 'facepalm': ':facepalm:'} | |
def _map_regional_indicator(char): | |
return ':regional_indicator_' + char + ':' | |
regional_indicator = {k: _map_regional_indicator(k) for k in string.ascii_lowercase} | |
def _process_word(word, space=True, ending=True): | |
if _contains_symbol(word): | |
tokens = _tokenize(word) | |
for i, t in enumerate(tokens): | |
if t in symbol_map: tokens[i] = symbol_map[t] + (SEPARATOR if i is len(tokens)-1 else '') | |
else: tokens[i] = _process_word(tokens[i], ending=(True if i is len(tokens)-1 else False)) | |
return ''.join(tokens) | |
text = [] | |
if word in word_emotes: text.append(word_emotes[word] + SEPARATOR) | |
else: | |
for char in word.lower(): | |
if char in symbol_map: text.append(symbol_map[char]) | |
elif char == ' ': text.append(' ') | |
elif char in regional_indicator: text.append(regional_indicator[char] + (' ' if space else '')) | |
if ending: text.append(SEPARATOR) | |
return ''.join(text) | |
def _contains_symbol(word): | |
symbols = '*?!¿¡' | |
for char in word: | |
if char in symbols: return True | |
return False | |
def _tokenize(word): | |
tokens = re.split('([!?¿¡])+', word) | |
return tokens | |
def translate(word): | |
text = [] | |
words = word.split(' ') | |
text = map(_process_word, words) | |
return ' '.join(text) | |
def main(): | |
if len(sys.argv) > 1: | |
print(translate(sys.argv[1])) | |
else: | |
print('Pass the string you want to translate into emotes') | |
if __name__ == '__main__': | |
main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment