File size: 1,825 Bytes
ea3d866 | 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
def process_proverbs(input_path, output_path):
try:
# Attempt to open the input file and read lines
with open(input_path, 'r', encoding='utf-8') as file:
lines = file.readlines()
print("Successfully read the input file.")
except FileNotFoundError:
print(f"File not found: {input_path}")
return
proverbs = []
current_proverb = ""
for line in lines:
# Clean the line by stripping leading/trailing whitespace
line = line.strip()
print(f"Processing line: {line}") # Debug output for each line
# Identify proverbs or meanings based on specific patterns, e.g., numbers at the start
if re.match(r'^\d+', line): # This line has a number, possibly a new entry
if current_proverb:
# Save the accumulated proverb/meaning pair
proverbs.append(current_proverb.strip())
current_proverb = line
else:
# Continue accumulating lines for the current entry
current_proverb += " " + line
# Save any remaining proverb
if current_proverb:
proverbs.append(current_proverb.strip())
# Write formatted proverbs to the output file
try:
with open(output_path, 'w', encoding='utf-8') as f:
for entry in proverbs:
f.write(entry + '\n')
print(f"Writing line to file: {entry}") # Debug print for each line written
print(f"Data successfully saved to {output_path}")
except Exception as e:
print(f"Error writing to file: {e}")
# Example usage
input_path = "iraqi_proverbs.txt" # Replace with the path to your text file
output_path = "data/raw_data/iraqi_data.txt" # Adjust this path if necessary
process_proverbs(input_path, output_path)
|