代码功能:将文本分句。
import re
cutLineFlag = ["?", "!", "。"]
sentenceList = []
with open("F:\\shiyan\\4.txt", "r", encoding="UTF-8") as file:
for line in file:
#words = re.sub("\?", "", line.strip())
#words = re.sub("\[*\]", "", words)
words = line.strip()
oneSentence = ""
for word in words:
if word not in cutLineFlag:
oneSentence = oneSentence + word
else:
oneSentence = oneSentence + word
if oneSentence.__len__() > 4:
sentenceList.append(oneSentence.strip() + "\r")
oneSentence = ""
with open("F:\\gannanju.txt", "w", encoding="UTF-8") as resultFile:
print(sentenceList.__len__())
resultFile.writelines(sentenceList)