-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathextract.py
166 lines (120 loc) · 4.84 KB
/
extract.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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
import pdftotext
import re
abbrPage = 6
startPage = 8
def checkLine(line, multiLine):
# Check line of a rule without a 'heading'
outLine = ""
linkStart = ""
# Link to section
line = re.sub(r'(A|T|[CED]V|IN|S|D) +(\d+\.?)+(\d+)', ":ref:`\g<0>`", line)
line = line.lstrip().split()
for word in line:
alteredLine = False
# Bullet point
if word == '•':
outLine += '\n\n-'
alteredLine = True
continue
# Abbreviations
for key in abbrs:
keySearch = re.match(key, word)
if keySearch:
if len(outLine) > 0 or multiLine:
outLine += ' '
outLine += ':abbr:`' + word + ' (' + abbrs[keySearch.group()] + ')`'
alteredLine = True
break
if not alteredLine:
#or (line.index(word) == 0 and len(outLine) > 0)
if len(outLine) > 0 or multiLine:
outLine += ' '
outLine += word
return outLine
# Load PDF
with open("rules.pdf", "rb") as f:
abbrText = ""
pdfText = ""
currentLevelOne = ""
ruleText = ""
ruleGroup = ""
headingPrintRule = False
pdf = pdftotext.PDF(f)
for i in range(abbrPage - 1, startPage - 1):
abbrText += pdf[i]
for i in range(startPage - 1, len(pdf)):
# print(pdf[i].index("\n"))
pdfText += pdf[i][pdf[i].index("\n"):]
# Abbreviations parsing
abbrs = {}
for line in abbrText.splitlines():
footerMatch = re.match(r'Formula Student Rules 2020 +'
'Version: (\d+\.?)* +'
'\d+ of \d+', line)
if footerMatch:
continue
headerMatch = re.match(r'A BBREVIATIONS', line)
if headerMatch:
continue
abbrMatch = re.findall(r'([A-Z0-9]+\s+([\w-]+[ ]?)+\b)', line)
if abbrMatch:
for abbr in abbrMatch:
abbr = abbr[0]
abbrSplit = re.split('\s{2,}', abbr)
abbrs[abbrSplit[0]] = abbrSplit[1]
# Main text parsing
for line in pdfText.splitlines():
footerMatch = re.match(r'Formula Student Rules 2020 +'
'Version: (\d+\.?)* +'
'\d+ of \d+', line)
if footerMatch:
continue
line = re.sub(r'\s{1}\]', "]", line)
headingUnderlines = ['=', '-', '^']
# Match heading
mo = re.match(r'^(A|T|[CED]V|IN|S|D) *(\d+\.?)*', line)
if mo:
headingLevel = len(mo.group().split('.'))
# Level 1 heading
h1Match = re.match(r'^(A|T|[CED]V|IN|S|D)\d{1,2} +[A-Z-()&[\] ]*', line)
if h1Match:
if not headingPrintRule:
print(ruleText + "\n") # Print rule text
headingPrintRule = True
headingText = re.sub(r'\s{2,}', " ", h1Match.group())
headingText = re.sub(r'(\b.) ', '\g<1>', headingText)
headingText = headingText[mo.end():].strip()
heading = mo.group() + ' - ' + headingText;
# Add 3 for dash
underlineLength = len(headingText) + len(mo.group()) + 3
underlines = headingUnderlines[headingLevel-1]*underlineLength + "\n"
print(heading)
print(underlines)
if headingLevel == 2:
if not headingPrintRule:
print(ruleText + "\n") # Print rule text
headingPrintRule = True
ruleText = ""
headingText = line[mo.end():].strip()
headingText = re.sub(r'\s{2,}', " ", headingText)
heading = mo.group() + ' - ' + headingText;
# Add 3 for dash
underlineLength = len(headingText) + len(mo.group()) + 3
underlines = headingUnderlines[headingLevel-1]*underlineLength + "\n"
print(heading)
print(underlines)
# If we have a rule
if headingLevel == 3:
if not headingPrintRule:
print(ruleText + "\n") # Print prev rule text
ruleGroup = mo.group()
ruleText = checkLine(line[mo.end():].strip(), False)
# Add 3 for dash
underlineLength = len(ruleGroup)
underlines = headingUnderlines[headingLevel-1]*underlineLength + "\n"
print(ruleGroup)
print(underlines)
headingPrintRule = False
else:
ruleText += checkLine(line, True)
print(ruleText + "\n") # Print final rule