-
Notifications
You must be signed in to change notification settings - Fork 0
/
normalized.py
79 lines (55 loc) · 2.13 KB
/
normalized.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
import re
import sys
from snippets import *
def main():
#input from command line
if len(sys.argv) != 3:
print('Usage: programname.py inputfile outputfile')
sys.exit()
inputfile = sys.argv[1]
outputfile = sys.argv[2]
file = open(inputfile, 'r')
content = file.read()
content = replace_normalized(content)
#writes output
file = open(outputfile, 'w')
file.write(content)
file.close()
def replace_normalized(content):
normalized_file = open('normalized_names', 'r').read()
global names
names = {}
name_pat = re.compile(r'section{(.*?)}\s(.+)')
lines = normalized_file.split('\n')
for line in lines:
if len(name_pat.findall(line)) > 0:
list = name_pat.findall(line)[0]
names[list[0]] = (list[1]).strip()
KLS_pat = re.compile(r'section{([^\n]+)}\n(.+?){Symmetry}', re.DOTALL) #TODO: add recurrence relation
content = KLS_pat.sub(rpl_section, content)
section_pat = re.compile(r'(section{(.+?)}.+?{Recurrence relation})(.+?)(\\subsection)', re.DOTALL)
content = section_pat.sub(rpl_section, content)
section_pat = re.compile(r'(section{(.+?)}.+?{Orthogonality relation})(.+?)(\\subsection)', re.DOTALL)
content = section_pat.sub(rpl_section, content)
return content
def rpl_section(match):
section_n = match.group(2)
search_in = match.group(3)
matchstr = match.group(0)
full_rpl_pat = re.compile(r'where\n.+?:=(.+?)(?:=|\$)')
if len(full_rpl_pat.findall(search_in)) == 0 or section_n not in names:
return matchstr
macro_pat = names[section_n] # holds current pattern
global full_macro
full_macro = full_rpl_pat.findall(search_in)[0]
full_macro = full_macro.replace(r'@', r'@@')
normalized_pat = re.compile(macro_pat)
search_in = normalized_pat.sub(replace_macro, search_in)
print(search_in)
return match.group(1) + search_in + match.group(4)
def replace_macro(match):
before_m = full_macro[:full_macro.find('{') + 1]
after_m = full_macro[full_macro.find('}'):]
return before_m + match.group(2) + after_m
if __name__ == '__main__':
main()