M1-Arborator-NaijaSUD/arborator/lib/treebankfiles.py
def glue(sentencedic,nodenum,direction):
		"""
		glues the nodenum (t and lemma) to the neighboring node in the direction
		ignores govs etc du nodenum
		gives back new sentencedic
		
		"""
		
		if debug>2:print "================== glueing"
		newsentencedic={}
		try:
			intchi=nodenum+direction
			if direction<0:
				sentencedic[intchi][tokenname]
				sentencedic[nodenum][tokenname]
				sentencedic[intchi][tokenname]=sentencedic[intchi][tokenname]+sentencedic[nodenum][tokenname]
				sentencedic[intchi]['lemma']=sentencedic[intchi]['lemma']+sentencedic[nodenum]['lemma']
			else:
				sentencedic[intchi][tokenname]=sentencedic[nodenum][tokenname]+sentencedic[intchi][tokenname]
				sentencedic[intchi]['lemma']=sentencedic[nodenum]['lemma']+sentencedic[intchi]['lemma']
				
			
			
			l = [0]+sorted(sentencedic)[:nodenum-1]+sorted(sentencedic)[nodenum:]
			if debug>1:print "lllll",l
			
			
			
			# correct wrong gov links
			for i in sorted(sentencedic):
				if nodenum in sentencedic[i]['gov'].keys():
					sentencedic[i]['gov'][intchi]=sentencedic[i]['gov'][nodenum]
					del sentencedic[i]['gov'][nodenum]		
			
			
			#print 'lllll',l
			# bring all in a new list with new indeces based on l
			
			for newi,oldi in enumerate(l):
				if newi:# not 0
					newsentencedic[newi]=sentencedic[oldi]
					govdic={}
					for govi,dep in sentencedic[oldi]['gov'].iteritems():
						
						govdic[l.index(govi)]=dep
					newsentencedic[newi]['gov']=govdic
		
		except Exception, err:
			print traceback.format_exc()
			print nodenum
			print direction
			print "oldoldold((((((((((((("
			for i in sorted(sentencedic):
				print i,sentencedic[i]
			print ")))))))))))))))"
			print "newnewnew((((((((((((("
			for i in sorted(newsentencedic):
				print i,newsentencedic[i]
			print ")))))))))))))))"
			print traceback.format_exc()
			if logfile:
				f=codecs.open(logfile,"a","utf-8")
				f.write(traceback.format_exc()+str(nodenum)+"\n" +str(intchi)+"\n" +  "\n\n\n"+unicode(sentencedic)+"\n\n\n"+unicode(newsentencedic)+"\n\n\n")
				f.close()
			return sentencedic
			#raise ShitError
		
		#del sentencedic[nodenum]

		if debug>1:
			print "((((((((((((("
			for i in sorted(newsentencedic):
				print i,newsentencedic[i]
			print ")))))))))))))))"
			
		return newsentencedic

		glues the nodenum (t and lemma) to the neighboring node in the direction
		ignores govs etc du nodenum
		gives back new sentencedic
		
		
glues the nodenum (t and lemma) to the neighboring node in the direction
ignores govs etc du nodenum
gives back new sentencedic
glues the nodenum ( t and lemma ) to the neighboring node in the direction ignores govs etc du nodenum gives back new sentencedic
def glue(sentencedic,nodenum,direction):
		if debug>2:print "================== glueing"
		newsentencedic={}
		try:
			intchi=nodenum+direction
			if direction<0:
				sentencedic[intchi][tokenname]
				sentencedic[nodenum][tokenname]
				sentencedic[intchi][tokenname]=sentencedic[intchi][tokenname]+sentencedic[nodenum][tokenname]
				sentencedic[intchi]['lemma']=sentencedic[intchi]['lemma']+sentencedic[nodenum]['lemma']
			else:
				sentencedic[intchi][tokenname]=sentencedic[nodenum][tokenname]+sentencedic[intchi][tokenname]
				sentencedic[intchi]['lemma']=sentencedic[nodenum]['lemma']+sentencedic[intchi]['lemma']
			l = [0]+sorted(sentencedic)[:nodenum-1]+sorted(sentencedic)[nodenum:]
			if debug>1:print "lllll",l
			for i in sorted(sentencedic):
				if nodenum in sentencedic[i]['gov'].keys():
					sentencedic[i]['gov'][intchi]=sentencedic[i]['gov'][nodenum]
					del sentencedic[i]['gov'][nodenum]		
			for newi,oldi in enumerate(l):
				if newi:
					newsentencedic[newi]=sentencedic[oldi]
					govdic={}
					for govi,dep in sentencedic[oldi]['gov'].iteritems():
						govdic[l.index(govi)]=dep
					newsentencedic[newi]['gov']=govdic
		except Exception, err:
			print traceback.format_exc()
			print nodenum
			print direction
			print "oldoldold((((((((((((("
			for i in sorted(sentencedic):
				print i,sentencedic[i]
			print ")))))))))))))))"
			print "newnewnew((((((((((((("
			for i in sorted(newsentencedic):
				print i,newsentencedic[i]
			print ")))))))))))))))"
			print traceback.format_exc()
			if logfile:
				f=codecs.open(logfile,"a","utf-8")
				f.write(traceback.format_exc()+str(nodenum)+"\n" +str(intchi)+"\n" +  "\n\n\n"+unicode(sentencedic)+"\n\n\n"+unicode(newsentencedic)+"\n\n\n")
				f.close()
			return sentencedic
		if debug>1:
			print "((((((((((((("
			for i in sorted(newsentencedic):
				print i,newsentencedic[i]
			print ")))))))))))))))"
		return newsentencedic
def glue ( sentencedic , nodenum , direction ) : if debug > 2 : print "================== glueing" newsentencedic = { } try : intchi = nodenum + direction if direction < 0 : sentencedic [ intchi ] [ tokenname ] sentencedic [ nodenum ] [ tokenname ] sentencedic [ intchi ] [ tokenname ] = sentencedic [ intchi ] [ tokenname ] + sentencedic [ nodenum ] [ tokenname ] sentencedic [ intchi ] [ 'lemma' ] = sentencedic [ intchi ] [ 'lemma' ] + sentencedic [ nodenum ] [ 'lemma' ] else : sentencedic [ intchi ] [ tokenname ] = sentencedic [ nodenum ] [ tokenname ] + sentencedic [ intchi ] [ tokenname ] sentencedic [ intchi ] [ 'lemma' ] = sentencedic [ nodenum ] [ 'lemma' ] + sentencedic [ intchi ] [ 'lemma' ] l = [ 0 ] + sorted ( sentencedic ) [ : nodenum - 1 ] + sorted ( sentencedic ) [ nodenum : ] if debug > 1 : print "lllll" , l for i in sorted ( sentencedic ) : if nodenum in sentencedic [ i ] [ 'gov' ] . keys ( ) : sentencedic [ i ] [ 'gov' ] [ intchi ] = sentencedic [ i ] [ 'gov' ] [ nodenum ] del sentencedic [ i ] [ 'gov' ] [ nodenum ] for newi , oldi in enumerate ( l ) : if newi : newsentencedic [ newi ] = sentencedic [ oldi ] govdic = { } for govi , dep in sentencedic [ oldi ] [ 'gov' ] . iteritems ( ) : govdic [ l . index ( govi ) ] = dep newsentencedic [ newi ] [ 'gov' ] = govdic except Exception , err : print traceback . format_exc ( ) print nodenum print direction print "oldoldold(((((((((((((" for i in sorted ( sentencedic ) : print i , sentencedic [ i ] print ")))))))))))))))" print "newnewnew(((((((((((((" for i in sorted ( newsentencedic ) : print i , newsentencedic [ i ] print ")))))))))))))))" print traceback . format_exc ( ) if logfile : f = codecs . open ( logfile , "a" , "utf-8" ) f . write ( traceback . format_exc ( ) + str ( nodenum ) + "\n" + str ( intchi ) + "\n" + "\n\n\n" + unicode ( sentencedic ) + "\n\n\n" + unicode ( newsentencedic ) + "\n\n\n" ) f . close ( ) return sentencedic if debug > 1 : print "(((((((((((((" for i in sorted ( newsentencedic ) : print i , newsentencedic [ i ] print ")))))))))))))))" return newsentencedic
glues the nodenum (t and lemma) to the neighboring node in the direction
ignores govs etc du nodenum
gives back new sentencedic
glues the nodenum ( t and lemma ) to the neighboring node in the direction ignores govs etc du nodenum gives back new sentencedic
"""
		glues the nodenum (t and lemma) to the neighboring node in the direction
		ignores govs etc du nodenum
		gives back new sentencedic
		
		""" # correct wrong gov links #print 'lllll',l # bring all in a new list with new indeces based on l # not 0 #raise ShitError #del sentencedic[nodenum]
sentencedic
nodenum
direction