Python文本数据切分及HTML数据处理

要实现这个功能,你可以使用Python的字符串处理方法,结合正则表达式来找到合适的切分点。下面是一个简单的实现示例:
import re

def split_text(text, max_length=50):
    # 使用正则表达式匹配句子结束符"。!"等,并尝试在此处分割
    sentences = re.split('(?<=[。!])', text)
    result = []
    current_sentence = ""
    
    for sentence in sentences:
        # 如果加上新的句子长度不超过限制,则直接添加
        if len(current_sentence) + len(sentence) <= max_length:
            current_sentence += sentence
        else:
            # 如果超过了长度限制,则将当前句子加入结果列表,并开始新句子
            result.append(current_sentence.strip())
            current_sentence = sentence
    
    # 添加最后一个

你可能感兴趣的:(NLP与推荐算法,python,开发语言,文本,分割,NLP,embedding)