【问题标题】:RegEx to insert spaces between (Number-Number), (Number-char), (Char-Special char) and (special char- char) except '\'RegEx 在 (Number-Number)、(Number-char)、(Char-Special char) 和 (special char- char) 之间插入空格,除了 '\'
【发布时间】:2021-08-01 10:07:08
【问题描述】:

我编写了一个代码来清理LaTex(只是一个字符串),我想在其中插入空格以标记字符串。我的代码如下:

def insert_spaces(sentence):
  '''
  Add a space around special characters, number and digits. So "2x+y -1/3x" becomes: "2 x + y - 1 / 3 x"
  '''
  dummy_list = []
  splitted_sent = list(sentence)
    
  for i in range(len(splitted_sent)-1):
    dummy_list.append(splitted_sent[i])
    
    if splitted_sent[i].isalpha(): # if it is an alphabet
      if splitted_sent[i+1].isdigit() or (not splitted_sent[i+1].isalnum()):
        dummy_list.append(' ')
    
    elif splitted_sent[i].isdigit(): # if it is a number
      if splitted_sent[i+1].isalpha() or (not splitted_sent[i+1].isalnum()):
        dummy_list.append(' ')
        
    elif (not splitted_sent[i].isalnum()) and (splitted_sent[i] not in [' ','\\']): # if it is a special char but not ' ' already
      if splitted_sent[i+1].isalnum():
        dummy_list.append(' ')
        
  dummy_list.append(splitted_sent[-1])
  
  return ''.join(dummy_list)

例如,如果我的原始查询是:

'ds^{2} = (1 - {qcos\\theta\\over r})^{2\\over 1 + \\alpha^{2}}\\lbrace dr^2+r^2d\\theta^2+r^2sin^2\\theta d\\varphi^2\\rbrace -{dt^2\\over  (1 - {qcos\\theta\\over r})^{2\\over 1 + \\alpha^{2}}}\\, .\\label{eq:sps1} \\widetilde\\gamma_{\\rm hopf}\\simeq\\sum_{n>0}\\widetilde{G}_n{(-a)^n\\over2^{2n-1}}\\label{H4}3455'

然后我希望它被清理为:

'd s ^ { 2 } = ( 1  - { q c o s \\theta \\over  r } ) ^ { 2 \\over  1  + \\alpha ^ { 2 } } \\lbrace  d r ^ 2 + r ^ 2 d \\theta ^ 2 + r ^ 2 sin ^ 2 \\theta  d \\varphi ^ 2 \\rbrace  -{ d t ^ 2 \\over   ( 1  - { q c o s \\theta \\over  r } ) ^ { 2 \\over  1  + \\alpha ^ { 2 } } } \\ , . \\label { eq : sps 1 } \\widetilde \\gamma _ { \\rm  h o p f } \\simeq \\sum _ { n > 0 } \\widetilde { G } _ n { ( - a ) ^ n \\over 2 ^ { 2 n - 1 } } \\label { H 4 } 3 4 5 5'

The above result is a product of this this script 基本上调用this KaTex script

但是现在,我得到的结果是:

'ds ^{ 2 } = ( 1  - { qcos \\theta \\over  r })^{ 2 \\over  1  + \\alpha ^{ 2 }}\\lbrace  dr ^ 2 + r ^ 2 d \\theta ^ 2 + r ^ 2 sin ^ 2 \\theta  d \\varphi ^ 2 \\rbrace  -{ dt ^ 2 \\over   ( 1  - { qcos \\theta \\over  r })^{ 2 \\over  1  + \\alpha ^{ 2 }}}\\, .\\label { eq : sps 1 } \\widetilde \\gamma _{\\rm  hopf }\\simeq \\sum _{ n > 0 }\\widetilde { G }_ n {(- a )^ n \\over 2 ^{ 2 n - 1 }}\\label { H 4 } 3455'

有没有什么方法可以在 RegEx 的帮助下达到同样的效果?

【问题讨论】:

  • 您想要的输出似乎也将\\over 转换为frac?您能否提供有关您尝试应用的转换的更多信息?
  • 这是我从 repo 本身使用的唯一代码。现在,我将只更新所需的输出。

标签: python regex text nlp re


【解决方案1】:

您的预期输出中有一些令人讨厌的违规行为,例如

  • 不规则的额外空格
  • {eq:sps1} 你想输出为{ eq : sps 1 })
  • sin 与所有其他词(例如q c o s)相反,应该作为sin 保持在一起,而不是转换为s i n
  • -{ 在某些时候没有像我预期的那样转换为 - {

这些异常违反了我在测试期间能够提出的任何正则表达式。但这与您要达到的目标非常接近,也许您会发现它作为起点很有用...

更新:根据cmets更改了预期结果,已修复

  • sin -> s i n
  • -{ -> - {

除了空格,这个正则表达式应该处理你的例子。请注意,由于您的描述,例如eq:... 是正则表达式的一部分,可按照您的要求将 eqsqs 保持在一起。

import re

text = r'ds^{2} = (1 - {qcos\\theta\\over r})^{2\\over 1 + \\alpha^{2}}\\lbrace dr^2+r^2d\\theta^2+r^2sin^2\\theta d\\varphi^2\\rbrace -{dt^2\\over  (1 - {qcos\\theta\\over r})^{2\\over 1 + \\alpha^{2}}}\\, .\\label{eq:sps1} \\widetilde\\gamma_{\\rm hopf}\\simeq\\sum_{n>0}\\widetilde{G}_n{(-a)^n\\over2^{2n-1}}\\label{H4}3455'

# updated version according to comments, fixed in expected result:
# * "sin" -> "s i n"
# * "-{" -> "- {"
#expected = r'd s ^ { 2 } = ( 1  - { q c o s \\theta \\over  r } ) ^ { 2 \\over  1  + \\alpha ^ { 2 } } \\lbrace  d r ^ 2 + r ^ 2 d \\theta ^ 2 + r ^ 2 sin ^ 2 \\theta  d \\varphi ^ 2 \\rbrace  -{ d t ^ 2 \\over   ( 1  - { q c o s \\theta \\over  r } ) ^ { 2 \\over  1  + \\alpha ^ { 2 } } } \\ , . \\label { eq : sps 1 } \\widetilde \\gamma _ { \\rm  h o p f } \\simeq \\sum _ { n > 0 } \\widetilde { G } _ n { ( - a ) ^ n \\over 2 ^ { 2 n - 1 } } \\label { H 4 } 3 4 5 5'
expected = r'd s ^ { 2 } = ( 1  - { q c o s \\theta \\over  r } ) ^ { 2 \\over  1  + \\alpha ^ { 2 } } \\lbrace  d r ^ 2 + r ^ 2 d \\theta ^ 2 + r ^ 2 s i n ^ 2 \\theta  d \\varphi ^ 2 \\rbrace  - { d t ^ 2 \\over   ( 1  - { q c o s \\theta \\over  r } ) ^ { 2 \\over  1  + \\alpha ^ { 2 } } } \\ , . \\label { eq : sps 1 } \\widetilde \\gamma _ { \\rm  h o p f } \\simeq \\sum _ { n > 0 } \\widetilde { G } _ n { ( - a ) ^ n \\over 2 ^ { 2 n - 1 } } \\label { H 4 } 3 4 5 5'

res = re.sub(r"(?<!\s)(\s*(?:\\\\[a-zA-Z]*)|eq(?=:)|(?<=:)[a-zA-Z]+|(?:[^\s]))", r' \1', text).strip()
print(re.sub(r'\s+', r' ', res))
print(re.sub(r'\s+', r' ', expected))

输出:

d s ^ { 2 } = ( 1 - { q c o s \\theta \\over r } ) ^ { 2 \\over 1 + \\alpha ^ { 2 } } \\lbrace d r ^ 2 + r ^ 2 d \\theta ^ 2 + r ^ 2 s i n ^ 2 \\theta d \\varphi ^ 2 \\rbrace - { d t ^ 2 \\over ( 1 - { q c o s \\theta \\over r } ) ^ { 2 \\over 1 + \\alpha ^ { 2 } } } \\ , . \\label { eq : sps 1 } \\widetilde \\gamma _ { \\rm h o p f } \\simeq \\sum _ { n > 0 } \\widetilde { G } _ n { ( - a ) ^ n \\over 2 ^ { 2 n - 1 } } \\label { H 4 } 3 4 5 5
d s ^ { 2 } = ( 1 - { q c o s \\theta \\over r } ) ^ { 2 \\over 1 + \\alpha ^ { 2 } } \\lbrace d r ^ 2 + r ^ 2 d \\theta ^ 2 + r ^ 2 s i n ^ 2 \\theta d \\varphi ^ 2 \\rbrace - { d t ^ 2 \\over ( 1 - { q c o s \\theta \\over r } ) ^ { 2 \\over 1 + \\alpha ^ { 2 } } } \\ , . \\label { eq : sps 1 } \\widetilde \\gamma _ { \\rm h o p f } \\simeq \\sum _ { n > 0 } \\widetilde { G } _ n { ( - a ) ^ n \\over 2 ^ { 2 n - 1 } } \\label { H 4 } 3 4 5 5

【讨论】:

  • 否 实际上 sin 变为 s i n 因为可以有 sinxsiny..etc 等等,我的词汇会爆炸。但我很抱歉间距,因为我当时只在此处手动更改。是的,除了sin 等之外,我试图实现的目标是正确的
  • 另外,我还错过了一件事。我想删除final \text { whatever here } result 中的任何内容,包括\text。所以它变成final result。这里可以使用什么?一个独立的例子。
  • 我没有回答你的第二个问题,因为我看到你已经在这里发布了它:stackoverflow.com/q/67640363/12693728(请一次问一个问题,不要完全混合或更改它们)。我认为那里提供的解决方案应该可以解决您的问题,否则请在另一个线程中详细说明您的问题。
猜你喜欢
  • 2021-01-31
  • 2015-10-17
  • 1970-01-01
  • 1970-01-01
  • 2011-12-31
  • 1970-01-01
  • 1970-01-01
  • 2018-11-05
  • 2015-07-16
相关资源
最近更新 更多