[ create a new paste ] login | about

Link: http://codepad.org/qtG5leGy    [ raw code | output | fork ]

Python, pasted on Dec 20:
# -*- 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()


Output:
1
2
3
4
5
t.py:22: Warning: 'with' will become a reserved keyword in Python 2.6
  Line 22
    with open(file_name, 'r') as content_file:
            ^
SyntaxError: invalid syntax


Create a new paste based on this one


Comments: