智能分段,分段规则合并拆分

import java.util.*;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class FileContentParser {

    private static final int GLASS_SIZE_MAX = 90;

    public static List<String> parseFileContent(String fileKo) {
        List<String> contents = new ArrayList<>();

        // Step 1: Split by \r\n but keep the delimiter
        List<String> lines = splitWithDelimiters(fileKo, "(?<=[\\r\\n])");

        // Step 2: Clean and filter empty lines
        List<String> cleanedLines = new ArrayList<>();
        for (String line : lines) {
            String cleaned = line.replaceAll("[\\r\\n]", "");
            if (!cleaned.isEmpty()) {
                cleanedLines.add(cleaned);
            }
        }

        // Step 3: Merge short lines into paragraphs (<90 chars)
        List<String> mergedParagraphs = new ArrayList<>();
        for (String line : cleanedLines) {
            if (mergedParagraphs.isEmpty()) {
                mergedParagraphs.add(line);
            } else {
                String last = mergedParagraphs.get(mergedParagraphs.size() - 1);
                if (last.length() + line.length() < GLASS_SIZE_MAX) {
                    mergedParagraphs.set(mergedParagraphs.size() - 1, last + line);
                } else {
                    mergedParagraphs.add(line);
                }
            }
        }

        // Step 4: Split long strings into chunks of max 90 chars
        for (String paragraph : mergedParagraphs) {
            List<String> chunks = splitLongString(paragraph, GLASS_SIZE_MAX);
            contents.addAll(chunks);
        }

        return contents;
    }

    private static List<String> splitWithDelimiters(String input, String regex) {
        List<String> result = new ArrayList<>();
        Pattern pattern = Pattern.compile(regex);
        Matcher matcher = pattern.matcher(input);
        int start = 0;
        while (matcher.find()) {
            String match = input.substring(start, matcher.start());
            result.add(match + input.substring(matcher.start(), matcher.end()));
            start = matcher.end();
        }
        result.add(input.substring(start));
        return result;
    }

    private static List<String> splitLongString(String text, int maxLength) {
        List<String> result = new ArrayList<>();
        if (text.length() <= maxLength) {
            result.add(text);
            return result;
        }

        for (int i = 0; i < text.length(); i += maxLength) {
            result.add(text.substring(i, Math.min(i + maxLength, text.length())));
        }

        return result;
    }

    // 示例 main 方法测试
    public static void main(String[] args) {
        String fileKo = "你的完整文本内容";
        List<String> contents = parseFileContent(fileKo);
        for (String content : contents) {
            System.out.println(content);
        }
    }
}

============以下为dart代码===========

contents = file_ko
    .split(RegExp(r'(?<=[\r\n])'))
    .map((element) => element.replaceAll(RegExp(r'[\r\n]'), ''))
    .where((element) => element.isNotEmpty)
    .fold<List<String>>([], (List<String> list, String item) {
      if (list.isEmpty) {
        list.add(item);
      } else if (list.last.length + item.length < glassSizeMax) {
        list.last += item;
      } else {
        list.add(item);
      }
      return list;
    })
    .expand((element) => splitLongString(element, glassSizeMax))
    .toList();

List<String> splitLongString(String text, int maxLength) {
  List<String> result = [];
  if (text.length <= maxLength) {
    result.add(text);
    return result;
  }

  int start = 0;
  while (start < text.length) {
    int end = start + maxLength;
    if (end > text.length) {
      result.add(text.substring(start));
    } else {
      result.add(text.substring(start, end));
    }
    start = end;
  }
  return result;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值