1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181
|
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
#
# cin2db - Convert cin table to sqlite db
#
# Copyright (c) 2018 Keng-Yu Lin <kengyu@lexical.tw>
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2, or
# any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License along
# with this program; if not, write to the Free Software Foundation, Inc.,
# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
import sqlite3 as sqlite
from sys import argv
REGION_UNIFIED_Base = 1
REGION_ARRAY_SPECIAL = 2
REGION_ARRAY_COMPATIBLE = 3
REGION_UNIFIED_ExtA = 4
REGION_UNIFIED_ExtB = 5
REGION_UNIFIED_ExtC = 6
REGION_UNIFIED_ExtD = 7
REGION_UNIFIED_ExtE = 8
REGION_UNIFIED_ExtF = 9
REGION_UNIFIED_ExtG = 10
REGION_ARRAY_SYMBOL = 11
STR_UNIFIED_Base = "CJK Unified Ideographs Base"
STR_ARRAY_SPECIAL = "Special Codes"
STR_ARRAY_COMPATIBLE = "Compatible Input Codes"
STR_UNIFIED_ExtA = "CJK Unified Ideographs Extension A"
STR_UNIFIED_ExtB = "CJK Unified Ideographs Extension B"
STR_UNIFIED_ExtC = "CJK Unified Ideographs Extension C"
STR_UNIFIED_ExtD = "CJK Unified Ideographs Extension D"
STR_UNIFIED_ExtE = "CJK Unified Ideographs Extension E"
STR_UNIFIED_ExtF = "CJK Unified Ideographs Extension F"
STR_UNIFIED_ExtG = "CJK Unified Ideographs Extension G"
STR_ARRAY_SYMBOL = "CJK Symbols & Punctuation (w+0~9)"
REG_STACK = []
def array_updatedb(table_file):
con = sqlite.connect("array.db")
cur = con.cursor()
# read from the text table
f = open(table_file, 'r')
for ln in f.readlines():
ln = ln.strip()
print("This is ln: " + ln)
if (ln == "%chardef begin"):
print("This is begin")
#REG_STACK.append(-1)
if (ln == "# Begin of " + STR_UNIFIED_Base):
print("Enter: " + STR_UNIFIED_Base)
REG_STACK.append(REGION_UNIFIED_Base)
elif(ln == "# Begin of " + STR_ARRAY_SPECIAL):
print("Enter: " + STR_ARRAY_SPECIAL)
REG_STACK.append(REGION_ARRAY_SPECIAL)
elif(ln == "# Begin of " + STR_ARRAY_COMPATIBLE):
print("Enter: " + STR_ARRAY_COMPATIBLE)
REG_STACK.append(REGION_ARRAY_COMPATIBLE)
elif(ln == "# Begin of " + STR_UNIFIED_ExtA):
print("Enter: " + STR_UNIFIED_ExtA)
REG_STACK.append(REGION_UNIFIED_ExtA)
elif(ln == "# Begin of " + STR_UNIFIED_ExtB):
print("Enter: " + STR_UNIFIED_ExtB)
REG_STACK.append(REGION_UNIFIED_ExtB)
elif(ln == "# Begin of " + STR_UNIFIED_ExtC):
print("Enter: " + STR_UNIFIED_ExtC)
REG_STACK.append(REGION_UNIFIED_ExtC)
elif(ln == "# Begin of " + STR_UNIFIED_ExtD):
print("Enter: " + STR_UNIFIED_ExtD)
REG_STACK.append(REGION_UNIFIED_ExtD)
elif(ln == "# Begin of " + STR_UNIFIED_ExtE):
print("Enter: " + STR_UNIFIED_ExtE)
REG_STACK.append(REGION_UNIFIED_ExtE)
elif(ln == "# Begin of " + STR_UNIFIED_ExtF):
print("Enter: " + STR_UNIFIED_ExtF)
REG_STACK.append(REGION_UNIFIED_ExtF)
elif(ln == "# Begin of " + STR_UNIFIED_ExtG):
print("Enter: " + STR_UNIFIED_ExtG)
REG_STACK.append(REGION_UNIFIED_ExtG)
elif(ln == "# Begin of " + STR_ARRAY_SYMBOL):
print("Enter: " + STR_ARRAY_SYMBOL)
REG_STACK.append(REGION_ARRAY_SYMBOL)
elif(ln == "# End of " + STR_UNIFIED_Base):
print("Exit: " + STR_UNIFIED_Base)
REG_STACK.pop()
elif(ln == "# End of " + STR_ARRAY_SPECIAL):
print("Exit: " + STR_ARRAY_SPECIAL)
REG_STACK.pop()
elif(ln == "# End of " + STR_ARRAY_COMPATIBLE):
print("Exit: " + STR_ARRAY_COMPATIBLE)
REG_STACK.pop()
elif(ln == "# End of " + STR_UNIFIED_ExtA):
print("Exit: " + STR_UNIFIED_ExtA)
REG_STACK.pop()
elif(ln == "# End of " + STR_UNIFIED_ExtB):
print("Exit: " + STR_UNIFIED_ExtB)
REG_STACK.pop()
elif(ln == "# End of " + STR_UNIFIED_ExtC):
print("Exit: " + STR_UNIFIED_ExtC)
REG_STACK.pop()
elif(ln == "# End of " + STR_UNIFIED_ExtD):
print("Exit: " + STR_UNIFIED_ExtD)
REG_STACK.pop()
elif(ln == "# End of " + STR_UNIFIED_ExtE):
print("Exit: " + STR_UNIFIED_ExtE)
REG_STACK.pop()
elif(ln == "# End of " + STR_UNIFIED_ExtF):
print("Exit: " + STR_UNIFIED_ExtF)
REG_STACK.pop()
elif(ln == "# End of " + STR_UNIFIED_ExtG):
print("Exit: " + STR_UNIFIED_ExtG)
REG_STACK.pop()
elif(ln == "# End of " + STR_ARRAY_SYMBOL):
print("Exit: " + STR_ARRAY_SYMBOL)
REG_STACK.pop()
elif(ln == "%chardef end"):
print("this is final")
if REG_STACK:
REG_STACK.pop()
print(REG_STACK)
else:
if(len(REG_STACK) == 0 or len(ln.strip()) == 0):
pass
else:
# Write data to SQL Database
r = (str(REG_STACK[-1]) + "\t" + ln).split()
print(r)
cur.execute('INSERT INTO main (keys, ch, cat, cnt) VALUES ("' + r[1] + '", "' + r[2] + '", "' + r[0] + '", "0")')
f.close()
con.commit()
con.close()
# empty tables
con = sqlite.connect("array.db")
cur = con.cursor()
cur.execute('DELETE FROM main;')
con.commit()
con.close()
array_updatedb(argv[1])
|