Nantes Université

Skip to content
Extraits de code Groupes Projets
Valider 544c49de rédigé par ayushkumarshah's avatar ayushkumarshah
Parcourir les fichiers

Remove txl usage for pretty formatting

Create custom function fot pretty formatting the mathml output
parent 7d20673d
Aucune branche associée trouvée
Aucune étiquette associée trouvée
Aucune requête de fusion associée trouvée
......@@ -24,6 +24,6 @@ fi
BNAME=`basename $1 .lg`
python $LgEvalDir/src/lg2txt.py $1 $LgEvalDir/translate/mathMLMap.csv > temp.mml
txl -I $CROHMELibDir/src -I $CROHMELibDir/src/Grammars temp.mml $CROHMELibDir/src/pprintMathML.Txl > $BNAME.mml
rm temp.mml
python $LgEvalDir/src/lg2txt.py $1 $LgEvalDir/translate/mathMLMap.csv > $BNAME.mml
# txl -I $CROHMELibDir/src -I $CROHMELibDir/src/Grammars temp.mml $CROHMELibDir/src/pprintMathML.Txl > $BNAME.mml
# rm temp.mml
......@@ -83,16 +83,16 @@ def translateStructure( lg, label, nodeRelationPairs, structureMap,\
for (childId, relation) in sortedNodeRelationPairs:
queryList += [ relation ]
#print(primListString)
#print(queryList)
# Obtain the replacement, provided as an ordered sequence of
# regions, giving the order in which to map subregions.
#print(primListString)
#print(queryList)
# Obtain the replacement, provided as an ordered sequence of
# regions, giving the order in which to map subregions.
key = tuple(queryList)
anyKey = tuple(['ANY'] + queryList[1:])
#print("key: " + str(key))
#print(list(structureMap))
#print("key: " + str(key))
#print(list(structureMap))
if key in list(structureMap):
replacementTuple = structureMap[ key ]
#print("replacement: " + str(replacementTuple))
......@@ -117,7 +117,7 @@ def translateStructure( lg, label, nodeRelationPairs, structureMap,\
strString += replacementTuple[i].replace('_I_','\"' + \
primListString + '\"')
# HACK!!! Copying and modifying above conditional branch.
# HACK!!! Copying and modifying above conditional branch.
elif anyKey in list(structureMap):
replacementTuple = structureMap[ anyKey ]
#print("replacement: " + str(replacementTuple))
......@@ -193,7 +193,7 @@ def translate(lg, segId, segPrimMap, edgeMap, symbolMap, structureMap):
nodeString = label
# Create label identifying primitives in the object.
# Create label identifying primitives in the object.
primListString = ""
for primitiveId in sorted(list(segPrimMap[ segId ][0])):
primListString += primitiveId + ':'
......@@ -271,26 +271,51 @@ def translate(lg, segId, segPrimMap, edgeMap, symbolMap, structureMap):
return nodeString
def postprocess(mml_out):
# Add linebreaks for tags expect for mi, mo, mn
mml_out = mml_out.replace("><", ">\n<")
mml_out = mml_out.replace("</", "\n</")
mml_out = mml_out.replace("\n</mi", "</mi")
mml_out = mml_out.replace("\n</mo", "</mo")
mml_out = mml_out.replace("\n</mn", "</mn")
# tags with no indentation
constant_tags = ["mi", "mo", "mn"]
# Counts number of tab sapces
tab = 0
out = []
for line in mml_out.split("\n"):
if not line:
continue
# if re.match(r"<\w", line):
if line.startswith("<m"):
out.append(tab * "\t" + line)
if not line[1:3] in constant_tags:
tab += 1
elif line.startswith("</"):
tab -= 1
out.append(tab * "\t" + line)
return "\n".join(out)
def main(lg_file, mapFile):
lg = Lg(lg_file)
# Hide the unlabeled edges.
# Hide the unlabeled edges.
lg.hideUnlabeledEdges()
(segmentPrimitiveMap, primitiveSegmentMap, noparentSegments, segmentEdges) = \
lg.segmentGraph()
(rootNodes, treeEdges, otherEdges) = lg.separateTreeEdges()
# Default symbol and structure mappings.
# Default symbol and structure mappings.
symbolMap = { }
structureMap = { }
if mapFile:
(symbolMap, structureMap) = readMapFile(mapFile)
# Create a map from nodes to child nodes, in order to be able to
# detect structures such as fractions, etc.
# Create a map from nodes to child nodes, in order to be able to
# detect structures such as fractions, etc.
treeEdgeMap = {}
for (parent, child) in treeEdges:
if parent in treeEdgeMap:
......@@ -298,24 +323,25 @@ def main(lg_file, mapFile):
else:
treeEdgeMap[ parent ] = [ child ]
# NOTE: currently this will print out more than one expression on
# separate lines if a graph has multiple root nodes.
# Exit if there is no root node, generate a list of TeX expressions if there are
# multiple root nodes.
# NOTE: currently this will print out more than one expression on
# separate lines if a graph has multiple root nodes.
# Exit if there is no root node, generate a list of TeX expressions if there are
# multiple root nodes.
if len(rootNodes) < 1:
sys.stderr.write(" !! Error: graph contains no root node; cannot generate output.\n")
sys.exit(1)
elif len(rootNodes) > 1:
sys.stderr.write(" !! Graph contains " + str(len(rootNodes)) + " root nodes.\n")
mml_out = []
mml_out_raw = []
for root in rootNodes:
# print(translate(lg, root, segmentPrimitiveMap, treeEdgeMap,\
# symbolMap, structureMap))
mml_out.append(translate(lg, root, segmentPrimitiveMap,
mml_out_raw.append(translate(lg, root, segmentPrimitiveMap,
treeEdgeMap, symbolMap, structureMap))
return '\n'.join(mml_out)
mml_out = postprocess("\n".join(mml_out_raw))
return mml_out
if __name__ == '__main__':
if len(sys.argv) < 2:
......
0% Chargement en cours ou .
You are about to add 0 people to the discussion. Proceed with caution.
Terminez d'abord l'édition de ce message.
Veuillez vous inscrire ou vous pour commenter