summaryrefslogtreecommitdiff
path: root/convert.py
blob: ab5b35ddd1f3a14e20313efcd19d7d0819b71bd5 (plain)
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
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
#!/usr/bin/python2
# -*- coding: utf-8 -*-

import MySQLdb as mysql
import sqlite3
import sys
from definition import Definition

def create():
	try:
		con = sqlite3.connect('dictionaries/wordnet.db');

		with con:
			cur = con.cursor()
			cur.execute("CREATE TABLE types (id integer primary key not null , type text, abbreviation text)")
			cur.execute("INSERT INTO types (type, abbreviation) VALUES('noun', 'n')")
			cur.execute("INSERT INTO types (type, abbreviation) VALUES('verb', 'v')")
			cur.execute("INSERT INTO types (type, abbreviation) VALUES('adjective', 'adj')")
			cur.execute("INSERT INTO types (type, abbreviation) VALUES('adverb', 'adv')")
			cur.execute("CREATE TABLE definitions (id integer primary key not null , word text, type_id int, sub_id int, synset_id bigint, definition text)")
			cur.execute("CREATE TABLE uses (id integer primary key not null, definition_id bigint, quote text)")
			cur.execute("CREATE TABLE info (id integer primary key not null , key text, value text)")
			cur.execute("INSERT INTO info (key, value) VALUES('version', '1')")

	except sqlite3.Error, e:
		print "Database Error %s" % (e.args[0])
		sys.exit(1)

def select_wn():
	try:
		con = mysql.connect('localhost', 'wordnet', 'words', 'wordnet');

		with con:
			cur = con.cursor(mysql.cursors.DictCursor)
			cur.execute("SELECT lemma, pos, sensenum, synsetid, definition, sampleset from dict")
			rows = cur.fetchall()
	except mysql.Error, e:
		print "Database Error %d: %s" % (e.args[0],e.args[1])
		sys.exit(1)
	
	return rows

def insert_wn(rows):
	try:
		con = sqlite3.connect('dictionaries/wordnet.db');

		with con:
			cur = con.cursor()

			for row in rows:
				if row['pos'] == 'n':
					type_id = 1
				elif row['pos'] == 'v':
					type_id = 2
				elif row['pos'] == 'a' or row['pos'] == 's':
					type_id = 3
				elif row['pos'] == 'r':
					type_id = 4

				cur.execute("INSERT INTO definitions(word, type_id, sub_id, synset_id, definition) values(?, ?, ?, ?, ?)", [row['lemma'], type_id, row['sensenum'], row['synsetid'], row['definition']])

				row_id = cur.lastrowid

				if row['sampleset'] is not None:
					uses = row['sampleset'].split("|")

					for use in uses:
						cur.execute("INSERT INTO uses(definition_id, quote) values(?, ?)", [row_id, use])

	except sqlite3.Error, e:
		print "Database Error %s" % (e.args[0])
		sys.exit(1)

def get_db_version():
	try:
		con = sqlite3.connect('dictionaries/wordnet.db');

		with con:
			cur = con.cursor()

			# Check if info table exists at all, if not then version 0
			cur.execute("SELECT count(name) FROM sqlite_master WHERE type='table' AND name='info'")
			row = cur.fetchone()
			if row[0] == 0:
				return 0
			else:
				cur.execute("SELECT value FROM info WHERE key = 'version'")
				row = cur.fetchone()
				return row[0]

	except sqlite3.Error, e:
		print "Database Error %s" % (e.args[0])
		sys.exit(1)

def update_db(version):
		version = int(version)
		try:
			con = sqlite3.connect('dictionaries/wordnet.db');
			with con:
				cur = con.cursor()
				if version < 1:
					cur.execute("CREATE TABLE info (id integer primary key not null , key text, value text)")
					cur.execute("INSERT INTO info (key, value) VALUES('version', '1')")

				if version < 2:
					cur = con.cursor()
					cur.execute("INSERT INTO types (type, abbreviation) VALUES('technical', 'tech.')")
					cur.execute("ALTER TABLE definitions ADD COLUMN dictionary_id int")
					cur.execute("CREATE TABLE categories (id integer primary key not null, category text)")
					cur.execute("CREATE TABLE dictionaries (id integer primary key not null, name text, abbreviation text)")
					cur.execute("INSERT INTO dictionaries (name, abbreviation) VALUES('WordNet', 'wn')")
					cur.execute("INSERT INTO dictionaries (name, abbreviation) VALUES('FOLDOC', 'foldoc')")
					cur.execute("CREATE TABLE definition_categories (id integer primary key not null, definition_id int, category_id int)")
					cur.execute("UPDATE info set value = 2 where key = 'version'")

		except sqlite3.Error, e:
			print "Database Error %s" % (e.args[0])
			sys.exit(1)

def parse_foldoc():

	file = open("dictionaries/foldoc.txt")
	word_line = ""
	see_also = False
	items = []
	id = 1
	word = ""
	categories = []

	definition = ""

	for line in file:
		#Find lines of word headings, these are ones without an indent
		remaining = ""
		if line[0] != "\t" and len(line) > 0 and line[0] != "\n" and line[0] != " ":

			if definition is not None and len(definition) > 1:
				item = Definition(word, id, "foldoc", "tech", definition.strip("\n "), [], [], [], categories, see_also)
				items.append(item)

			# Start the new definition
			word = line.strip()
			id = 1
			definition = ""
			categories = []
			see_also = False
			continue
		elif len(line) == 0 or line[0] == "\n":
			definition += "\n"
			continue
		else:
			line = line.strip()

			if definition != "" and (len(line) == 0 or line[0] == "\n"):
				definition += ""
			elif line[0].isdigit():
				id_parts = line.split(".")
				if len(id_parts[0]) <=2:

					if definition is not None and len(definition) > 1:
						item = Definition(word, id, "foldoc", "tech", definition.strip("\n "), [], [], [], categories, see_also)
						items.append(item)

						# Start the new definition
						id = id_parts[0]
						definition = ""
						see_also = False
						categories = []

					remaining = ""
					
					for part in id_parts[1:-1]:
						remaining += part + ". "
					remaining += id_parts[-1]

					remaining = remaining.strip()

					#Get the categories, at the start of the line enclosed in <>s
					if len(remaining) > 0 and remaining[0] == "<":
						categories = []
						category_parts = remaining.split(">")
						def_categories = category_parts[0].strip().split(",")

						for category in def_categories:
							categories.append(category.strip("<> "))

						remaining = ""
						for part in category_parts[1:]:
							remaining += part

					remaining = remaining.strip(". ")

					# Check if it's a 'see also' definition
					if len(remaining) > 0 and remaining[0] == "{" and remaining[-1] == "}":
						count_braces = 0
						# Avoid false positives that happen to start with a { and end with a }, but have others in between
						for char in remaining:
							if char == "}":
								count_braces += 1
						if count_braces == 1:
							see_also = True

				elif line[0] != "[" and line[-1] != "]" and not (line[-1] == "." and line[-2] == "]"):
					remaining = line
				
				definition += remaining.strip("<> ").replace("{", "").replace("}", "")

			# Check if it's a 'see also' definition
			elif line[0] == "{" and line[-1] == "}":
				count_braces = 0

				# Avoid false positives that happen to start with a { and end with a }, but have others in between
				for char in line:
					if char == "}":
						count_braces += 1
				if count_braces == 1:
					see_also = True
					definition = definition = line.strip("{} ")
				else:
					definition += line.replace("{", "").replace("}", "") 
			#Ignore the note lines enclosed in []s

			elif len(line) > 1 and line[0] != "[" and line[-1] != "]" and not (line[-1] == "." and line[-2] == "]"):
				# Get the categories
				if len(line) > 0 and line[0] == "<":
					categories = []
					category_parts = line.strip().split(">")
					def_categories = category_parts[0].strip().split(",")
					for category in def_categories:
						if "@" not in category:
							categories.append(category.strip("<> "))

					for part in category_parts[1:]:
						definition += part.replace("{", "").replace("}", "")
				elif len(line) > 0:
					#Ignore date lines
					#TODO: Make this a date regex
					if  len(line) > 1 and line[0] != "(" and line[-1] != ")":
						definition += line.replace("{", "").replace("}", "")
		if len(definition) > 0 and definition[-1] != " ":
			definition += " "

	#Add the last item
	item = Definition(word, id, "foldoc", "tech", definition.strip("\n "), [], [], [], categories)
	items.append(item)
	
	try:
		con = sqlite3.connect('dictionaries/wordnet.db');
		con.text_factory = str
		with con:
			cur = con.cursor()
			for item in items:
				cur.execute("INSERT INTO definitions(word, dictionary_id, type_id, sub_id, definition) values(?, ?, ?, ?, ?)", [item.word, 2, 5, item.id, item.definition])
				def_id = cur.lastrowid

				for category in item.categories:
					cur.execute("SELECT * FROM categories WHERE category = ?", [category])
					db_category = cur.fetchone()

					# Only make a new category if it doesn't exist, otherwise use the existing ID
					if db_category is not None:
						cat_id = db_category[0]
					else:
						cur.execute("INSERT INTO categories(category) values(?)", [category])
						cat_id = cur.lastrowid

					cur.execute("INSERT INTO definition_categories(category_id, definition_id) values(?, ?)", [cat_id, def_id])


	except sqlite3.Error, e:
		print "Database Error %s" % (e.args[0])
		sys.exit(1)


def main():

	version = get_db_version()

	# if version == 0:
	# 	create()
	# 	items = select_wn()
	# 	insert(items)

	update_db(version)
	parse_foldoc()

if __name__ == "__main__":
	main()