2025-04-30 06:14:41 +00:00
|
|
|
|
# coding=utf-8
|
|
|
|
|
|
"""
|
|
|
|
|
|
@project: maxkb
|
|
|
|
|
|
@Author:虎
|
|
|
|
|
|
@file: text_split_handle.py
|
|
|
|
|
|
@date:2024/3/27 18:19
|
|
|
|
|
|
@desc:
|
|
|
|
|
|
"""
|
|
|
|
|
|
import re
|
|
|
|
|
|
import traceback
|
|
|
|
|
|
from typing import List
|
|
|
|
|
|
|
|
|
|
|
|
from charset_normalizer import detect
|
|
|
|
|
|
|
|
|
|
|
|
from common.handle.base_split_handle import BaseSplitHandle
|
2025-06-30 04:40:11 +00:00
|
|
|
|
from common.utils.logger import maxkb_logger
|
2025-04-30 06:14:41 +00:00
|
|
|
|
from common.utils.split_model import SplitModel
|
|
|
|
|
|
|
2025-06-26 08:23:32 +00:00
|
|
|
|
default_pattern_list = [
|
|
|
|
|
|
re.compile('(?<=^)# (?!-\\*- coding:).*|(?<=\\n)# (?!-\\*- coding:).*'),
|
|
|
|
|
|
re.compile('(?<=\\n)(?<!#)## (?!#).*|(?<=^)(?<!#)## (?!#).*'),
|
|
|
|
|
|
re.compile("(?<=\\n)(?<!#)### (?!#).*|(?<=^)(?<!#)### (?!#).*"),
|
|
|
|
|
|
re.compile("(?<=\\n)(?<!#)#### (?!#).*|(?<=^)(?<!#)#### (?!#).*"),
|
|
|
|
|
|
re.compile("(?<=\\n)(?<!#)##### (?!#).*|(?<=^)(?<!#)##### (?!#).*"),
|
|
|
|
|
|
re.compile("(?<=\\n)(?<!#)###### (?!#).*|(?<=^)(?<!#)###### (?!#).*")
|
|
|
|
|
|
]
|
2025-04-30 06:14:41 +00:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
class TextSplitHandle(BaseSplitHandle):
|
|
|
|
|
|
def support(self, file, get_buffer):
|
|
|
|
|
|
buffer = get_buffer(file)
|
|
|
|
|
|
file_name: str = file.name.lower()
|
|
|
|
|
|
if file_name.endswith(".md") or file_name.endswith('.txt') or file_name.endswith('.TXT') or file_name.endswith(
|
|
|
|
|
|
'.MD'):
|
|
|
|
|
|
return True
|
|
|
|
|
|
result = detect(buffer)
|
|
|
|
|
|
if result['encoding'] is not None and result['confidence'] is not None and result['encoding'] != 'ascii' and \
|
|
|
|
|
|
result['confidence'] > 0.5:
|
|
|
|
|
|
return True
|
|
|
|
|
|
return False
|
|
|
|
|
|
|
|
|
|
|
|
def handle(self, file, pattern_list: List, with_filter: bool, limit: int, get_buffer, save_image):
|
|
|
|
|
|
buffer = get_buffer(file)
|
2025-06-30 04:49:50 +00:00
|
|
|
|
if type(limit) is str:
|
|
|
|
|
|
limit = int(limit)
|
2025-07-10 07:06:10 +00:00
|
|
|
|
if type(with_filter) is str:
|
|
|
|
|
|
with_filter = with_filter.lower() == 'true'
|
2025-04-30 06:14:41 +00:00
|
|
|
|
if pattern_list is not None and len(pattern_list) > 0:
|
|
|
|
|
|
split_model = SplitModel(pattern_list, with_filter, limit)
|
|
|
|
|
|
else:
|
|
|
|
|
|
split_model = SplitModel(default_pattern_list, with_filter=with_filter, limit=limit)
|
|
|
|
|
|
try:
|
|
|
|
|
|
content = buffer.decode(detect(buffer)['encoding'])
|
|
|
|
|
|
except BaseException as e:
|
2025-06-30 04:40:11 +00:00
|
|
|
|
maxkb_logger.error(f"Error processing TEXT file {file.name}: {e}, {traceback.format_exc()}")
|
2025-06-26 08:23:32 +00:00
|
|
|
|
return {'name': file.name, 'content': []}
|
|
|
|
|
|
return {'name': file.name, 'content': split_model.parse(content)}
|
2025-04-30 06:14:41 +00:00
|
|
|
|
|
|
|
|
|
|
def get_content(self, file, save_image):
|
|
|
|
|
|
buffer = file.read()
|
|
|
|
|
|
try:
|
2025-04-30 08:06:30 +00:00
|
|
|
|
return buffer.decode(detect(buffer)['encoding'])
|
2025-04-30 06:14:41 +00:00
|
|
|
|
except BaseException as e:
|
|
|
|
|
|
traceback.print_exception(e)
|
2025-04-30 08:06:30 +00:00
|
|
|
|
return f'{e}'
|