Skip to content

Commit

Permalink
fix bugs
Browse files Browse the repository at this point in the history
  • Loading branch information
SUSYUSTC committed Mar 29, 2023
1 parent 2fc858a commit 2869c0f
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 16 deletions.
40 changes: 25 additions & 15 deletions mathtranslate/translate.py
Original file line number Diff line number Diff line change
Expand Up @@ -148,7 +148,7 @@ def translate_latex_env(self, latex_original, names, complete):
def translate_latex_commands(self, latex_original, names, complete):
return self._translate_latex_objects(process_latex.process_specific_commands, latex_original, names, complete)

def translate_full_latex(self, latex_original):
def translate_full_latex(self, latex_original, loadmain=False):
latex_original = process_latex.remove_tex_comments(latex_original)
complete = process_latex.is_complete(latex_original)
if complete:
Expand All @@ -166,32 +166,42 @@ def translate_full_latex(self, latex_original):
# It is difficult for regex to exclude \{ during match so I replace it to something else and then replace back
latex_original = latex_original.replace(r'\{', f'{math_code}LB')
latex_original = latex_original.replace(r'\}', f'{math_code}RB')
# The following two can probably be put somewhere else
latex_original = latex_original.replace(r'\%', f'{math_code}PC')
latex_original = latex_original.replace(r'\ ', f'{math_code}SP')

latex_original_paragraphs = self.split_latex_to_paragraphs(latex_original)
latex_translated_paragraphs = []
if loadmain:
latex_translated = open('text_after_main.txt').read()
else:
latex_original_paragraphs = self.split_latex_to_paragraphs(latex_original)
latex_translated_paragraphs = []

num = 0
print('processing main text')
for latex_original_paragraph in latex_original_paragraphs:
latex_translated_paragraph = self.translate_paragraph_latex(latex_original_paragraph, num, complete)
latex_translated_paragraphs.append(latex_translated_paragraph)
print(num, '/', len(latex_original_paragraphs))
num += 1
latex_translated = '\n\n'.join(latex_translated_paragraphs)
num = 0
print('processing main text')
for latex_original_paragraph in latex_original_paragraphs:
latex_translated_paragraph = self.translate_paragraph_latex(latex_original_paragraph, num, complete)
latex_translated_paragraphs.append(latex_translated_paragraph)
print(num, '/', len(latex_original_paragraphs))
num += 1
latex_translated = '\n\n'.join(latex_translated_paragraphs)

if self.debug:
print(latex_translated, file=open('text_after_main.txt', 'w'))
if self.debug:
print(latex_translated, file=open('text_after_main.txt', 'w'))

# TODO: add more here
environment_list = ['abstract', 'acknowledgments', 'itemize', 'enumerate', 'description', 'list']
# addition: ['theorem', 'proposition', 'conjecture', 'lemma', 'claim', 'fact', 'corollary', 'remark', 'definition', 'example', 'proof']
print('processing latex environments')
latex_translated = self.translate_latex_env(latex_translated, ['abstract', 'acknowledgments', 'itermize', 'enumrate', 'description', 'list'], complete)
latex_translated = self.translate_latex_env(latex_translated, environment_list, complete)

command_list = ['section', 'subsection', 'subsubsection', 'caption', 'subcaption', 'footnote', 'paragraph']
print('processing latex commands')
latex_translated = self.translate_latex_commands(latex_translated, ['section', 'subsection', 'subsubsection', 'caption', 'subcaption', 'footnote', 'paragraph'], complete)
latex_translated = self.translate_latex_commands(latex_translated, command_list, complete)

latex_translated = latex_translated.replace(f'{math_code}LB', r'\{')
latex_translated = latex_translated.replace(f'{math_code}RB', r'\}')
latex_translated = latex_translated.replace(f'{math_code}PC', r'\%')
latex_translated = latex_translated.replace(f'{math_code}SP', r'\ ')

latex_translated = tex_begin + '\n' + latex_translated + '\n' + tex_end

Expand Down
3 changes: 2 additions & 1 deletion mathtranslate/translate_tex.py
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,7 @@ def main():
parser.add_argument("--setkey", action='store_true', help='set id and key of tencent translator')
parser.add_argument("--setdefault", action='store_true', help='set default translation engine and languages')
parser.add_argument("--debug", action='store_true')
parser.add_argument("--loadmain", action='store_true')
parser.add_argument("--compile", action='store_true')
options = parser.parse_args()

Expand Down Expand Up @@ -155,7 +156,7 @@ def main():

input_encoding = get_file_encoding(input_path)
text_original = open(input_path, encoding=input_encoding).read()
text_final = latex_translator.translate_full_latex(text_original)
text_final = latex_translator.translate_full_latex(text_original, options.loadmain)
with open(output_path, "w", encoding='utf-8') as file:
print(text_final, file=file)
print(output_path, 'is generated')
Expand Down

0 comments on commit 2869c0f

Please sign in to comment.