Skip to content

Commit

Permalink
Merge pull request #28 from SUSYUSTC/dev
Browse files Browse the repository at this point in the history
Dev
  • Loading branch information
SUSYUSTC authored Apr 2, 2023
2 parents 598f0c0 + 83eb863 commit 3aadac2
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 4 deletions.
2 changes: 1 addition & 1 deletion mathtranslate/__init__.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
__version__ = "2.1.7"
__version__ = "2.1.8"
__author__ = "Jiace Sun"

import os
Expand Down
17 changes: 15 additions & 2 deletions mathtranslate/process_latex.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
match_code = r"(" + math_code + r"_\d+(?:_\d+)*)"
match_code_replace = math_code + r"_(\d+(?:_\d+)*)*"

options = r"\[[a-zA-Z\s,\\\*\.\+\-=_]*?\]" # ,\*.+-=_
options = r"\[[a-zA-Z\s,\\\*\.\+\-=_{}]*?\]" # ,\*.+-=_{}

pattern_env = r"\\begin[ \t]*\{(.*?)\}[ \t]*(options)?(.*?)\\end[ \t]*\{\1\}".replace('options', options) # \begin{xxx} \end{xxx}, group 1: name, group 2: option, group 3: content
pattern_command_full = r"\\([a-zA-Z]+\*?)[ \t]*(options)?[ \t]*(\{((?:[^{}]++|(?3))++)\})".replace('options', options) # \xxx[xxx]{xxx} and \xxx{xxx}, group 1: name, group 2: option, group 4: content
Expand Down Expand Up @@ -100,7 +100,7 @@ def replace_latex_objects(text):
pattern = regex.compile(regex_symbol, regex.DOTALL)
while pattern.search(text):
latex_obj = pattern.search(text).group()
replaced_objs.append(f'{latex_obj}')
replaced_objs.append(f' {latex_obj} ')
text = pattern.sub(' ' + variable_code(count) + ' ', text, 1)
count += 1

Expand Down Expand Up @@ -297,3 +297,16 @@ def replace_function(match):
text = re.compile(match_code_accent).sub(replace_function, text)

return text


def combine_sentences(text):
pattern = re.compile(r'\n(\s*([^\s]))')

def process_function(match):
char = match.group(2)
if char == '\\':
return match.group(0)
else:
return ' '+match.group(1)

return pattern.sub(process_function, text)
3 changes: 2 additions & 1 deletion mathtranslate/translate.py
Original file line number Diff line number Diff line change
Expand Up @@ -92,13 +92,14 @@ def translate_paragraph_latex(self, latex_original_paragraph, num, complete):
'''
Translate a latex paragraph, which means that it could contain latex objects
'''
latex_original_paragraph = process_latex.combine_sentences(latex_original_paragraph)
text_original_paragraph, objs = process_latex.replace_latex_objects(latex_original_paragraph)
# Since \n is equivalent to space in latex, we change \n back to space
# otherwise the translators view them as separate sentences
text_original_paragraph = text_original_paragraph.replace('\n', '')
text_original_paragraph = process_text.split_too_long_paragraphs(text_original_paragraph)
if not complete:
text_original_paragraph = process_text.split_titles(text_original_paragraph)
text_original_paragraph = re.sub(r' +', ' ', text_original_paragraph)
if self.debug:
print(f'\n\nParagraph {num}\n\n', file=self.f_old)
print(text_original_paragraph, file=self.f_old)
Expand Down

0 comments on commit 3aadac2

Please sign in to comment.