Nantes Université

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

Add translation to support lg2mml for infty

parent 4a9eb3aa
Aucune branche associée trouvée
Aucune étiquette associée trouvée
Aucune requête de fusion associée trouvée
......@@ -24,4 +24,4 @@ fi
BNAME=`basename $1 .lg`
python $LgEvalDir/src/lg2txt.py $1 $LgEvalDir/translate/mathMLMap.csv > $BNAME.mml
python $LgEvalDir/src/lg2txt.py $1 $LgEvalDir/translate/mathMLMap.csv $LgEvalDir/translate/infty_to_crohme.csv > $BNAME.mml
......@@ -11,11 +11,41 @@
# Copyright (c) 2012-2014 Richard Zanibbi and Harold Mouchere
################################################################
import os
import re
import sys
import csv
from bs4 import BeautifulSoup
from lg import *
def readtranslateFile(mapFile):
"""Read in symbol and structure mappings from a file."""
try:
fileReader = csv.reader(open(mapFile))
except:
sys.stderr.write(' !! IO Error (cannot open): ' + mapFile + '\n')
return
symbolsMap = {}
relationsMap = {}
readingSymbols = True
for row in fileReader:
# Skip blank lines and comments.
if len(row) == 0:
continue
elif row[0].strip()[0] == '#':
continue
elif row[0].strip() == 'SYMBOLS':
readingSymbols = True
elif row[0].strip() == 'RELATIONSHIPS':
readingSymbols = False
else:
if readingSymbols:
symbolsMap[row[0]] = row[1]
else:
relationsMap[row[0]] = row[1]
return symbolsMap, relationsMap
def readMapFile(fileName):
"""Read in symbol and structure mappings from a file."""
try:
......@@ -286,33 +316,7 @@ def cleanRows( mmlFile ):
return rowSoup.prettify()
# 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):
def lg2mml(lg_file, mapFile):
lg = Lg(lg_file)
......@@ -360,18 +364,52 @@ def main(lg_file, mapFile):
mml_out = cleanRows("\n".join(mml_out_raw))
return mml_out
def preprocess(filename, translateFile, translate=True):
lg = Lg(filename)
print(lg)
lg_NE_string = lg.csv()
if translate:
symbolsMap, relationsMap = readtranslateFile(translateFile)
edge_pattern = re.compile(r'^E,')
node_pattern = re.compile(r'(^N,)|#')
symbol_temp = relabel(lg_NE_string, symbolsMap, node_pattern)
edge_temp = relabel(lg_NE_string, dict(**symbolsMap, **relationsMap),
edge_pattern)
lg_NE_string = symbol_temp + "\n" + edge_temp
temp_file = "temp.lg"
with open(temp_file, 'w') as f:
f.writelines(lg_NE_string)
return temp_file
def relabel(lg_NE_string, Map, pattern):
temp = '\n'.join([line for line in lg_NE_string.split('\n')
if re.match(pattern, line)])
for source_label, mapped_label in Map.items():
temp = temp.replace("," + source_label + ",", "," + mapped_label + ",")
return temp
def main(lg_file, mapFile, translateFile):
temp_lg_file = preprocess(lg_file, translateFile)
mml_out = lg2mml(temp_lg_file, mapFile)
os.remove(temp_lg_file)
return mml_out
if __name__ == '__main__':
if len(sys.argv) < 2:
print("Usage: [[python]] lg2txt.py <infile.lg> [mapfile.csv]")
print("Usage: [[python]] lg2txt.py <infile.lg> [mapfile.csv] [translatefile.csv]")
print("")
print(" Produces a text file for label graph file")
print(" <infile.lg>. A symbol and structure map file (mapfile.csv)")
print(" may be provided to override default (latex) mappings.")
sys.exit()
lg_file = sys.argv[1]
if len(sys.argv) > 2:
if len(sys.argv) > 3:
mapFile = sys.argv[2]
translateFile = sys.argv[3]
elif len(sys.argv) > 2:
mapFile = sys.argv[2]
translateFile = None
else:
mapFile = None
mml_out = main(lg_file, mapFile)
mml_out = main(lg_file, mapFile, translateFile)
print(mml_out)
......@@ -177,4 +177,4 @@ UNDER,Below
LSUB,Sub
UPPER,Above
LSUP,Sup
PUNC,Right
PUNC,Sub
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