-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfintel_process_text.py
More file actions
50 lines (41 loc) · 1.75 KB
/
fintel_process_text.py
File metadata and controls
50 lines (41 loc) · 1.75 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
#####################################
# Import Modules
#####################################
# Import from Python Standard Library
import pathlib
# Import from local project modules
from utils_logger import logger
#####################################
# Declare Global Variables
#####################################
fetched_folder_name: str = "iolp_buildings.txt"
processed_folder_name: str = "iolp_buildings_txt.processed"
#####################################
# Define Functions
#####################################
def count_word_occurrences(file_path: pathlib.Path, word: str) -> int:
"""Count the occurrences of a specific word in a text file (case-insensitive)."""
try:
with file_path.open('r') as file:
content: str = file.read()
return content.lower().count(word.lower())
except Exception as e:
logger.error(f"Error reading text file: {e}")
return 0
def process_text_file():
"""Read a text file, count occurrences of 'active', and save the result."""
input_file = pathlib.Path(fetched_folder_name, "iolp_buildings.txt")
output_file = pathlib.Path(processed_folder_name, "iolp_buildings_txt.processed")
word_to_count: str = "active"
word_count: int = count_word_occurrences(input_file, word_to_count)
output_file.parent.mkdir(parents=True, exist_ok=True)
with output_file.open('w') as file:
file.write(f"Occurrences of '{word_to_count}': {word_count}\n")
logger.info(f"Processed text file: {input_file}, Word count saved to: {output_file}")
#####################################
# Main Execution
#####################################
if __name__ == "__main__":
logger.info("Starting text processing...")
process_text_file()
logger.info("Text processing complete.")