Web   ·   Wiki   ·   Activities   ·   Blog   ·   Lists   ·   Chat   ·   Meeting   ·   Bugs   ·   Git   ·   Translate   ·   Archive   ·   People   ·   Donate
summaryrefslogtreecommitdiffstats
path: root/dictionary.py
blob: b9627140c9ac163ef4b405aa4e863a9732f69962 (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
#!/bin/env python
import sys
# Provides the API to control the dictionary.

global __root__
global __element__
global __word__
global __def__
global __phnm__
global __pos__
global __src__
global __fld__
global __debug
global __numword
global __DBName
global __DBTableName

__root__ = "dictionary"
__element__ = "p"
__word__ = "hw"
__def__ = "def"
__phnm__ = "pr"
__pos__ = "pos"
__src__ = "source"
__fld__ = "fld"
__debug = True
__numword = -1
__DBName = "/tmp/las"
__DBTableName = "dictionary" #Currently not used

#strings which are tag in XML

class dictionary:
	
	def __init__(self, filename):
	
		from xml.etree.ElementTree  import ElementTree
		#create an ElementTree instance from an XML file
		self.ETree = ElementTree(file=filename)
		if self.ETree.getroot().tag != __root__:
			print "Invalid File"
			sys.exit(0)

	def getroottag(self):
		return self.ETree.getroot().tag
	
	def getnumwords(self):
		self.iter = self.ETree.getiterator(__element__)
		__numword = len(self.iter)
		return __numword	
	def loadict(self):
		self.MakeDB()
		for element in self.iter:
			if element.getchildren():
			#Can also use: "for child in element.getchildren():"
				tempDict = {}
				tempDict["word"] = ""
				tempDict["def"] = ""
				tempDict["phnm"] = ""
				tempDict["src"] = ""
				tempDict["pos"] = ""
				for child in element:
					#Child element tag name
					if child.tag == __word__:
						tempDict["word"] = child.text
					elif child.tag == __def__:
						tempDict["def"] = child.text
					elif child.tag == __phnm__:
						tempDict["phnm"] = child.text
					elif child.tag == __src__:
						tempDict["src"] = child.text
					elif child.tag == __pos__:
						tempDict["pos"] = child.text
				t = ( tempDict["word"],tempDict["def"],tempDict["phnm"],tempDict["src"],tempDict["pos"] )
				self.c.execute( "insert into dict values (?, ? , ?, ?, ?)", t )
		self.conn.commit()
		self.c.close()
		self.conn.close()
	
	def MakeDB(self):
		import sqlite3
		import random
		self.conn = sqlite3.connect("las.db")
		self.c = self.conn.cursor()
		self.c.execute('''DROP TABLE IF EXISTS dict''')
		self.c.execute('''CREATE table IF NOT EXISTS dict (word text, def text, phnm text, src text, pos text)''')

if __name__ == "__main__":
	k = dictionary("dict/k.xml")
	print k.getroottag()
	print k.getnumwords()
	k.loadict()