preprocessing.py 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418
  1. import re
  2. from .aggregator import group
  3. def symmetric(latex: str):
  4. key = 0
  5. l = re.split(r'(\\left|\\right)', latex)
  6. for i in range(len(l) - 1, 0, -1):
  7. if l[i] == '\\right' and l[i + 1].strip('\\').startswith('.'):
  8. key = 0
  9. elif l[i] == '\\right':
  10. key += 1
  11. elif l[i] == '\\left' and key == 0:
  12. return l[i + 1].strip('\\').startswith('{')
  13. elif l[i] == '\\left':
  14. key -= 1
  15. def _absolute(maple: str):
  16. # mp = re.split(r'(\|)([+-/*=])',maple)
  17. if maple.count('|') > 1 and '||' not in maple:
  18. right = 0
  19. left = 0
  20. for r in range(right, len(maple)):
  21. if maple[r] == '|':
  22. right = r
  23. break
  24. for l in range(len(maple) - 1, left, -1):
  25. if maple[l] == '|':
  26. left = l
  27. break
  28. if maple[right + 1:left] not in '+-/*=·⋅' and not maple[right + 1:left].isupper():
  29. maple = maple[:right] + 'abs(' + maple[right + 1:left] + ')' + maple[left + 1:]
  30. elif maple[right + 1:left] not in '+-/*=·⋅' and maple[right + 1:left].isupper():
  31. maple = maple[:right] + '#' + maple[right + 1:left] + '#' + maple[left + 1:]
  32. return _absolute(maple)
  33. return maple
  34. def _t1(maple: str):
  35. if maple.count('|') % 2 != 0:
  36. maple = maple.replace('||', '#|')
  37. return maple
  38. # 旧版的绝对值 舍弃
  39. def __absolute(maple: str):
  40. if re.search('\|\|.*\|\|', maple):
  41. return maple
  42. if maple.count('|') == 2 and not re.search(r'\|(.*?)\|', maple).group().isupper() and \
  43. not re.findall(r'[,{}=]', re.search(r'\|(.*?)\|', maple).group()):
  44. return re.sub(r'\|(.*?)\|', r'abs(\1)', maple)
  45. mp_list = [_t1(i) for i in re.split(r'(\|)([·⋅+-/*=⩽⩾<>])|([,,;])', maple) if i]
  46. for i in range(len(mp_list)):
  47. if mp_list[i] == '|' and i != 0:
  48. mp_list[i - 1] = mp_list[i - 1] + '|'
  49. maple = ''.join([_absolute(i) for i in mp_list if i != '|'])
  50. if mp_list[0] == '|':
  51. maple = '|' + maple
  52. return maple.replace('#', '|')
  53. def absolute(s):
  54. inx = []
  55. for i in range(len(s)):
  56. if s[i] == "|":
  57. inx.append(i)
  58. if len(inx) > 1 and len(inx) % 2 == 0:
  59. e = inx.pop(-1)
  60. b = inx.pop(-1)
  61. if re.search('}.*?{',s[b + 1:e]):
  62. return s
  63. s = s.replace(s[b:e + 1], 'abs(' + s[b + 1:e] + ')')
  64. if len(inx) > 1:
  65. s = absolute(s)
  66. return s
  67. def double(matched):
  68. value = matched.group('value')
  69. if re.search('[+\-*/]', value):
  70. if not re.search('[,,;;。><]', value):
  71. return str(value).replace('[', '(').replace(']', ')')
  72. return value
  73. def zhongkuohao(maple: str):
  74. if re.search(r'\[.*?\]', maple):
  75. s = re.search(r'\[.*?\]', maple).group()
  76. if s:
  77. if re.search('[fg]\([^/,,]*?\)', s):
  78. maple = re.sub(r'\[(.*?)\]', r'(\1)', maple)
  79. # maple = maple.replace(']', ')')
  80. maple = re.sub('(?P<value>\[.*?])', double, maple)
  81. if 'log(' in maple:
  82. start = re.search('log\(', maple).end()-1
  83. b = 0
  84. end = 0
  85. for i in range(start, len(maple)):
  86. if maple[i] == '(':
  87. b += 1
  88. elif maple[i] == ')':
  89. b -= 1
  90. if b == 0:
  91. end = i
  92. break
  93. if start and end:
  94. maple = maple.replace(maple[start:end + 1], '[%s]' % (maple[start + 1:end]))
  95. return maple
  96. def multip(maple: str):
  97. maple = re.sub(r'([0-9])([ABCifghkabcmnxXYZyzepθπαφβωλt])', r'\1*\2', maple) # 乘法
  98. maple = re.sub(r'([πkabcmnxyzωλte])([abcdklmnrxyzepθπαφβωλti])', r'\1*\2', maple) # 乘法问题
  99. maple = re.sub(r'([0-9kabcmnxyztei])(log|ln|sin|cos|tan|cot|lg|sqrt|abs|[fgFG]\()', r'\1*\2', maple)
  100. maple = re.sub(r'([^a-z])([kabcmnxyzeti])(\([^)><=⩽⩾≠∈,;]+\))', r'\1\2*\3', maple)
  101. maple = re.sub(r'(\([^)><=⩽⩾≠∈,;]+\))([abcmnxyzeki]|log|ln|sin|cos|tan|cot|lg|sqrt|abs|[fg]\()', r'\1*\2', maple)
  102. maple = re.sub(r'(\)[^)><=⩽⩾≠∈,;]*\))([abcmnxyzeki]|log|ln|sin|cos|tan|cot|lg|sqrt|abs|[fg]\()', r'\1*\2', maple)
  103. maple = re.sub(r'(\([^)><=⩽⩾≠∈,;]+\))(\([^)><=⩽⩾≠∈,;]+\))', r'\1*\2', maple)
  104. maple = re.sub(r'([0-9x])(\([^)><=⩽⩾≠∈,;]+\))', r'\1*\2', maple)
  105. maple = re.sub(r'(\([^)><=⩽⩾≠∈,;]+\))(\d)', r'\1*\2', maple)
  106. maple = re.sub(r'\)([0-9abci])', r')*\1', maple)
  107. maple = re.sub(r'__(\d)\*\(', r'__\1(', maple)
  108. if not re.search(r'<.*?>', maple):
  109. maple = re.sub('⋅', '*', maple)
  110. maple = maple.replace('m*a*x', 'max')
  111. maple = maple.replace('ma*x', 'max')
  112. maple = maple.replace('m*ax', 'max')
  113. maple = maple.replace('m*in', 'min') # tjt新增
  114. maple = maple.replace('a*bs', 'abs')
  115. maple = maple.replace('t*an', 'tan')
  116. maple = maple.replace('()', '')
  117. maple = maple.replace('___', '__')
  118. maple = maple.replace(')(f(', ')*(f(')
  119. maple = re.sub(r'([ABCi0-9π])(sin|cos|tan|cot)', r'\1*\2', maple)
  120. maple = re.sub(r'([ABCabci0-9π])(f\(.*?\)|g\(.*?\)|h\(.*?\))', r'\1*\2', maple)
  121. maple = re.sub(r'([ABCabci0-9π])(f\^|g\^|h\^)', r'\1*\2', maple)
  122. # maple = maple.strip(';.')
  123. return maple
  124. def sin_cos(s):
  125. s = re.sub('sin([ABCi0-9ωφxπαβ+\-*/]+)', r'sin(\1)', s)
  126. s = re.sub('cos([ABCi0-9ωφxπαβ+\-*/]+)', r'cos(\1)', s)
  127. s = re.sub('tan([ABCi0-9ωφxπαβ+\-*/]+)', r'tan(\1)', s)
  128. s = re.sub('cot([ABCi0-9ωφxπαβ+\-*/]+)', r'cot(\1)', s)
  129. s = s.replace('-)', ')-').replace('+)', ')+')
  130. return s
  131. def danwei(s):
  132. l1 = [('{J}', 'J'), ('{c*m/s}', 'cm/s'), ('{kg/m^3}', 'kg/m^3'), ('{N}', 'N'), ('{Hz}', 'Hz'), ('{k*m^2}', 'km^2'),
  133. ('{c*m^2}', 'cm^2'), ('{㎛}', '㎛'), ('{k*m/h}', 'km/h'), ('{dm^2}', 'dm^2'), ('{dm}', 'dm'), ('{t}', 't'),
  134. ('{k*m^3}', 'km^3'), ('{h}', 'h'), ('{m*m/s}', 'mm/s'), ('{dB}', 'dB'), ('{m*m^3}', 'mm^3'), ('{mA}', 'mA'),
  135. ('{㎛^2}', '㎛^2'), ('{m/s}', 'm/s'), ('{c*m}', 'cm'), ('{k*m}', 'km'), ('{V}', 'V'), ('{m^2}', 'm^2'),
  136. ('{MPa}', 'MPa'), ('{m*m}', 'mm'), ('{℃}', '℃'), ('{㎚^3}', '㎚^3'), ('{L}', 'L'), ('{㎚^2}', '㎚^2'),
  137. ('{㎍}', '㎍'),
  138. ('{min}', 'min'), ('{W}', 'W'), ('{Ω}', 'Ω'), ('{s}', 's'), ('{m}', 'm'), ('{g}', 'g'), ('{Pa}', 'Pa'),
  139. ('{dm^3}', 'dm^3'), ('{℉}', '℉'), ('{kg}', 'kg'), ('{㎚}', '㎚'), ('{m*m^2}', 'mm^2'), ('{㎛^3}', '㎛^3'),
  140. ('{m^3}', 'm^3'), ('{mg}', 'mg'), ('{c*m^3}', 'cm^3'), ('{A}', 'A'), ('{mL}', 'mL')]
  141. l2 = [('c*m/s', 'cm/s'), ('k*m^2', 'km^2'), ('c*m^2', 'cm^2'), ('k*m/h', 'km/h'), ('k*m^3', 'km^3'),
  142. ('m*m/s', 'mm/s'), ('i*mg', 'img'), (r'^)', r')^'), ('f^(-1)*', '反函数f'), ('g^(-1)*', '反函数g'),
  143. ('h^(-1)*', '反函数h'),
  144. ('m*m^3', 'mm^3'), ('c*m', 'cm'), ('k*m', 'km'), ('m*m', 'mm'), ('m*m^2', 'mm^2'), ('c*m^3', 'cm^3')]
  145. for i, j in l1:
  146. s = s.replace(i, j)
  147. for i, j in l2:
  148. s = s.replace(i, j)
  149. return s
  150. def last_replace(s):
  151. if s.count(']') - s.count('[')>0:
  152. s = s.replace(')]',')')
  153. if re.search('\{.*\}',s):
  154. s= s.replace(r'\\',';')
  155. if re.search(r'log\[.*?]',s):
  156. s = re.sub(r'log\[(.*?)]([^=(]*?)$',r'log[\1](\2)',s)
  157. s = re.sub(r'log\[(.*?)]([^=(]*?)=',r'log[\1](\2)=',s)
  158. while s.count('{') != s.count('}') and '{{' in s:
  159. s=s.replace('{{','{')
  160. while s.count('{') != s.count('}') and '}}' in s:
  161. s=s.replace('}}','}')
  162. li = [(r'log\(\((.*?)\)\)', r'log[\1]'),
  163. (r'log\((.*?)\)\*\(', r'log[\1]('),
  164. (r';;', r';'),
  165. (r'*)*',r')*'),
  166. ('([fgh])\^(\d)\*\((.*?)\)', r'(\1(\3))^\2'),
  167. ('lg\(\^(\d)\*(.*?)\)',r'(lg(\2))^\1'),
  168. ]
  169. for k, v in li:
  170. try:
  171. if re.search(k, s):
  172. s = re.sub(k, v, s)
  173. except:
  174. ...
  175. if '反函数' in s:
  176. s='反函数'+s.replace('反函数','')
  177. if '一拔' in s:
  178. s='一拔'+s.replace('一拔','')
  179. return s
  180. def wap(func):
  181. def inner(*args, **kwargs):
  182. s = func(*args, **kwargs)
  183. s = sin_cos(s)
  184. s = absolute(s)
  185. s = zhongkuohao(s)
  186. s = multip(s)
  187. s = danwei(s)
  188. s = last_replace(s)
  189. return s
  190. return inner
  191. def format_latex(latex: str):
  192. latex = latex.strip('$')
  193. latex = re.sub(r'^\\\[', '', latex)
  194. latex = re.sub(r'\\\]$', '', latex)
  195. latex = re.sub(r'&lt;', '<', latex)
  196. # latex = re.sub(r'\^{\^', '\^{', latex)
  197. latex = re.sub(r'&gt;', '>', latex)
  198. latex = re.sub(r'^\[', '【', latex)
  199. latex = latex.replace(r'^{}', '')
  200. # latex = re.sub(r'\\log _(\d+)',r'log[\1]',latex)
  201. # tjt修改---({m}_{1},{{r}^{2}}特殊处理)
  202. latex = re.sub(r'{([A-Za-z]{1,3})}_{([0-9])}', r'{\1_\2}', latex)
  203. latex = re.sub(r'{([A-Za-z0-9]{1,3})}\^{([A-Za-z0-9]{1,3})}', r'{\1^\2}', latex)
  204. # formula_list = ["{[A-Za-z]}_{[0-9]}","{[A-Za-z0-9]}\^{[A-Za-z0-9]}"]
  205. # re_list = re.findall('|'.join(formula_list), s)
  206. # if len(re_list) > 0:
  207. # latex_list = [ele for ele in re_list if ele.strip() != '']
  208. # if len(latex_list) == len(re_list):
  209. # latex_list = [ele.replace('}_{','_').replace('}^{','^')
  210. # for ele in latex_list]
  211. # for i in range(len(latex_list)):
  212. # s = s.replace(re_list[i], latex_list[i], 1)
  213. latex = re.sub(r'\\log _{\\sqrt(.*?)}', r'log[sqrt(\1)]', latex)
  214. latex = re.sub(r'\\log _{([^\\=]*?)}',r'log[\1]',latex)
  215. # latex = latex.replace(r'_{', '{')
  216. latex = latex.replace(r'{}', '')
  217. latex = latex.replace(r'&', '')
  218. # if re.search(r'\]$', latex):
  219. # latex = re.sub(r'\]$', '】', latex)
  220. # latex = re.sub(r'\[', '【', latex,1,re.VERBOSE)
  221. # latex = re.sub(r'^\(', '(', latex)
  222. # latex = re.sub(r'\)$', ')', latex)
  223. # latex = re.sub(r'\\sqrt\[(\d+)\]]', r'\root{\1}', latex)
  224. latex = latex.replace(r'\right.\left.', '')
  225. latex = latex.replace(r'{\kern 1pt}', '')
  226. latex = latex.replace(r'\;', '')
  227. latex = latex.replace(r'\bar', r'\overline')
  228. latex = latex.replace(r'\overset{\lower0.5em\hbox{$\smash{\scriptscriptstyle\rightharpoonup}$}}', '\overrightarrow')
  229. latex = latex.replace(r'\lower0.5em', '')
  230. latex = latex.replace(r'\smash', '')
  231. latex = latex.replace(r'\user', '')
  232. # latex = re.sub(r'_(.{1,3})\^', r'\1%%', latex)
  233. #
  234. # latex = re.sub(r'_({.*?})\^({.*?})', r'\1%%\2', latex)
  235. # latex = re.sub(r'_({.*?})\^(.)', r'\1%%\2', latex)
  236. # latex = re.sub(r'_(.{1,3})\^', r'\1%%', latex)
  237. latex = re.sub(r'_(.{1,3})\^([^{])', r'\1%%', latex)
  238. latex = re.sub(r'_(.{1,3})\^({[^{}]*?})', r'\1%%T@T\2T@T', latex)
  239. latex = re.sub(r'_(.{1,3})\^({[^{}]*?{[^{}]*?}{[^{}]*?}})', r'\1%%T@T\2T@T', latex) #\int _0^{\frac{\pi }{2}}\left( {\sin x - \cos x} \right)dx
  240. latex = re.sub(r'_(.{1,3})\^({[^{}]*?{{[^{}]*?{[^{}]*?}}}{[^{}]*?}})', r'T@T\1T@T%%T@T\2T@T', latex) #\int _0^{\frac{{\rm{\pi }}}{4}}dx
  241. latex = re.sub(r'_({.*?})\^(.)', r'T@T\1T@T%%\2', latex)
  242. latex = re.sub(r'_({.*?})\^({[^{}]*?{[^{}]*?}{[^{}]*?}})', r'T@T\1T@T%%T@T\2T@T', latex)
  243. latex = re.sub(r'_({.*?})\^({[^{}]*?})', r'T@T\1T@T%%T@T\2T@T', latex)
  244. latex = re.sub(r'_({.*?})\^({[^{}]*?{{[^{}]*?{[^{}]*?}}}{[^{}]*?}})', r'T@T\1T@T%%T@T\2T@T', latex)
  245. latex = re.sub(r'\\begin{array}{\*\{20\}\{c\}\} +\{ *\}.*?\\end\{array\}', '', latex)
  246. latex = re.sub(r'\{\\begin\{array\}\{\*\{20\}\{l\}\}(.*?)\\end\{array\}', r'\1', latex)
  247. latex = latex.replace(r'\,', '')
  248. latex = latex.replace(r'\operatorname', '')
  249. latex = latex.replace(r'\~', '~')
  250. latex = latex.replace(r'\limits', '')
  251. latex = latex.replace(r'\mathop', '')
  252. latex = re.sub(r'\(([^}]*?)\)\^', r'{(\1)}^', latex)
  253. latex = re.sub(r'\\left\((.*?)\\right\)\^', r'{(\1)}^', latex)
  254. if latex.endswith(r'\right.') and symmetric(latex):
  255. latex = latex.replace(r'\right.', r'\right\}')
  256. if r'\right.' in latex and r'\left\{' in latex and r'\right\}' not in latex:
  257. latex = latex.replace(r'\right.', r'\right\}')
  258. latex = latex.replace(r'%27', r"'")
  259. latex = latex.replace(r'\left[', r'\left【')
  260. latex = latex.replace(r'\right]', r'\right】')
  261. latex = latex.replace(r'\left(', '\left(')
  262. latex = latex.replace(r'\right)', r'\right)')
  263. # print(re.search(r'([a-z])_([0-9]) *\^([0-9])',latex))
  264. latex = re.sub(r'([a-z])_([0-9]) *\^([0-9])', r'\1__\2 ^\3', latex)
  265. latex = re.sub(r'{\rm{(.*?)}}',r'\1',latex)
  266. return latex
  267. @wap
  268. def last_clear(s, debug=False):
  269. BList = (
  270. # r'([a-z]_[0-9n])([a-z])': r'\1*\2',
  271. # r'([a-z])_([0-9n]+)': r'\1__\2',
  272. # r'{([0-9a-zA-Z _=+*/\-><]*?)}': r'\1', # 减法问题 括里含有等号
  273. # r'\|': r'∥', #???平行
  274. # r'|.*?|': r'abs(\1)',
  275. # r'([0-9a-zA-Z])(abs|sin|cos|tan|cot)': r'\1*\2',
  276. # r'sin\((\d*\*?\w*)\)cos\((\d*\*?\w*)\)': r'sin(\1)*cos(\2)',
  277. # r'sqrt\((.*?)\)sqrt\((.*?)\)': r'sqrt(\1)*sqrt(\2)',
  278. # r'sqrt\((.*?)\)sin\((.*?)\)': r'sqrt(\1)*sin(\2)',
  279. # r'([0-9a-zA-Z])pi': r'\1*pi',
  280. # r'{([0-9a-zA-Z]+\^[0-9a-zA-Z]+)}': r'\1',
  281. # r'\)\(': r')*(',
  282. # r'(\w|\d+)Pi': r'\1*Pi',
  283. (r'\\left', ''),
  284. (r'\\right', ''),
  285. (r',;', r';'),
  286. (r',\}', r'}'),
  287. (r';\}', r'}'),
  288. (r'\.\}', r'}'),
  289. (r'\{\.', r'{'),
  290. (r'\{\,', r'{'),
  291. (r'\{\;', r'{'),
  292. (r'】', r']'),
  293. (r'【', r'['),
  294. (r'(', r'('),
  295. (r')', r')'),
  296. (r'−', r'-'),
  297. (r'÷', r'/'),
  298. (r'(lg|ln)(sin|cos|tan)\((.*?)\)', r'\1(\2(\3))'),
  299. (r'(lg|ln)(sin|cos|tan)([0-9abmnxyzθπαφβA-Z]{1,2})', r'\1(\2(\3))'),
  300. (r'sin(\d+\^.{1})', r'sin(\1)'),
  301. (r'cos(\d+\^.{1})', r'cos(\1)'),
  302. (r'tan(\d+\^.{1})', r'tan(\1)'),
  303. (r'sin(\^\d)?\(([0-9abmnxyzθπαφβA-Z].*?)\)', r'sin(\2)\1'),
  304. (r'cos(\^\d)?\(([0-9abmnxyzθπαφβA-Z].*?)\)', r'cos(\2)\1'),
  305. (r'sin(\^\d)?([0-9abmnxyzθπαφβA-Z]{1,2})', r'sin(\2)\1'),
  306. (r'cos(\^\d)?([0-9abmnxyzθπαφβA-Z]{1,2})', r'cos(\2)\1'),
  307. (r'tan([0-9abmnxyzθπαβ]{1,2})', r'tan(\1)'),
  308. (r'log\[(.*?)\]([^\+\-*/\(\),;\[\]>⩾⩽≤{}≠≥<=lsc]+)', r'log[\1](\2)'),
  309. (r'ln([^\+\-*=/\(\),;[]+)', r'ln(\1)'),
  310. (r'lg([^\+\-*=/\(\),;[]+)', r'lg(\1)'),
  311. (r'log__([1-9a-zA-Z])', r'log[\1]'),
  312. # (r'C__', r'∁'),
  313. (r'C__([^0-9])', r'∁\1'),
  314. (r'∁__', r'∁'),
  315. (r'≤|⩽', r'<='),
  316. (r'≥|⩾', r'>='),
  317. (r'≠', r'<>'),
  318. (r'x̸⊂', r'⊄'),
  319. (r'{}', r''),
  320. (r'^*', r'*'),
  321. (r'^+', r'+'),
  322. (r'__\+', r'+'),
  323. (r'__\*', r'*'),
  324. (r'__\^', r'^'),
  325. (r';,', r','),
  326. (r'×', r'*'),
  327. (r'﹣', r'-'),
  328. (r'\)\)\(\(', r'))*(('),
  329. (r'\)f\(', r')*f('),
  330. (r'([CAP])([0-9mnk][+-][\dmnk])%%([0-9mnk])', r'\1[\2,\3]'),
  331. (r'T@T(.*?)T@T%%T@T(.*?)T@T', r'[\1,\2]'),
  332. (r'([0-9mnk])%%T@T(.*?)T@T', r'[\1,\2]'),
  333. (r'T@T(.*?)T@T%%([0-9mnk])', r'[\1,\2]'),
  334. (r'([∑∫])(.*?)%%(.*?)([a-zA-Z(*\-+])', r'\1[\2,\3]\4'),
  335. (r'([0-9mnk])%%([0-9mnk])([CAP])', r'[\1,\2]*\3'),
  336. (r'([0-9mnk]*?)%%([0-9mnk]+)', r'[\1,\2]'),
  337. (r'([n\dk])([CAP]\[.{1,3},.{1,3}\])', r'\1*\2'),
  338. (r'([fgh])\^(\d+)\*\((.*?)\)', r'\1(\3)^\2'),
  339. (
  340. r'([0-9a-zA-Z])(km|m|dm|cm|mm|㎛|㎚|L|mL|t|kg|g|mg|㎍|℃|℉|h|min|s|km/h|m/s|cm/s|mm/s|N|kg/m\^3|km\^2|m\^2|dm\^2|cm\^2|mm\^2|㎛\^2|㎚\^2|km\^3|m\^3|dm\^3|cm\^3|mm\^3|㎛\^3|㎚\^3|Pa|A|mA|MPa|V|Ω|J|W|Hz|dB)$',
  341. r'\1*{\2}'),
  342. )
  343. for k, v in BList:
  344. try:
  345. if re.search(k, s):
  346. s = re.sub(k, v, s)
  347. if debug:
  348. print(k, v, '----->', s)
  349. except:
  350. pass
  351. for k, v in BList:
  352. try:
  353. if re.search(k, s):
  354. s = re.sub(k, v, s)
  355. if debug:
  356. print(k, v, '----->', s)
  357. return s
  358. except:
  359. return ''
  360. if __name__ == '__main__':
  361. # symmetric(r'f(x) = \left\{ \begin{gathered} \left| {\lg \left| x \right|} \right|,x \ne 0 \hfill \\ 0,x = 0 \hfill \\ \end{gathered} \right.')
  362. # s = r'f(x) = \left\{ \begin{gathered} \left| {\lg \left| x \right|} \right|,x \ne 0 \hfill \\ 0,x = 0 \hfill \\ \end{gathered} \right.'
  363. # format_latex(s)
  364. print(group(
  365. '<p>由<img src="/data/word/wordimg/2021/10/6166561b71f91.png" style="width: 94pt; height: 17.9pt" data-type="math" data-latex="\[1 + f(2x) = 2{f^2}(x)\]" width="125" height="24"/>,得<img src="/data/word/wordimg/2021/10/6166561b72660.png" style="width: 94pt; height: 17.9pt" data-type="math" data-latex="\[f(2x) = 2{f^2}(x) - 1\]" width="125" height="24"/>,联想到<img src="/data/word/wordimg/2021/10/6166561b72cbb.png" style="width: 95.9pt; height: 16.05pt" data-type="math" data-latex="\[\cos 2x = 2{\cos ^2}x - 1\]" width="128" height="21"/>,可推测<img src="/data/word/wordimg/2021/10/6166561b795a1.png" style="width: 70.1pt; height: 16.05pt" data-type="math" data-latex="\[f(x) = \cos \omega x\]" width="93" height="21"/>.</p><p>由<img src="/data/word/wordimg/2021/10/6166561b79e83.png" style="width: 82.05pt; height: 31.15pt" data-type="math" data-latex="\[f(x) = f(x + \frac{\pi }{2})\]" width="109" height="42"/>,得<img src="/data/word/wordimg/2021/10/6166561b7a6b0.png" style="width: 92.1pt; height: 33pt" data-type="math" data-latex="\[\frac{\pi }{2} = k\frac{{2\pi }}{{|\omega |}}(k \in {{\rm{N}}^*})\]" width="123" height="44"/>.则<img src="/data/word/wordimg/2021/10/6166561b7ae2a.png" style="width: 82.05pt; height: 17.9pt" data-type="math" data-latex="\[|\omega | = 4k(k \in {{\rm{N}}^*})\]" width="109" height="24"/>.又<img src="/data/word/wordimg/2021/10/6166561b7b4e6.png" style="width: 54.1pt; height: 31.15pt" data-type="math" data-latex="\[f(\frac{\pi }{4}) \ne - 1\]" width="72" height="42"/>.所以<img src="/data/word/wordimg/2021/10/6166561b7bbec.png" style="width: 110.05pt; height: 16.05pt" data-type="math" data-latex="\[f(x) = \cos (4kx)(k \in Z\]" width="147" height="21"/>,<img src="/data/word/wordimg/2021/10/6166561b82974.png" style="width: 10.05pt; height: 13.85pt" data-type="math" data-latex="\[k\]" width="13" height="18"/>为偶数,且<img src="/data/word/wordimg/2021/10/6166561b83189.png" style="width: 31.15pt; height: 16.05pt" data-type="math" data-latex="\[|k| &gt; 1\]" width="42" height="21"/>)</p>'))