戳戳猫的小窝
更新日志
关于
在项目中创建python文件“滑动分割.py”: ```python def fixed_chunk(text, chunk_size=128, overlap=0): """ 固定长度分块函数(支持重叠) 参数: text: 要分块的原始文本 chunk_size: 每个块的长度(默认128) overlap: 块之间的重叠长度(默认0) 返回: 分块后的文本列表 """ # 检查参数有效性 if overlap >= chunk_size: print("重叠长度必须小于块大小") return chunks = [] # 存储分块结果 current_pos = 0 # 当前处理位置 # 通过循环逐步截取文本块 while current_pos < len(text): # 计算当前块的结束位置 end_pos = current_pos + chunk_size # 截取当前文本块(切片自动处理越界情况) current_chunk = text[current_pos:end_pos] # 将分块添加到结果列表 chunks.append(current_chunk) # 移动到下一个块的起始位置(减去重叠长度) current_pos += chunk_size - overlap return chunks with open("背影.txt", 'r', encoding='utf-8') as f: test_text = f.read() texts = fixed_chunk(test_text, chunk_size=64, overlap=24) for text in texts: print(text) print("\n") ```
滑动分割