-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathcleaner.py
58 lines (53 loc) · 2.14 KB
/
cleaner.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
from utils import (
create_database_connection,
check_column_exists,
encode_blob,
decode_blob
)
def is_potential_encoded_text(line, encoding_symbols='+=/$'):
line = line.strip()
if not line:
return False
if not all(c.isupper() or not c.isalpha() for c in line):
return False
if not any(symbol in line for symbol in encoding_symbols):
return False
return True
def clean_text(text: str) -> str:
cleaned_lines = []
previous_line_empty = False
for line in text.split('\n'):
if not is_potential_encoded_text(line):
current_line_empty = len(line.strip()) == 0
if not current_line_empty or not previous_line_empty:
cleaned_lines.append(line)
previous_line_empty = current_line_empty
return '\n'.join(cleaned_lines)
def clean_and_store_filing(accession_number: str) -> bool:
connection = create_database_connection()
if not connection:
return False
try:
if not check_column_exists(connection, 'cleaned_text', 'sec_filings'):
with connection.cursor() as cursor:
cursor.execute("ALTER TABLE sec_filings ADD COLUMN cleaned_text LONGBLOB")
with connection.cursor() as cursor:
cursor.execute("SELECT parsed_text FROM sec_filings WHERE accession_number = %s", (accession_number,))
result = cursor.fetchone()
if not result:
print(f"No parsed content found for accession number: {accession_number}")
return False
parsed_content_blob = result[0]
parsed_content = decode_blob(parsed_content_blob)
cleaned_content = clean_text(parsed_content)
cleaned_content_blob = encode_blob(cleaned_content)
with connection.cursor() as cursor:
cursor.execute("UPDATE sec_filings SET cleaned_text = %s WHERE accession_number = %s",
(cleaned_content_blob, accession_number))
connection.commit()
return True
except Exception as e:
print(f"Error cleaning and storing filing: {e}")
return False
finally:
connection.close()