findarray30code/__main__.py

82 lines
2.1 KiB
Python
Raw Normal View History

2016-03-15 02:01:06 +08:00
#!/usr/bin/env python3
2016-03-09 23:24:44 +08:00
#-*-coding:utf-8-*-
import sqlite3
2016-03-15 02:01:06 +08:00
import sys
from PyQt4 import QtGui, uic
2016-03-09 23:24:44 +08:00
def table2list(table_tsv):
with open(table_tsv,'r',encoding='utf-16') as tsv:
code_list = []
for line in tsv:
line_stripped = line.strip()
code_char_mapping = tuple(line_stripped.split())
code_list.append(code_char_mapping)
return code_list
2016-03-10 04:21:10 +08:00
def connect_db(db_filename):
db = sqlite3.connect(db_filename)
c = db.cursor()
return db,c
2016-03-09 23:24:44 +08:00
2016-03-10 04:21:10 +08:00
def create_new_db(db_filename):
db, c = connect_db(db_filename)
c.execute('''CREATE TABLE ime (code text, char text)''')
return db, c
2016-03-09 23:24:44 +08:00
2016-03-10 04:21:10 +08:00
def list2sqlite(code_list,c):
2016-03-09 23:24:44 +08:00
c.executemany(
'INSERT INTO ime VALUES (?,?)', code_list)
def import_all_table():
import os
import re
2016-03-10 04:21:10 +08:00
main_dirname = os.path.dirname(os.path.abspath(__file__))
db ,c = create_new_db(':memory:')
table_folder = os.path.join(main_dirname, 'table')
table_folder_files = os.listdir(table_folder)
for file in table_folder_files:
2016-03-09 23:24:44 +08:00
if re.match('.+\.txt$',file):
2016-03-10 04:21:10 +08:00
2016-03-09 23:24:44 +08:00
file_path = os.path.join(table_folder,file)
list = table2list(file_path)
2016-03-10 04:21:10 +08:00
list2sqlite(list,c)
2016-03-09 23:24:44 +08:00
2016-03-10 04:21:10 +08:00
db.commit()
2016-03-14 04:30:05 +08:00
return db, c
2016-03-09 23:24:44 +08:00
2016-03-10 04:21:10 +08:00
def find_code(char,c):
2016-03-09 23:24:44 +08:00
2016-03-14 04:30:05 +08:00
raw_query = c.execute('''SELECT code FROM ime WHERE char = ?''', (char,))
raw_code = [i[0] for i in raw_query.fetchall()]
2016-03-10 04:21:10 +08:00
code = [rawcode2truecode(i) for i in raw_code]
2016-03-14 04:30:05 +08:00
code = sorted(code,reverse=True)
print(code)
2016-03-10 04:21:10 +08:00
return code, c
2016-03-09 23:24:44 +08:00
2016-03-14 04:30:05 +08:00
2016-03-10 04:21:10 +08:00
def rawcode2truecode(raw):
2016-03-14 04:30:05 +08:00
#1^ = Q, 1- = A, 1v = Z, 2^ = W, 2- = S ......, 0^ = P, 0- = :, 0v = ?
2016-03-10 04:21:10 +08:00
raw_code_order = "QAZWSXEDCRFVTGBYHNUJMIK<OL>P:?"
true_code = ""
2016-03-09 23:24:44 +08:00
2016-03-10 04:21:10 +08:00
for i in raw:
i_index = raw_code_order.index(i)
2016-03-14 04:30:05 +08:00
uncorrected_column = i_index // 3
#correct the column no. 2 -> 3; 9 -> 0
column = str(uncorrected_column + 1)[-1]
raw_number = i_index % 3
raw = ['^','-','v'][raw_number] # 0=^;1=-;2=v
2016-03-10 04:21:10 +08:00
column_and_raw = column + raw
true_code = true_code + column_and_raw
2016-03-09 23:24:44 +08:00
2016-03-10 04:21:10 +08:00
return true_code
2016-03-09 23:24:44 +08:00
2016-03-14 04:30:05 +08:00
db,c = import_all_table()
find_code("",c)