import re import csv def extract_info(text): """Extracts information from a text string.…
- User
import re import csv
def extract_info(text): """Extracts information from a text string.
codeArgs: text: The text string to extract information from. Returns: A dictionary containing the extracted information: - Ref number - Date - To - From - Subject - Ref (concatenated list of all references found) """ info = { "Ref number": "", "Date": "", "To": "", "From": "", "Subject": "", "Ref": "", } # Extract information using regular expressions (case-insensitive) info["Ref number"] = re.search(r"Ref(?:.|:)?\s*(.*?)(?:\s|Date)", text, re.IGNORECASE).group(1).strip() if re.search(r"Ref(?:.|:)?\s*(.*?)(?:\s|Date)", text, re.IGNORECASE) else "" info["Date"] = re.search(r"Date(?:.|:)?\s*(.*?)(?:\s|To)", text, re.IGNORECASE).group(1).strip() if re.search(r"Date(?:.|:)?\s*(.*?)(?:\s|To)", text, re.IGNORECASE) else "" info["To"] = re.search(r"To(?:.|:)?\s*(.*?)(?:\s|Kind Attention)", text, re.IGNORECASE).group(1).strip() if re.search(r"To(?:.|:)?\s*(.*?)(?:\s|Kind Attention)", text, re.IGNORECASE) else "" info["From"] = re.search(r"From(?:.|:)?\s*(.*?)(?:\s|Subject)", text, re.IGNORECASE).group(1).strip() if re.search(r"From(?:.|:)?\s*(.*?)(?:\s|Subject)", text, re.IGNORECASE) else "" info["Subject"] = re.search(r"Subject(?:.|:)?\s*(.*?)(?:\s|Ref)", text, re.IGNORECASE).group(1).strip() if re.search(r"Subject(?:.|:)?\s*(.*?)(?:\s|Ref)", text, re.IGNORECASE) else "" info["Ref"] = "; ".join([r.strip() for r in re.findall(r"Ref(?:.|:)?\s*(.*?)(?:\s|Date|To|From|Subject)", text, re.IGNORECASE)]) if re.findall(r"Ref(?:.|:)?\s*(.*?)(?:\s|Date|To|From|Subject)", text, re.IGNORECASE) else "" return info - Assistant
- User
import pandas as pd import re
def extract_info(text): """ Extracts To, From, Subject, Date, Ref No., and Ref from the given text.
codeArgs: text: The text to extract information from. Returns: A dictionary containing the extracted information. """ info = {} # Extract To to_match = re.search(r"To:\s*(.+)", text) if to_match: info["To"] = to_match.group(1).strip() # Extract From from_match = re.search(r"From:\s*(.+)", text) if from_match: info["From"] = from_match.group(1).strip() # Extract Subject subject_match = re.search(r"Subject:\s*(.+)", text) if subject_match: info["Subject"] = subject_match.group(1).strip() # Extract Date date_match = re.search(r"Date:\s*(\d{2}.\d{2}.\d{4})", text) if date_match: info["Date"] = date_match.group(1) # Extract Ref No. ref_no_match = re.search(r"No.:\s*(.+)", text) if ref_no_match: info["Ref No."] = ref_no_match.group(1).strip() # Extract Ref ref_match = re.search(r"Ref:\s*(.+)", text, flags=re.DOTALL) if ref_match: info["Ref"] = ref_match.group(1).strip() return info - Assistant