M1-Arborator-NaijaSUD/arborator/lib/database.py
def enterTree(self, cursor, nodedic, sentenceid, userid, notenter=[], intokenname=None, tokensChanged=False):
		"""
		takes the 
			db cursor, 
			nodedic, 
			sentenceid, 
			userid, 
			t (the name of the feature that serves as the token = invariable by the annotators)
		drops tree into database
		returns word counter

		called in two cases:
		- a new tree is entered whose tokens are not in the base yet
		- a tree is saved: a treeid already exists, all the features are erased first
		"""
		#debug start:
		#import sys
		#currentFuncName = lambda n=0: sys._getframe(n + 1).f_code.co_name
		#open("logger.txt","a").write("enterTree "+str( [cursor, nodedic, sentenceid, userid, notenter, intokenname, tokensChanged,"daddy:",currentFuncName(1)])+"\n")
		#print nodedic, sentenceid, userid, intokenname
		#debug end
		
		if not intokenname:  intokenname=self.tokenName

		wcounter=0
		timestamp = mktime( datetime.now().timetuple() ) # contrary: asctime(localtime(timestamp))


		treeid=self.getUniqueId(cursor, "trees", ["sentenceid","userid","annotype"],[sentenceid, userid, 0])
		if treeid:
			#open("logger.txt","a").write("enterTree2 treeid existed\n")
			self.clean(cursor, "features",["treeid"],(treeid,))
			self.clean(cursor, "links",["treeid"],(treeid,))
			cursor.execute("update trees set timestamp=? where sentenceid=? and userid=? and annotype=?;",(timestamp, sentenceid, userid, 0,))

		else:
			#open("logger.txt","a").write("enterTree2 treeid didn't exist\n")
			treeid=self.enter(cursor, "trees",["sentenceid","userid","annotype","status","timestamp"],(sentenceid,userid,0,"0",timestamp),True) # createIfNotExists
			self.clean(cursor, "features",["treeid"],(treeid,)) # erase all existing features for this treeid, should be a useless line. just in case...
			self.clean(cursor, "links",["treeid"],(treeid,))


		sent=""

		for i in sorted(nodedic.keys()): # for every token

			tok=nodedic[i].get(intokenname,"")
			sent+=tok+" "
			if tokensChanged:
				cursor.execute("update features set value=? where features.attr=? and features.nr=? and features.treeid=(select trees.rowid from trees where trees.sentenceid =?)",(tok, self.tokenName, i, sentenceid,))

			wcounter+=1
			# enter attributes for token
			for attr in nodedic[i]: # i is now used as the nr value in the database's feature table
				if attr not in notenter:
					val=nodedic[i][attr]
					if type(val).__name__=='list':
						# not nice: values tabseparated. but not used for the moment
						#for v in val: # doesn't work because database doesn't allow multiple features of same attr
						self.enter(cursor, "features",["treeid","nr","attr","value"],(treeid,i,attr,"\t".join(val),), computeId=False)
					elif type(val).__name__=='dict': # should only happen for attr="gov"
						for gov in val:
							self.enter(cursor, "links",["treeid","depid","govid","function"],(treeid,i,gov,val[gov],), computeId=False)
					else: # atomic value
						#if attr!=t:
						if attr==intokenname: attr=self.tokenName # change the token name in the nodedic to the project's token name
						self.enter(cursor, "features",["treeid","nr","attr","value"],(treeid,i,attr,val,), computeId=False)
		if tokensChanged:
			sent=" ".join([a for a, in cursor.execute("select value from features where attr='t' and treeid in (select trees.rowid from trees, users where trees.sentenceid =? and users.user=? and users.rowid=trees.userid) ORDER BY nr ;",(sentenceid,self.importAnnotatorName,)).fetchall()])

			cursor.execute("UPDATE sentences SET sentence=? WHERE rowid=? ",(sent,sentenceid,))
			cursor.execute("UPDATE sentencesearch SET sentence=? WHERE rowid=? ",(sent,sentenceid,))
		return wcounter, sent.strip(), treeid

		takes the 
			db cursor, 
			nodedic, 
			sentenceid, 
			userid, 
			t (the name of the feature that serves as the token = invariable by the annotators)
		drops tree into database
		returns word counter

		called in two cases:
		- a new tree is entered whose tokens are not in the base yet
		- a tree is saved: a treeid already exists, all the features are erased first
		
takes the
db cursor,
nodedic,
sentenceid,
userid,
t (the name of the feature that serves as the token = invariable by the annotators)
drops tree into database
returns word counter

called in two cases:
a new tree is entered whose tokens are not in the base yet
a tree is saved: a treeid already exists, all the features are erased first
takes the db cursor nodedic sentenceid userid t ( the name of the feature that serves as the token = invariable by the annotators ) drops tree into database returns word counter called in two cases : a new tree is entered whose tokens are not in the base yet a tree is saved : a treeid already exists all the features are erased first
def enterTree(self, cursor, nodedic, sentenceid, userid, notenter=[], intokenname=None, tokensChanged=False):
		if not intokenname:  intokenname=self.tokenName
		wcounter=0
		timestamp = mktime( datetime.now().timetuple() ) 
		treeid=self.getUniqueId(cursor, "trees", ["sentenceid","userid","annotype"],[sentenceid, userid, 0])
		if treeid:
			self.clean(cursor, "features",["treeid"],(treeid,))
			self.clean(cursor, "links",["treeid"],(treeid,))
			cursor.execute("update trees set timestamp=? where sentenceid=? and userid=? and annotype=?;",(timestamp, sentenceid, userid, 0,))
		else:
			treeid=self.enter(cursor, "trees",["sentenceid","userid","annotype","status","timestamp"],(sentenceid,userid,0,"0",timestamp),True) 
			self.clean(cursor, "features",["treeid"],(treeid,)) 
			self.clean(cursor, "links",["treeid"],(treeid,))
		sent=""
		for i in sorted(nodedic.keys()): 
			tok=nodedic[i].get(intokenname,"")
			sent+=tok+" "
			if tokensChanged:
				cursor.execute("update features set value=? where features.attr=? and features.nr=? and features.treeid=(select trees.rowid from trees where trees.sentenceid =?)",(tok, self.tokenName, i, sentenceid,))
			wcounter+=1
			for attr in nodedic[i]: 
				if attr not in notenter:
					val=nodedic[i][attr]
					if type(val).__name__=='list':
						self.enter(cursor, "features",["treeid","nr","attr","value"],(treeid,i,attr,"\t".join(val),), computeId=False)
					elif type(val).__name__=='dict': 
						for gov in val:
							self.enter(cursor, "links",["treeid","depid","govid","function"],(treeid,i,gov,val[gov],), computeId=False)
					else: 
						if attr==intokenname: attr=self.tokenName 
						self.enter(cursor, "features",["treeid","nr","attr","value"],(treeid,i,attr,val,), computeId=False)
		if tokensChanged:
			sent=" ".join([a for a, in cursor.execute("select value from features where attr='t' and treeid in (select trees.rowid from trees, users where trees.sentenceid =? and users.user=? and users.rowid=trees.userid) ORDER BY nr ;",(sentenceid,self.importAnnotatorName,)).fetchall()])
			cursor.execute("UPDATE sentences SET sentence=? WHERE rowid=? ",(sent,sentenceid,))
			cursor.execute("UPDATE sentencesearch SET sentence=? WHERE rowid=? ",(sent,sentenceid,))
		return wcounter, sent.strip(), treeid
def enterTree ( self , cursor , nodedic , sentenceid , userid , notenter = [ ] , intokenname = None , tokensChanged = False ) : if not intokenname : intokenname = self . tokenName wcounter = 0 timestamp = mktime ( datetime . now ( ) . timetuple ( ) ) treeid = self . getUniqueId ( cursor , "trees" , [ "sentenceid" , "userid" , "annotype" ] , [ sentenceid , userid , 0 ] ) if treeid : self . clean ( cursor , "features" , [ "treeid" ] , ( treeid , ) ) self . clean ( cursor , "links" , [ "treeid" ] , ( treeid , ) ) cursor . execute ( "update trees set timestamp=? where sentenceid=? and userid=? and annotype=?;" , ( timestamp , sentenceid , userid , 0 , ) ) else : treeid = self . enter ( cursor , "trees" , [ "sentenceid" , "userid" , "annotype" , "status" , "timestamp" ] , ( sentenceid , userid , 0 , "0" , timestamp ) , True ) self . clean ( cursor , "features" , [ "treeid" ] , ( treeid , ) ) self . clean ( cursor , "links" , [ "treeid" ] , ( treeid , ) ) sent = "" for i in sorted ( nodedic . keys ( ) ) : tok = nodedic [ i ] . get ( intokenname , "" ) sent += tok + " " if tokensChanged : cursor . execute ( "update features set value=? where features.attr=? and features.nr=? and features.treeid=(select trees.rowid from trees where trees.sentenceid =?)" , ( tok , self . tokenName , i , sentenceid , ) ) wcounter += 1 for attr in nodedic [ i ] : if attr not in notenter : val = nodedic [ i ] [ attr ] if type ( val ) . __name__ == 'list' : self . enter ( cursor , "features" , [ "treeid" , "nr" , "attr" , "value" ] , ( treeid , i , attr , "\t" . join ( val ) , ) , computeId = False ) elif type ( val ) . __name__ == 'dict' : for gov in val : self . enter ( cursor , "links" , [ "treeid" , "depid" , "govid" , "function" ] , ( treeid , i , gov , val [ gov ] , ) , computeId = False ) else : if attr == intokenname : attr = self . tokenName self . enter ( cursor , "features" , [ "treeid" , "nr" , "attr" , "value" ] , ( treeid , i , attr , val , ) , computeId = False ) if tokensChanged : sent = " " . join ( [ a for a , in cursor . execute ( "select value from features where attr='t' and treeid in (select trees.rowid from trees, users where trees.sentenceid =? and users.user=? and users.rowid=trees.userid) ORDER BY nr ;" , ( sentenceid , self . importAnnotatorName , ) ) . fetchall ( ) ] ) cursor . execute ( "UPDATE sentences SET sentence=? WHERE rowid=? " , ( sent , sentenceid , ) ) cursor . execute ( "UPDATE sentencesearch SET sentence=? WHERE rowid=? " , ( sent , sentenceid , ) ) return wcounter , sent . strip ( ) , treeid
takes the
db cursor,
nodedic,
sentenceid,
userid,
t (the name of the feature that serves as the token = invariable by the annotators)
drops tree into database
returns word counter
takes the db cursor nodedic sentenceid userid t ( the name of the feature that serves as the token = invariable by the annotators ) drops tree into database returns word counter
"""
		takes the 
			db cursor, 
			nodedic, 
			sentenceid, 
			userid, 
			t (the name of the feature that serves as the token = invariable by the annotators)
		drops tree into database
		returns word counter

		called in two cases:
		- a new tree is entered whose tokens are not in the base yet
		- a tree is saved: a treeid already exists, all the features are erased first
		""" #debug start: #import sys #currentFuncName = lambda n=0: sys._getframe(n + 1).f_code.co_name #open("logger.txt","a").write("enterTree "+str( [cursor, nodedic, sentenceid, userid, notenter, intokenname, tokensChanged,"daddy:",currentFuncName(1)])+"\n") #print nodedic, sentenceid, userid, intokenname #debug end # contrary: asctime(localtime(timestamp)) #open("logger.txt","a").write("enterTree2 treeid existed\n") #open("logger.txt","a").write("enterTree2 treeid didn't exist\n") # createIfNotExists # erase all existing features for this treeid, should be a useless line. just in case... # for every token # enter attributes for token # i is now used as the nr value in the database's feature table # not nice: values tabseparated. but not used for the moment #for v in val: # doesn't work because database doesn't allow multiple features of same attr # should only happen for attr="gov" # atomic value #if attr!=t: # change the token name in the nodedic to the project's token name
self
cursor
nodedic
sentenceid
userid
notenter
intokenname
tokensChanged