-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript2.py
More file actions
113 lines (85 loc) · 3.42 KB
/
script2.py
File metadata and controls
113 lines (85 loc) · 3.42 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
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
"""
File: read_█.py
Author: Sebastian Le
Description: Generic script to extract structured information from text files
"""
import re
import os
import sys
import csv
# Set of valid acronyms used in label tags
valid = {
# Enter valid acronyms here
}
def extract_base_and_revision(filename: str):
name_part = filename.rsplit('█', █)[█]
match = re.match(█, name_part)
if match:
return match.group(█), match.group(█)
else:
return name_part, ''
def find_acronym_tag_pairs_█(txt_file_path: str):
number_pattern = re.compile(█)
acronym_pattern = re.compile(█)
inline_pattern = re.compile(█)
tag_pairs = set()
used_indices = set()
with open(txt_file_path, 'r', encoding='utf-8') as file:
lines = [line.strip() for line in file if line.strip()]
for i in range(len(lines)):
if i in used_indices:
continue
# Case 1: █
if i < len(lines) - 1 and (█) not in used_indices:
line1, line2 = lines[█], lines[█]
if number_pattern.match(line1) and acronym_pattern.match(line2) and line2 in valid:
tag_pairs.add(f"{line2} {line1}")
used_indices.update({█, █})
continue
elif acronym_pattern.match(line1) and number_pattern.match(line2) and line1 in valid:
tag_pairs.add(f"{line1} {line2}")
used_indices.update({█, █})
continue
# Case 2: █
line = lines[i]
inline_match = inline_pattern.match(line)
if inline_match:
code_a, code_b = inline_match.groups()
if code_a in valid:
tag_pairs.add(f"{code_a} {code_b}")
used_indices.add(i)
if i > 0 and number_pattern.match(lines[█]):
tag_pairs.add(f"{code_a} {lines[█]}")
used_indices.add(i-1)
if i + 1 < len(lines) and acronym_pattern.match(lines[█]) and lines[█] in valid:
tag_pairs.add(f"{lines[█]} {code_b}")
used_indices.add(█)
return tag_pairs
def process_folder(folder_path):
output_rows = [("█","█", "█","█")]
for filename in sorted(os.listdir(folder_path)):
if filename.lower().endswith('.txt'):
file_path = os.path.join(folder_path, filename)
base_name, revision = extract_base_and_revision(filename)
tags = find_acronym_tag_pairs_█(file_path)
for idx, tag in enumerate(sorted(tags), 1):
output_rows.append(( idx, base_name, revision, tag))
with open("output_results.csv", "w", newline='', encoding='utf-8') as f:
writer = csv.writer(f)
writer.writerows(output_rows)
if __name__ == "__main__":
if len(sys.argv) < 2:
print("Usage: python process_tag_folder.py <folder_path>")
sys.exit(1)
folder = os.path.normpath(sys.argv[1].strip('"'))
if not os.path.isdir(folder):
print("Not a valid folder path.")
sys.exit(1)
process_folder(folder)
print("datas successfully extracted to 'output_results.csv'!")
print(f"Done! Opening now...")
if os.name == 'nt':
os.startfile('output_results.csv')
elif os.name == 'posix':
subprocess.call(('xdg-open', 'output_results.csv'))
sys.exit(0)