M1-Arborator-NaijaSUD/arborator/lib/treebankfiles.py
def integrate(sentencedic,nodenum,intdic):
		"""
		integrates the intdic into sentencedic at position nodenum
		an empty intdic kicks out the node in a proper manner (other dependencies are still ok)
		
		gives back new sentencedic
		
		"""
		intgov,intchi=None,None
		newsentencedic=conll.Tree()
		if debug>2: 
			print "================== integrating"
			for i in sorted(intdic):
				print i,intdic[i]
			print "into ((((((((((((("
			for i in sorted(sentencedic):
				print i,sentencedic[i]
			print ")))))))))))))))"
		try:
			l = [0]+sorted(sentencedic)[:nodenum-1]+sorted(intdic)+sorted(sentencedic)[nodenum:]
			if debug>2: print "lllll",l
			
			
			for i in sorted(intdic):
			
				sentencedic[i]=sentencedic[nodenum].copy()
				#intdic[i][tokenname]
				sentencedic[i][tokenname]=intdic[i][tokenname]
				sentencedic[i]['lemma']=intdic[i]['lemma']
				#sentencedic[i]['cat']=intdic[i]['cat']
				sentencedic[i]['tag']=intdic[i]['tag']
				sentencedic[i]['gov']=intdic[i].get('gov',{})
				
				
				if 'i0' in intdic[i].get('gov',{}).keys(): # found the gov: the one that has a root link
					sentencedic[i]['gov']=sentencedic[nodenum]['gov']
					intgov=i
				if "child" in intdic[i]: # found the child bearer
					intchi = i
			if not intgov:# if no governor is indicated, i guess it's the first word
				intgov="i1"
				if intgov in sentencedic: sentencedic[intgov]['gov']=sentencedic[nodenum]['gov']
			if not intchi: intchi=intgov # by default same main token carries all
			
			# 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
			newsentencedic=conll.Tree()
			for newi,oldi in enumerate(l):
				if newi:# not 0
					newsentencedic[newi]=sentencedic[oldi]
					govdic={}
					for govi,dep in sentencedic[oldi]['gov'].iteritems():
						if govi>=0 and govi in l:govdic[l.index(govi)]=dep
					newsentencedic[newi]['gov']=govdic
		except Exception, err:
			
			print ":::::::::::::::Error: function integrate was called with these parameters:"
			print sentencedic.dictrepr()
			print nodenum
			print intdic.dictrepr()
			print traceback.format_exc()+"\n\n\n"
			
			#print traceback.format_exc()
			#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 "intdic((((((((((((("
			#for i in sorted(intdic):
				#print i,intdic[i]
			#print ")))))))))))))))"
			#print nodenum
			#print intdic
			
			if logfile:
				f=codecs.open(logfile,"a","utf-8")
				f.write(traceback.format_exc()+"\n\n\n"+unicode(sentencedic)+"\n\n\n"+unicode(newsentencedic)+"\n\n\n")
				f.close()

			raise ShitError
		#del sentencedic[nodenum]

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

		integrates the intdic into sentencedic at position nodenum
		an empty intdic kicks out the node in a proper manner (other dependencies are still ok)
		
		gives back new sentencedic
		
		
integrates the intdic into sentencedic at position nodenum
an empty intdic kicks out the node in a proper manner (other dependencies are still ok)

gives back new sentencedic
integrates the intdic into sentencedic at position nodenum an empty intdic kicks out the node in a proper manner ( other dependencies are still ok ) gives back new sentencedic
def integrate(sentencedic,nodenum,intdic):
		intgov,intchi=None,None
		newsentencedic=conll.Tree()
		if debug>2: 
			print "================== integrating"
			for i in sorted(intdic):
				print i,intdic[i]
			print "into ((((((((((((("
			for i in sorted(sentencedic):
				print i,sentencedic[i]
			print ")))))))))))))))"
		try:
			l = [0]+sorted(sentencedic)[:nodenum-1]+sorted(intdic)+sorted(sentencedic)[nodenum:]
			if debug>2: print "lllll",l
			for i in sorted(intdic):
				sentencedic[i]=sentencedic[nodenum].copy()
				sentencedic[i][tokenname]=intdic[i][tokenname]
				sentencedic[i]['lemma']=intdic[i]['lemma']
				sentencedic[i]['tag']=intdic[i]['tag']
				sentencedic[i]['gov']=intdic[i].get('gov',{})
				if 'i0' in intdic[i].get('gov',{}).keys(): 
					sentencedic[i]['gov']=sentencedic[nodenum]['gov']
					intgov=i
				if "child" in intdic[i]: 
					intchi = i
			if not intgov:
				intgov="i1"
				if intgov in sentencedic: sentencedic[intgov]['gov']=sentencedic[nodenum]['gov']
			if not intchi: intchi=intgov 
			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]		
			newsentencedic=conll.Tree()
			for newi,oldi in enumerate(l):
				if newi:
					newsentencedic[newi]=sentencedic[oldi]
					govdic={}
					for govi,dep in sentencedic[oldi]['gov'].iteritems():
						if govi>=0 and govi in l:govdic[l.index(govi)]=dep
					newsentencedic[newi]['gov']=govdic
		except Exception, err:
			print ":::::::::::::::Error: function integrate was called with these parameters:"
			print sentencedic.dictrepr()
			print nodenum
			print intdic.dictrepr()
			print traceback.format_exc()+"\n\n\n"
			if logfile:
				f=codecs.open(logfile,"a","utf-8")
				f.write(traceback.format_exc()+"\n\n\n"+unicode(sentencedic)+"\n\n\n"+unicode(newsentencedic)+"\n\n\n")
				f.close()
			raise ShitError
		if debug>2:
			print "result:((((((((((((("
			for i in sorted(newsentencedic):
				print i,newsentencedic[i]
			print ")))))))))))))))"
		return newsentencedic
def integrate ( sentencedic , nodenum , intdic ) : intgov , intchi = None , None newsentencedic = conll . Tree ( ) if debug > 2 : print "================== integrating" for i in sorted ( intdic ) : print i , intdic [ i ] print "into (((((((((((((" for i in sorted ( sentencedic ) : print i , sentencedic [ i ] print ")))))))))))))))" try : l = [ 0 ] + sorted ( sentencedic ) [ : nodenum - 1 ] + sorted ( intdic ) + sorted ( sentencedic ) [ nodenum : ] if debug > 2 : print "lllll" , l for i in sorted ( intdic ) : sentencedic [ i ] = sentencedic [ nodenum ] . copy ( ) sentencedic [ i ] [ tokenname ] = intdic [ i ] [ tokenname ] sentencedic [ i ] [ 'lemma' ] = intdic [ i ] [ 'lemma' ] sentencedic [ i ] [ 'tag' ] = intdic [ i ] [ 'tag' ] sentencedic [ i ] [ 'gov' ] = intdic [ i ] . get ( 'gov' , { } ) if 'i0' in intdic [ i ] . get ( 'gov' , { } ) . keys ( ) : sentencedic [ i ] [ 'gov' ] = sentencedic [ nodenum ] [ 'gov' ] intgov = i if "child" in intdic [ i ] : intchi = i if not intgov : intgov = "i1" if intgov in sentencedic : sentencedic [ intgov ] [ 'gov' ] = sentencedic [ nodenum ] [ 'gov' ] if not intchi : intchi = intgov 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 ] newsentencedic = conll . Tree ( ) for newi , oldi in enumerate ( l ) : if newi : newsentencedic [ newi ] = sentencedic [ oldi ] govdic = { } for govi , dep in sentencedic [ oldi ] [ 'gov' ] . iteritems ( ) : if govi >= 0 and govi in l : govdic [ l . index ( govi ) ] = dep newsentencedic [ newi ] [ 'gov' ] = govdic except Exception , err : print ":::::::::::::::Error: function integrate was called with these parameters:" print sentencedic . dictrepr ( ) print nodenum print intdic . dictrepr ( ) print traceback . format_exc ( ) + "\n\n\n" if logfile : f = codecs . open ( logfile , "a" , "utf-8" ) f . write ( traceback . format_exc ( ) + "\n\n\n" + unicode ( sentencedic ) + "\n\n\n" + unicode ( newsentencedic ) + "\n\n\n" ) f . close ( ) raise ShitError if debug > 2 : print "result:(((((((((((((" for i in sorted ( newsentencedic ) : print i , newsentencedic [ i ] print ")))))))))))))))" return newsentencedic
integrates the intdic into sentencedic at position nodenum
an empty intdic kicks out the node in a proper manner (other dependencies are still ok)
integrates the intdic into sentencedic at position nodenum an empty intdic kicks out the node in a proper manner ( other dependencies are still ok )
"""
		integrates the intdic into sentencedic at position nodenum
		an empty intdic kicks out the node in a proper manner (other dependencies are still ok)
		
		gives back new sentencedic
		
		""" #intdic[i][tokenname] #sentencedic[i]['cat']=intdic[i]['cat'] # found the gov: the one that has a root link # found the child bearer # if no governor is indicated, i guess it's the first word # by default same main token carries all # correct wrong gov links #print 'lllll',l # bring all in a new list with new indeces based on l # not 0 #print traceback.format_exc() #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 "intdic(((((((((((((" #for i in sorted(intdic): #print i,intdic[i] #print ")))))))))))))))" #print nodenum #print intdic #del sentencedic[nodenum]
sentencedic
nodenum
intdic