Skip to content

Commit 3aadac2

Browse files
authored
Merge pull request #28 from SUSYUSTC/dev
Dev
2 parents 598f0c0 + 83eb863 commit 3aadac2

File tree

3 files changed

+18
-4
lines changed

3 files changed

+18
-4
lines changed

mathtranslate/__init__.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
__version__ = "2.1.7"
1+
__version__ = "2.1.8"
22
__author__ = "Jiace Sun"
33

44
import os

mathtranslate/process_latex.py

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
match_code = r"(" + math_code + r"_\d+(?:_\d+)*)"
66
match_code_replace = math_code + r"_(\d+(?:_\d+)*)*"
77

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

1010
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
1111
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
@@ -100,7 +100,7 @@ def replace_latex_objects(text):
100100
pattern = regex.compile(regex_symbol, regex.DOTALL)
101101
while pattern.search(text):
102102
latex_obj = pattern.search(text).group()
103-
replaced_objs.append(f'{latex_obj}')
103+
replaced_objs.append(f' {latex_obj} ')
104104
text = pattern.sub(' ' + variable_code(count) + ' ', text, 1)
105105
count += 1
106106

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

299299
return text
300+
301+
302+
def combine_sentences(text):
303+
pattern = re.compile(r'\n(\s*([^\s]))')
304+
305+
def process_function(match):
306+
char = match.group(2)
307+
if char == '\\':
308+
return match.group(0)
309+
else:
310+
return ' '+match.group(1)
311+
312+
return pattern.sub(process_function, text)

mathtranslate/translate.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -92,13 +92,14 @@ def translate_paragraph_latex(self, latex_original_paragraph, num, complete):
9292
'''
9393
Translate a latex paragraph, which means that it could contain latex objects
9494
'''
95+
latex_original_paragraph = process_latex.combine_sentences(latex_original_paragraph)
9596
text_original_paragraph, objs = process_latex.replace_latex_objects(latex_original_paragraph)
9697
# Since \n is equivalent to space in latex, we change \n back to space
9798
# otherwise the translators view them as separate sentences
98-
text_original_paragraph = text_original_paragraph.replace('\n', '')
9999
text_original_paragraph = process_text.split_too_long_paragraphs(text_original_paragraph)
100100
if not complete:
101101
text_original_paragraph = process_text.split_titles(text_original_paragraph)
102+
text_original_paragraph = re.sub(r' +', ' ', text_original_paragraph)
102103
if self.debug:
103104
print(f'\n\nParagraph {num}\n\n', file=self.f_old)
104105
print(text_original_paragraph, file=self.f_old)

0 commit comments

Comments
 (0)