database_tools新增一些和括号配对, 数学环境鉴别有关的共鞥你
This commit is contained in:
parent
017bd63a36
commit
85556de57e
|
|
@ -1912,5 +1912,51 @@ def CheckUsagesValidity(data): #检查使用记录数据是否合理(没有超
|
||||||
return 1
|
return 1
|
||||||
return 0
|
return 0
|
||||||
|
|
||||||
|
def pairingbraces(string_raw,leftbracepos): #根据字符串中的左括号的位置寻找配对的右括号
|
||||||
|
types = {"(":"()","[":"[]","{":"{}"}
|
||||||
|
braces = "".join([types[i] for i in types])
|
||||||
|
string = string_raw
|
||||||
|
for i in braces:
|
||||||
|
string = string.replace("\\"+i," ") #去除LaTeX的括号
|
||||||
|
if not string[leftbracepos] in ("(","[","{"):
|
||||||
|
print("位置上的字符不是左括号")
|
||||||
|
return -1 #位置上不是括号, 返回-1
|
||||||
|
else:
|
||||||
|
braces = types[string[leftbracepos]]
|
||||||
|
lbrace = braces[0]
|
||||||
|
rbrace = braces[1]
|
||||||
|
count = 1
|
||||||
|
currentpos = leftbracepos + 1
|
||||||
|
while not currentpos == len(string):
|
||||||
|
if string[currentpos] == lbrace:
|
||||||
|
count += 1
|
||||||
|
elif string[currentpos] == rbrace:
|
||||||
|
count -= 1
|
||||||
|
if count == 0:
|
||||||
|
return currentpos # 返回配对的右括号位置
|
||||||
|
currentpos += 1
|
||||||
|
return -1 #未找到配对括号
|
||||||
|
|
||||||
|
def InMathEnv(string,pos): #判断string的pos位置上的字符是否在math环境中
|
||||||
|
string = string.repalce("\$"," ")
|
||||||
|
if not string[pos] == "$" and string[:pos].count("$") % 2 == 1:
|
||||||
|
return True
|
||||||
|
else:
|
||||||
|
return False
|
||||||
|
|
||||||
|
|
||||||
|
def MaskingMathEnv(string_raw): #把string_raw中数学环境中的符号都转为$后以新字符串的形式返回
|
||||||
|
string_raw = string_raw.replace(r"\$"," ")
|
||||||
|
countdollars = 0
|
||||||
|
string = ""
|
||||||
|
for i in range(len(string_raw)):
|
||||||
|
if string_raw[i] == "$":
|
||||||
|
countdollars = 1 - countdollars
|
||||||
|
if countdollars == 0:
|
||||||
|
string += string_raw[i]
|
||||||
|
else:
|
||||||
|
string += "$"
|
||||||
|
return string
|
||||||
|
|
||||||
if __name__ == "__main__":
|
if __name__ == "__main__":
|
||||||
print("数据库工具, import用.")
|
print("数据库工具, import用.")
|
||||||
Reference in New Issue