001/*
002 *  Copyright (c) 2023-2026, Agents-Flex (fuhai999@gmail.com).
003 *  <p>
004 *  Licensed under the Apache License, Version 2.0 (the "License");
005 *  you may not use this file except in compliance with the License.
006 *  You may obtain a copy of the License at
007 *  <p>
008 *  http://www.apache.org/licenses/LICENSE-2.0
009 *  <p>
010 *  Unless required by applicable law or agreed to in writing, software
011 *  distributed under the License is distributed on an "AS IS" BASIS,
012 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013 *  See the License for the specific language governing permissions and
014 *  limitations under the License.
015 */
016package com.agentsflex.core.document.splitter;
017
018import com.agentsflex.core.document.DocumentSplitter;
019import com.agentsflex.core.document.Document;
020import com.agentsflex.core.document.id.DocumentIdGenerator;
021import com.agentsflex.core.util.StringUtil;
022
023import java.util.ArrayList;
024import java.util.Collections;
025import java.util.List;
026
027public class RegexDocumentSplitter implements DocumentSplitter {
028
029    private final String regex;
030
031    public RegexDocumentSplitter(String regex) {
032        this.regex = regex;
033    }
034
035    @Override
036    public List<Document> split(Document document, DocumentIdGenerator idGenerator) {
037        if (document == null || StringUtil.noText(document.getContent())) {
038            return Collections.emptyList();
039        }
040        String[] textArray = document.getContent().split(regex);
041        List<Document> chunks = new ArrayList<>(textArray.length);
042        for (String textString : textArray) {
043            if (StringUtil.noText(textString)) {
044                continue;
045            }
046            Document newDocument = new Document();
047            newDocument.addMetadata(document.getMetadataMap());
048            newDocument.setContent(textString);
049
050            //we should invoke setId after setContent
051            newDocument.setId(idGenerator == null ? null : idGenerator.generateId(newDocument));
052            chunks.add(newDocument);
053        }
054        return chunks;
055    }
056}