M1-Arborator-NaijaSUD/arborator/lib/trees2train.py
def exportConllByAnnotators(project, annotators=["prof","Sy","parser"], fileExtension=".conllu"):
	"""
	exports complete project
	for every sentence, trees of annotators in given order.
	if no tree: throw error 
	
	"""
	outfiles=[]
	sql = SQL(project)
	db,cursor=sql.open()
	goodTexts={}
	outdir=os.path.join("..","projects",project,"export")
	try: os.mkdir(outdir)	
	except OSError: pass
	try:	annotatorIds=tuple(a for (a,) in [list(cursor.execute("select rowid from users where user =?;",(annotator,) ))[0] for annotator in annotators])
	except:
		print "some required annotator IDs are not in the database"
		return
	print annotators, annotatorIds
	
	for textid, textname, nrtokens in list(cursor.execute("select rowid, * from texts;" )):  # for each text
		print "doing",textname,"with", nrtokens, "tokens"
		nrutids={}
		for nr,userid,treeid in list(cursor.execute("select nr,userid,trees.rowid as treeid from trees, sentences where sentenceid=sentences.rowid and userid in {annotatorIds} and  textid = ? order by nr;".format(annotatorIds=annotatorIds if len(annotatorIds)>1 else '('+str(annotatorIds[0])+')'),(textid,))):
			nrutids[nr]=nrutids.get(nr,{})
			nrutids[nr][userid]=treeid
		trees=getSpecificTrees(sql, db, cursor, nrutids, annotatorIds)
		if trees:
			if textname.endswith(".conll"): textname=textname[:-len(".conll")]
			if textname.endswith(".conllu"): textname=textname[:-len(".conllu")]
			outfile=os.path.join(outdir,textname+fileExtension)
			conll.trees2conllFile(trees, outfile=outfile, columns=10)
			print len(trees),"trees"
			outfiles+=[outfile]
		else:
			print "skipped",textname
	return outfiles

	exports complete project
	for every sentence, trees of annotators in given order.
	if no tree: throw error 
	
	
exports complete project
for every sentence, trees of annotators in given order.
if no tree: throw error
exports complete project for every sentence trees of annotators in given order . if no tree : throw error
def exportConllByAnnotators(project, annotators=["prof","Sy","parser"], fileExtension=".conllu"):
	outfiles=[]
	sql = SQL(project)
	db,cursor=sql.open()
	goodTexts={}
	outdir=os.path.join("..","projects",project,"export")
	try: os.mkdir(outdir)	
	except OSError: pass
	try:	annotatorIds=tuple(a for (a,) in [list(cursor.execute("select rowid from users where user =?;",(annotator,) ))[0] for annotator in annotators])
	except:
		print "some required annotator IDs are not in the database"
		return
	print annotators, annotatorIds
	for textid, textname, nrtokens in list(cursor.execute("select rowid, * from texts;" )):  
		print "doing",textname,"with", nrtokens, "tokens"
		nrutids={}
		for nr,userid,treeid in list(cursor.execute("select nr,userid,trees.rowid as treeid from trees, sentences where sentenceid=sentences.rowid and userid in {annotatorIds} and  textid = ? order by nr;".format(annotatorIds=annotatorIds if len(annotatorIds)>1 else '('+str(annotatorIds[0])+')'),(textid,))):
			nrutids[nr]=nrutids.get(nr,{})
			nrutids[nr][userid]=treeid
		trees=getSpecificTrees(sql, db, cursor, nrutids, annotatorIds)
		if trees:
			if textname.endswith(".conll"): textname=textname[:-len(".conll")]
			if textname.endswith(".conllu"): textname=textname[:-len(".conllu")]
			outfile=os.path.join(outdir,textname+fileExtension)
			conll.trees2conllFile(trees, outfile=outfile, columns=10)
			print len(trees),"trees"
			outfiles+=[outfile]
		else:
			print "skipped",textname
	return outfiles
def exportConllByAnnotators ( project , annotators = [ "prof" , "Sy" , "parser" ] , fileExtension = ".conllu" ) : outfiles = [ ] sql = SQL ( project ) db , cursor = sql . open ( ) goodTexts = { } outdir = os . path . join ( ".." , "projects" , project , "export" ) try : os . mkdir ( outdir ) except OSError : pass try : annotatorIds = tuple ( a for ( a , ) in [ list ( cursor . execute ( "select rowid from users where user =?;" , ( annotator , ) ) ) [ 0 ] for annotator in annotators ] ) except : print "some required annotator IDs are not in the database" return print annotators , annotatorIds for textid , textname , nrtokens in list ( cursor . execute ( "select rowid, * from texts;" ) ) : print "doing" , textname , "with" , nrtokens , "tokens" nrutids = { } for nr , userid , treeid in list ( cursor . execute ( "select nr,userid,trees.rowid as treeid from trees, sentences where sentenceid=sentences.rowid and userid in {annotatorIds} and  textid = ? order by nr;" . format ( annotatorIds = annotatorIds if len ( annotatorIds ) > 1 else '(' + str ( annotatorIds [ 0 ] ) + ')' ) , ( textid , ) ) ) : nrutids [ nr ] = nrutids . get ( nr , { } ) nrutids [ nr ] [ userid ] = treeid trees = getSpecificTrees ( sql , db , cursor , nrutids , annotatorIds ) if trees : if textname . endswith ( ".conll" ) : textname = textname [ : - len ( ".conll" ) ] if textname . endswith ( ".conllu" ) : textname = textname [ : - len ( ".conllu" ) ] outfile = os . path . join ( outdir , textname + fileExtension ) conll . trees2conllFile ( trees , outfile = outfile , columns = 10 ) print len ( trees ) , "trees" outfiles += [ outfile ] else : print "skipped" , textname return outfiles
exports complete project
for every sentence, trees of annotators in given order.
exports complete project for every sentence trees of annotators in given order .
"""
	exports complete project
	for every sentence, trees of annotators in given order.
	if no tree: throw error 
	
	""" # for each text
project
annotators
fileExtension