findarray30code/__main__.py

149 lines
4.3 KiB
Python
Raw Normal View History

2016-04-06 20:09:56 +08:00
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
2016-03-23 07:34:46 +08:00
import ui, ui2
2016-03-30 08:08:27 +08:00
from PyQt4 import QtGui, QtCore
2016-03-09 23:24:44 +08:00
2016-03-30 23:29:00 +08:00
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)
2016-04-06 20:09:56 +08:00
c.execute('''CREATE TABLE ime (code text, char text)''')
2016-03-10 04:21:10 +08:00
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-30 23:29:00 +08:00
code_with_index = [rawcode2truecode(i) for i in raw_code]
code = [i[0] for i in sorted(code_with_index,key= lambda x : x[1])]
return code
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-30 23:29:00 +08:00
raw_code_order = "QAZWSXEDCRFVTGBYHNUJMIK,OL.P;/"
2016-03-10 04:21:10 +08:00
true_code = ""
2016-03-30 20:27:21 +08:00
index = 0
2016-03-10 04:21:10 +08:00
for i in raw:
i_index = raw_code_order.index(i)
2016-04-06 20:09:56 +08:00
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]
2016-03-30 23:29:00 +08:00
row_number = i_index % 3
row = ['^','-','v'][row_number] # 0=^;1=-;2=v
column_and_row = column + row
true_code = true_code + column_and_row
2016-03-30 20:27:21 +08:00
index = index * 30 + i_index
2016-03-30 23:29:00 +08:00
index = (5-len(raw))*(30**5) + index
return true_code,index
2016-03-09 23:24:44 +08:00
2016-03-23 23:39:28 +08:00
class MainWindow(QtGui.QMainWindow, ui.Ui_MainWindow):
2016-03-23 07:34:46 +08:00
def __init__(self, parent=None):
2016-03-30 23:29:00 +08:00
self.db,self.c = import_all_table()
2016-03-23 07:34:46 +08:00
super(MainWindow, self).__init__(parent)
self.setupUi(self)
2016-03-23 23:39:28 +08:00
self.lineEdit.returnPressed.connect(self.input_characters)
self.pushButton.clicked.connect(self.input_characters)
2016-04-02 18:31:49 +08:00
self.pushButton_2.clicked.connect(self.clear_input)
self.action_About.triggered.connect(self.show_about)
2016-04-06 20:09:56 +08:00
2016-04-02 18:31:49 +08:00
def clear_input(self):
self.label_2.setText("")
self.lineEdit.clear()
def show_about(self):
about_dialog = QtGui.QWidget()
about_dialog_ui = ui2.Ui_Dialog()
about_dialog_ui.setupUi(about_dialog)
about_dialog.show()
2016-03-30 23:29:00 +08:00
2016-04-01 23:22:25 +08:00
def show_result(self,char_code):
result = ""
2016-04-02 18:31:49 +08:00
header = "<table style=\"vertical-align:top;\"><tr><td colspan=2>查碼結果:</td></tr>"
2016-04-01 23:22:25 +08:00
result = header + result
for (char,code) in char_code:
result = result + '<tr><td style="font-size:x-large;' + \
'vertical-align:top;">' + \
char + '</td><td>'
2016-04-06 20:09:56 +08:00
2016-04-01 23:22:25 +08:00
for i in range(len(code)):
if (i < len(code) - 1):
result = result + code[i] + '<br/>'
else:
result = result + code[i] + '</td></tr>'
2016-04-06 20:09:56 +08:00
2016-04-01 23:22:25 +08:00
result = result + '</table>'
self.label_2.setText(result)
2016-04-06 20:09:56 +08:00
2016-03-23 23:39:28 +08:00
def input_characters(self):
2016-03-30 20:27:21 +08:00
import re
2016-03-29 08:02:32 +08:00
characters = self.lineEdit.text()
2016-04-06 20:09:56 +08:00
chinese_char_pattern = re.compile(u"^((?<cjk>[一-鿌㐀-䶵𠀀-𪛖𪜀-𫜴𫝀-𫠝𫠠-𬺡])|[^\CJK\s\t\n]+)+$",re.UNICODE)
2016-03-30 20:27:21 +08:00
is_chinese_chars = chinese_char_pattern.match(characters)
2016-04-06 20:09:56 +08:00
if 1:
2016-03-30 23:29:00 +08:00
char_code_list = [(ch,find_code(ch,self.c)) for ch in characters]
2016-04-01 23:22:25 +08:00
self.show_result(char_code_list)
2016-03-30 08:08:27 +08:00
else:
print("error")
2016-04-06 20:09:56 +08:00
2016-03-23 07:34:46 +08:00
def main():
app = QtGui.QApplication(sys.argv)
2016-04-02 18:31:49 +08:00
main_window = MainWindow()
main_window.show()
2016-03-23 07:34:46 +08:00
app.exec_()
2016-04-06 20:09:56 +08:00
2016-03-30 23:29:00 +08:00
2016-03-23 07:34:46 +08:00
if __name__ == '__main__':
main()
2016-04-06 20:09:56 +08:00
2016-03-23 07:34:46 +08:00
2016-03-14 04:30:05 +08:00