这次,我让AI帮我生成一个可以将文本按照段落分割的代码,就学到了一个以前没用过的splitlines()函数。经过两轮的提示词,它就给我了比较满意的回复(本次尝试的相关视频,我已经在视频号「退役程序员」中发布)
def parse_text_to_paragraphs(file_name):
with open(file_name, 'r') as f:
text = f.read()
# Split the text into lines
lines = text.splitlines()
# Initialize an empty list to store the paragraphs
paragraphs = []
# Initialize an empty string to build the current paragraph
current_paragraph = ''
# Iterate over each line
for line in lines:
# If the line is not empty, add it to the current paragraph
if line.strip():
current_paragraph += line + ' '
# If the line is empty or a new chapter starts,
# add the current paragraph to the list and reset the current paragraph
elif current_paragraph.strip():
paragraphs.append(current_paragraph.strip())
current_paragraph = ''
# Add any remaining paragraph to the list
if current_paragraph.strip():
paragraphs.append(current_paragraph.strip())