我试图用不同的键盘布局来表示输入字符串 例如: 输入:kak sme 输出:kak sme,ньнсме,,ньняпе,каксме,каксме
当输入是拉丁语时一切正常但是当它是西里尔语时输出与输入相同
我的代码
def mreplace(s, chararray, newchararray):
slist=list(s)
dlist=list(chararray)
rlist=list(newchararray)
for x in range(0, len(slist)):
charitem=slist[x]
if charitem in dlist:
index=dlist.index(charitem)
try:
slist[x]=rlist[index]
except ValueError:
pass
return ''.join(slist)
def RetJ(input):
a = []
temp_arr = []
keymap = [];
#US
keymap.append(["q","w","e","r","t","y","u","i","o","p","[","]","a","s","d","f","g","h","j","k","l",";","\"","\\","z","x","c","v","b","n","m",",",".","/"])
#BG
keymap.append([",","у","е","и","ш","щ","к","с","д","з","ц",";","ь","я","а","о","ж","г","т","н","в","м","ч","„","ю","й","ъ","э","ф","х","п","р","л","б"])
#BG Phonetic
keymap.append(["ч","ш","е","р","т","ъ","у","и","о","п","я","щ","а","с","д","ф","г","х","й","к","л",";","\"","ь","з","ж","ц","в","б","н","м",",",".","/"])
#BG Tradic Phonetic
keymap.append(["я","в","е","р","т","ъ","у","и","о","п","ш","щ","а","с","д","ф","г","х","й","к","л",";","\"","ю","з","ь","ц","ж","б","н","м",",",".","/"])
#BG Typewriter
keymap.append([",","у","е","и","ш","щ","к","с","д","з","ц",";","ь","я","а","о","ж","г","т","н","в","м","ч","(","ю","й","ъ","э","ф","х","п","р","л","б"])
temp = ''
for map1 in keymap:
for map2 in keymap:
temp = mreplace(input,map1,map2)
if temp not in temp_arr:
temp_arr.append(temp)
a.append({"value":"2", "n":temp, "p":"ppp"})
#a = [{"o1":'1'},{"o2":'2'},{"o3":'3'},{"o4":'4'}]
return a;
答案 0 :(得分:0)
嗨,我终于找到了一种工作方式 这是代码
# -*- coding: utf-8 -*- import string import array def mreplace(s, chararray, newchararray): if type(s) == type(u''): slist = s else: slist = unicode(s, "utf-8") dlist=list(chararray) rlist=list(newchararray) new_s = '' for x in range(0, len(slist)): i = 0 is_replace = False; for och in dlist: if slist[x] == unicode(och, "utf-8"): new_s = new_s + rlist[i] is_replace = True; i = i + 1 if is_replace == False: new_s = new_s + slist[x].encode("utf-8") return unicode(new_s, "utf-8") def rj(input): a = [] keymap = []; #US keymap.append(["q","w","e","r","t","y","u","i","o","p","[","]","a","s","d","f","g","h","j","k","l",";","\"","\\","z","x","c","v","b","n","m",",",".","/"]) #BG keymap.append([",","у","е","и","ш","щ","к","с","д","з","ц",";","ь","я","а","о","ж","г","т","н","в","м","ч","„","ю","й","ъ","э","ф","х","п","р","л","б"]) #BG Phonetic keymap.append(["ч","ш","е","р","т","ъ","у","и","о","п","я","щ","а","с","д","ф","г","х","й","к","л",";","\"","ь","з","ж","ц","в","б","н","м",",",".","/"]) #BG Tradic Phonetic keymap.append(["я","в","е","р","т","ъ","у","и","о","п","ш","щ","а","с","д","ф","г","х","й","к","л",";","\"","ю","з","ь","ц","ж","б","н","м",",",".","/"]) #BG Typewriter keymap.append([",","у","е","и","ш","щ","к","с","д","з","ц",";","ь","я","а","о","ж","г","т","н","в","м","ч","(","ю","й","ъ","э","ф","х","п","р","л","б"]) for map1 in keymap: for map2 in keymap: temp = mreplace(input,map1,map2) if temp not in a: a.append(temp) return a input = u'гс пщ хьпе ся хсндвьс ьха счп оидп фквжьись' output = rj(input) print(output)
如果我们想写
hi my name is,但我们采用保加利亚语键盘布局,我们会写
гс пщ хьпе ся当我们使用
гс пщ хьпе ся调用rj时,其中一个输出将
hi my name is
如果我们尝试纠正用户输入那就非常好
每个需要php版本的人 这里
function keymap($str) { $keymap = array(); //US $keymap[] = array('q','w','e','r','t','y','u','i','o','p','[',']','a','s','d','f','g','h','j','k','l',';','\'','\\','z','x','c','v','b','n','m',',','.','/'); //BG $keymap[] = array(',','у','е','и','ш','щ','к','с','д','з','ц',';','ь','я','а','о','ж','г','т','н','в','м','ч','„','ю','й','ъ','э','ф','х','п','р','л','б'); //BG Phonetic $keymap[] = array('ч','ш','е','р','т','ъ','у','и','о','п','я','щ','а','с','д','ф','г','х','й','к','л',';','\'','ь','з','ж','ц','в','б','н','м',',','.','/'); //BG Tradic Phonetic $keymap[] = array('я','в','е','р','т','ъ','у','и','о','п','ш','щ','а','с','д','ф','г','х','й','к','л',';','\'','ю','з','ь','ц','ж','б','н','м',',','.','/'); //BG Typewriter $keymap[] = array(',','у','е','и','ш','щ','к','с','д','з','ц',';','ь','я','а','о','ж','г','т','н','в','м','ч','(','ю','й','ъ','э','ф','х','п','р','л','б'); $out = array(); $temp = ''; foreach($keymap as $map1) { foreach($keymap as $map2) { $temp = str_replace($map1, $map2, $str); if (!in_array($temp, $out)) { $out[] = $temp; } } } return $out; } echo "<pre>"; print_r(keymap('гс пщ хьпе ся хсндвьс')); echo "</pre>";