1
0
mirror of https://github.com/nvbn/thefuck.git synced 2025-01-31 10:11:14 +00:00

Add support for switch_lang for Korean. (#981)

* switch korean letters to english

* revised according to recent changes

* Fix typo in tests/test_switch_lang.py

* Add a test case for coverage

* Change: Moved decomposing logic which changes command.script to get_new_command instead of match.

* Fix: changed unicode characters to unicode string for python2 compatibility.

* Fix: Modified to change request.

@ik1ne @yangkyeongmo
This commit is contained in:
ik1ne 2019-10-19 22:03:21 +09:00 committed by Vladimir Iakovlev
parent 7a9d87f502
commit 581a292797
2 changed files with 53 additions and 7 deletions

View File

@ -1,6 +1,7 @@
# -*- encoding: utf-8 -*- # -*- encoding: utf-8 -*-
import pytest import pytest
from thefuck.rules import switch_lang from thefuck.rules import switch_lang
from thefuck.types import Command from thefuck.types import Command
@ -9,7 +10,8 @@ from thefuck.types import Command
Command(u'фзе-пуе', 'command not found: фзе-пуе'), Command(u'фзе-пуе', 'command not found: фзе-пуе'),
Command(u'λσ', 'command not found: λσ'), Command(u'λσ', 'command not found: λσ'),
Command(u'שפא-עקא', 'command not found: שפא-עקא'), Command(u'שפא-עקא', 'command not found: שפא-עקא'),
Command(u'ךד', 'command not found: ךד')]) Command(u'ךד', 'command not found: ךד'),
Command(u'녀애 ㅣㄴ', 'command not found: 녀애 ㅣㄴ')])
def test_match(command): def test_match(command):
assert switch_lang.match(command) assert switch_lang.match(command)
@ -19,7 +21,8 @@ def test_match(command):
Command(u'ls', 'command not found: ls'), Command(u'ls', 'command not found: ls'),
Command(u'агсл', 'command not found: агсл'), Command(u'агсл', 'command not found: агсл'),
Command(u'фзе-пуе', 'some info'), Command(u'фзе-пуе', 'some info'),
Command(u'שפא-עקא', 'some info')]) Command(u'שפא-עקא', 'some info'),
Command(u'녀애 ㅣㄴ', 'some info')])
def test_not_match(command): def test_not_match(command):
assert not switch_lang.match(command) assert not switch_lang.match(command)
@ -28,6 +31,9 @@ def test_not_match(command):
(Command(u'фзе-пуе штыефдд мшь', ''), 'apt-get install vim'), (Command(u'фзе-пуе штыефдд мшь', ''), 'apt-get install vim'),
(Command(u'λσ -λα', ''), 'ls -la'), (Command(u'λσ -λα', ''), 'ls -la'),
(Command(u'שפא-עקא ןמדאשךך הןצ', ''), 'apt-get install vim'), (Command(u'שפא-עקא ןמדאשךך הןצ', ''), 'apt-get install vim'),
(Command(u'ךד -ךש', ''), 'ls -la')]) (Command(u'ךד -ךש', ''), 'ls -la'),
(Command(u'멧-ㅎㄷㅅ ㅑㅜㄴㅅ미ㅣ 퍄ㅡ', ''), 'apt-get install vim'),
(Command(u'ㅣㄴ -ㅣㅁ', ''), 'ls -la'),
(Command(u'ㅔㅁㅅ촤', ''), 'patchk'), ])
def test_get_new_command(command, new_command): def test_get_new_command(command, new_command):
assert switch_lang.get_new_command(command) == new_command assert switch_lang.get_new_command(command) == new_command

View File

@ -5,13 +5,14 @@ target_layout = '''qwertyuiop[]asdfghjkl;'zxcvbnm,./QWERTYUIOP{}ASDFGHJKL:"ZXCVB
# any new keyboard layout must be appended # any new keyboard layout must be appended
greek = u''';ςερτυθιοπ[]ασδφγηξκλ΄ζχψωβνμ,./:΅ΕΡΤΥΘΙΟΠ{}ΑΣΔΦΓΗΞΚΛ¨"ΖΧΨΩΒΝΜ<>?''' greek = u''';ςερτυθιοπ[]ασδφγηξκλ΄ζχψωβνμ,./:΅ΕΡΤΥΘΙΟΠ{}ΑΣΔΦΓΗΞΚΛ¨"ΖΧΨΩΒΝΜ<>?'''
korean = u'''ㅂㅈㄷㄱㅅㅛㅕㅑㅐㅔ[]ㅁㄴㅇㄹㅎㅗㅓㅏㅣ;'ㅋㅌㅊㅍㅠㅜㅡ,./ㅃㅉㄸㄲㅆㅛㅕㅑㅒㅖ{}ㅁㄴㅇㄹㅎㅗㅓㅏㅣ:"ㅋㅌㅊㅍㅠㅜㅡ<>?'''
source_layouts = [u'''йцукенгшщзхъфывапролджэячсмитьбю.ЙЦУКЕНГШЩЗХЪФЫВАПРОЛДЖЭЯЧСМИТЬБЮ,''', source_layouts = [u'''йцукенгшщзхъфывапролджэячсмитьбю.ЙЦУКЕНГШЩЗХЪФЫВАПРОЛДЖЭЯЧСМИТЬБЮ,''',
u'''йцукенгшщзхїфівапролджєячсмитьбю.ЙЦУКЕНГШЩЗХЇФІВАПРОЛДЖЄЯЧСМИТЬБЮ,''', u'''йцукенгшщзхїфівапролджєячсмитьбю.ЙЦУКЕНГШЩЗХЇФІВАПРОЛДЖЄЯЧСМИТЬБЮ,''',
u'''ضصثقفغعهخحجچشسیبلاتنمکگظطزرذدپو./ًٌٍَُِّْ][}{ؤئيإأآة»«:؛كٓژٰ‌ٔء><؟''', u'''ضصثقفغعهخحجچشسیبلاتنمکگظطزرذدپو./ًٌٍَُِّْ][}{ؤئيإأآة»«:؛كٓژٰ‌ٔء><؟''',
u'''/'קראטוןםפ][שדגכעיחלךף,זסבהנמצתץ.QWERTYUIOP{}ASDFGHJKL:"ZXCVBNM<>?''', u'''/'קראטוןםפ][שדגכעיחלךף,זסבהנמצתץ.QWERTYUIOP{}ASDFGHJKL:"ZXCVBNM<>?''',
greek] greek,
korean]
source_to_target = { source_to_target = {
greek: {u';': "q", u'ς': "w", u'ε': "e", u'ρ': "r", u'τ': "t", u'υ': "y", greek: {u';': "q", u'ς': "w", u'ε': "e", u'ρ': "r", u'τ': "t", u'υ': "y",
@ -30,6 +31,19 @@ source_to_target = {
u'Ώ': "V"}, u'Ώ': "V"},
} }
'''Lists used for decomposing korean letters.'''
HEAD_LIST = [u'', u'', u'', u'', u'', u'', u'', u'', u'', u'', u'',
u'', u'', u'', u'', u'', u'', u'', u'']
BODY_LIST = [u'', u'', u'', u'', u'', u'', u'', u'', u'', u'', u'',
u'', u'', u'', u'', u'', u'', u'', u'', u'', u'']
TAIL_LIST = [u' ', u'', u'', u'', u'', u'', u'', u'', u'', u'', u'',
u'', u'', u'', u'', u'', u'', u'', u'', u'', u'', u'', u'',
u'', u'', u'', u'', u'']
DOUBLE_LIST = [u'', u'', u'', u'', u'', u'', u'', u'', u'', u'', u'',
u'', u'', u'', u'', u'', u'']
DOUBLE_MOD_LIST = [u'ㅗㅏ', u'ㅗㅐ', u'ㅗㅣ', u'ㅜㅓ', u'ㅜㅔ', u'ㅜㅣ', u'ㅡㅣ', u'ㄱㅅ',
u'ㄴㅈ', u'ㄴㅎ', u'ㄹㄱ', u'ㄹㅁ', u'ㄹㅂ', u'ㄹㅅ', u'ㄹㅌ', u'ㄹㅎ', u'ㅂㅅ']
@memoize @memoize
def _get_matched_layout(command): def _get_matched_layout(command):
@ -50,7 +64,6 @@ def _get_matched_layout(command):
def _switch(ch, layout): def _switch(ch, layout):
if ch in layout: if ch in layout:
return target_layout[layout.index(ch)] return target_layout[layout.index(ch)]
else:
return ch return ch
@ -63,9 +76,33 @@ def _switch_command(command, layout):
return ''.join(_switch(ch, layout) for ch in command.script) return ''.join(_switch(ch, layout) for ch in command.script)
def _decompose_korean(command):
def _change_double(ch):
if ch in DOUBLE_LIST:
return DOUBLE_MOD_LIST[DOUBLE_LIST.index(ch)]
return ch
hg_str = u''
for ch in command.script:
if u'' <= ch <= u'':
ord_ch = ord(ch) - ord(u'')
hd = ord_ch // 588
bd = (ord_ch - 588 * hd) // 28
tl = ord_ch - 588 * hd - 28 * bd
for ch in [HEAD_LIST[hd], BODY_LIST[bd], TAIL_LIST[tl]]:
if ch != ' ':
hg_str += _change_double(ch)
else:
hg_str += _change_double(ch)
return hg_str
def match(command): def match(command):
if 'not found' not in command.output: if 'not found' not in command.output:
return False return False
if any(u'' <= ch <= u'' or u'' <= ch <= u'' or u'' <= ch <= u''
for ch in command.script):
return True
matched_layout = _get_matched_layout(command) matched_layout = _get_matched_layout(command)
return (matched_layout and return (matched_layout and
@ -73,5 +110,8 @@ def match(command):
def get_new_command(command): def get_new_command(command):
if any(u'' <= ch <= u'' or u'' <= ch <= u'' or u'' <= ch <= u''
for ch in command.script):
command.script = _decompose_korean(command)
matched_layout = _get_matched_layout(command) matched_layout = _get_matched_layout(command)
return _switch_command(command, matched_layout) return _switch_command(command, matched_layout)