# -*- coding: utf-8 -*-
import re
import sqlite3
text_file = "warodai.txt"
database_file = "edict2.sql"
def main():
content = read_file_content(text_file)
query = make_query_from_text(content)
write_to_file(query)
print "Done!"
def read_file_content(file_name):
print "Reading " + file_name + "..."
content = ''
with open(file_name, 'r') as content_file:
content = content_file.read()
return content
def write_to_file(query):
print "Write to file"
fl = open("patch.sql", "w")
fl.write(query)
fl.close()
def write_to_database(query):
print "Write to database"
database = sqlite3.connect(database_file)
cursor = database.cursor()
cursor.executescript(query)
database.close();
def make_query_from_text(string):
print "Search matches..."
pattern = re.compile(r'([^【】\n]+)(?:【([^【】]+)】)?\s?\(.+\)\s?〔[0-9]+\;[0-9]+;[0-9]+〕\n(.+)\n')
query = 'BEGIN;'
for (word, transcription, translate) in re.findall(pattern, string):
query += '\nUPDATE dictionary SET defn="' + translate + '" WHERE '
if transcription != "":
query += 'word="' + transcription + '" OR '
query += 'kana="' + word + '";'
query += '\nCOMMIT'
return query
main()