Created
February 25, 2014 07:51
-
-
Save aikoven/9204680 to your computer and use it in GitHub Desktop.
Switch input text layout
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 python | |
# -*- coding: utf-8 -*- | |
import sys | |
import codecs | |
DVORAK = ur"§[]',.pyfgcrl/=aoeuidhtns-\`;qjkxbmwvz" ur'±!@#$%^&*(){}"<>PYFGCRL?+AOEUIDHTNS_|~:QJKXBMWVZ' | |
RUSSIAN = ur"ё-=йцукенгшщзхъфывапролджэ\]ячсмитьбю." ur'Ё!"№;%:?*()_+ЙЦУКЕНГШЩЗХЪФЫВАПРОЛДЖЭ/[ЯЧСМИТЬБЮ,' | |
LAYOUTS = (DVORAK, RUSSIAN) | |
def translate(char, frm, to): | |
index = frm.find(char) | |
if index >= 0: | |
return to[index] | |
def occurrence(chars, text): | |
return sum(char in chars for char in text) | |
def translate_line(line): | |
idx, line_layout = max(enumerate(LAYOUTS), key=lambda (i, layout): occurrence(layout, line)) | |
next_layout = LAYOUTS[(idx + 1) % len(LAYOUTS)] | |
return ''.join(translate(char, line_layout, next_layout) or char for char in line) | |
if __name__ == '__main__': | |
arg = u' '.join(a.decode('utf-8') for a in sys.argv[1:]) | |
lines = [arg] if arg else codecs.getreader('utf-8')(sys.stdin) | |
print ''.join(translate_line(line).encode('utf-8') for line in lines), |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment