-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathutils.py
More file actions
49 lines (40 loc) · 1.21 KB
/
utils.py
File metadata and controls
49 lines (40 loc) · 1.21 KB
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
import re
import pypdfium2 as pdfium
# This function reads the file data/FS-Rules_2024_v1.1.0.txt and extracts the titles and texts of the rules.
# For example, the file contains the following text:
# """
# AIP
#
# Anti Intrusion Plate
# """
#
# or in this format:
# """
# A 1.2.6
#
# Vehicles of both classes can take part in an additional Driverless Cup (DC).
# """
#
# arguments:
# file_name: str: the file name of the rules in .pdf format
def extract_rules_from_pdf(file_name='data/Rules_2024_v1.1.pdf'):
pdf = pdfium.PdfDocument(file_name)
full_text = ""
for page in pdf:
textpage = page.get_textpage()
full_text += textpage.get_text_range()
# print(full_text)
# Split the text into lines
lines = full_text.split("\n")
# Initialize variables
pattern = re.compile(r'^[A-Z]+\d+\.\d+\.\d+')
current_rule = None
rules_splitted = []
# Iterate through the lines and group them based on the rule identifiers
for line in lines:
if pattern.match(line):
current_rule = line.strip()
rules_splitted.append(current_rule)
elif current_rule:
rules_splitted[-1] += "\n" + line.strip()
return rules_splitted