1.24 后处理提取带星要求添加了脚本提取的逻辑

This commit is contained in:
zy123 2025-01-24 14:51:46 +08:00
parent 29c17c375d
commit 9117c4117b

View File

@ -272,9 +272,14 @@ def extract_text_with_keywords(processed_paragraphs, keywords, follow_up_keyword
found_next_number = False
current_section_pattern = None
try:
while current_index < len(processed_paragraphs) - 1:
current_index += 1
try:
next_text = processed_paragraphs[current_index].strip()
except IndexError:
print(f"IndexError: current_index={current_index} is out of range.")
break # Exit the loop if index is out of range
if not next_text:
continue # Skip empty lines
@ -306,23 +311,44 @@ def extract_text_with_keywords(processed_paragraphs, keywords, follow_up_keyword
continue_collecting = False
active_key = None
break
except Exception as e:
print(f"An unexpected error occurred in process_matching_section: {e}")
# Optionally, you can re-raise the exception or handle it as needed
# raise e
if continue_collecting:
if text == '[$$table_start$$]':
try:
current_index += 1
while processed_paragraphs[current_index] != '[$$table_over$$]':
extracted_paragraphs[active_key].append(processed_paragraphs[current_index])
current_index += 1
except IndexError:
print(f"IndexError: current_index={current_index} during table processing.")
except Exception as e:
print(f"An unexpected error occurred while processing table: {e}")
return current_index
if current_section_pattern and re.match(current_section_pattern, text):
if current_section_pattern:
try:
if re.match(current_section_pattern, text):
continue_collecting = False
active_key = None
except re.error as re_err:
print(f"Regex error: {re_err} with pattern {current_section_pattern} and text '{text}'")
except Exception as e:
print(f"An unexpected error occurred while matching section pattern: {e}")
else:
if active_key is not None:
try:
extracted_paragraphs[active_key].append(text)
except KeyError:
print(f"KeyError: active_key='{active_key}' not found in extracted_paragraphs.")
except Exception as e:
print(f"An unexpected error occurred while appending text: {e}")
return current_index
try:
if match_keywords(text, keywords):
active_key = text
extracted_paragraphs[active_key] = [text]
@ -332,7 +358,7 @@ def extract_text_with_keywords(processed_paragraphs, keywords, follow_up_keyword
process_matching_section()
elif match_keywords(text, follow_up_keywords):
continue_collecting = True
section_number = re.match(r'^(\d+([.]\d+)*)\s*[.]?', text)
section_number = re.match(r'^(\d+([.]\d+)*)\s*[.]?', text)
if section_number:
current_section_number = section_number.group(1)
@ -340,28 +366,39 @@ def extract_text_with_keywords(processed_paragraphs, keywords, follow_up_keyword
parts = current_section_number.split('.')
matched_patterns = []
if len(parts) > 1:
for i in range(1, 6):
parent_section_parts = parts[:-1].copy()
try:
parent_section_parts[-1] = str(int(parent_section_parts[-1]) + i)
parent_pattern = r'^' + r'\s*[.]\s*'.join(parent_section_parts) + r'(?!\s*[.]\s*\d+)'
except ValueError:
print(f"ValueError: Unable to convert '{parent_section_parts[-1]}' to int.")
continue
parent_pattern = r'^' + r'\s*[..、]\s*'.join(parent_section_parts) + r'(?!\s*[..、]\s*\d+)'
matched_patterns.append(parent_pattern)
digit_comma_pattern = r'^\d+\s*、'
matched_patterns.append(digit_comma_pattern)
try:
current_top_level_num = int(current_section_number.split('.')[0])
for i in range(1, 6):
next_top_level_num = current_top_level_num + i
next_top_level_pattern = r'^' + str(next_top_level_num) + r'\s*[.]'
next_top_level_pattern = r'^' + str(next_top_level_num) + r'\s*[.]'
if next_top_level_pattern not in matched_patterns:
matched_patterns.append(next_top_level_pattern)
except ValueError:
print(f"ValueError: Unable to convert '{current_section_number.split('.')[0]}' to int.")
combined_pattern = r'(' + r')|('.join(matched_patterns) + r')'
try:
current_section_pattern = re.compile(combined_pattern)
except re.error as re_err:
print(f"Regex compilation error: {re_err} with pattern '{combined_pattern}'")
else:
process_matching_section() # Reusing the common function for matching sections
except Exception as e:
print(f"An unexpected error occurred in extract_from_text: {e} 当前index:{current_index}")
return current_index
index = 0
@ -676,7 +713,7 @@ if __name__ == '__main__':
output_dir = r"C:\Users\Administrator\Desktop\货物标\zbfiles\output6"
# invalid_added = insert_mark(pdf_path)
# invalid_added_docx = pdf2docx(invalid_added)
invalid_added_docx=r'C:\Users\Administrator\Desktop\货物标\zbfiles\output6\invalid_added.docx'
invalid_added_docx=r'C:\Users\Administrator\Desktop\fsdownload\a3b259fd-5ade-4c66-9117-eb44796682ad\invalid_added.docx'
results = combine_find_invalid(invalid_added_docx, output_dir)
end_time = time.time()
print("Results:", json.dumps(results, ensure_ascii=False, indent=4))