|
| 1 | +/* |
| 2 | + * Copyright OpenSearch Contributors |
| 3 | + * SPDX-License-Identifier: Apache-2.0 |
| 4 | + */ |
| 5 | +package org.opensearch.neuralsearch.processor.chunker; |
| 6 | + |
| 7 | +import java.util.Locale; |
| 8 | +import java.util.Map; |
| 9 | +import java.util.List; |
| 10 | +import java.util.ArrayList; |
| 11 | + |
| 12 | +import static org.opensearch.neuralsearch.processor.chunker.ChunkerParameterParser.parseInteger; |
| 13 | +import static org.opensearch.neuralsearch.processor.chunker.ChunkerParameterParser.parseDoubleWithDefault; |
| 14 | +import static org.opensearch.neuralsearch.processor.chunker.ChunkerParameterParser.parsePositiveIntegerWithDefault; |
| 15 | + |
| 16 | +/** |
| 17 | + * The implementation {@link Chunker} for fixed character length algorithm. |
| 18 | + */ |
| 19 | +public final class FixedCharLengthChunker extends Chunker { |
| 20 | + |
| 21 | + /** The identifier for the fixed character length chunking algorithm. */ |
| 22 | + public static final String ALGORITHM_NAME = "fixed_char_length"; |
| 23 | + |
| 24 | + /** Field name for specifying the maximum number of characters per chunk. */ |
| 25 | + public static final String CHAR_LIMIT_FIELD = "char_limit"; |
| 26 | + |
| 27 | + /** Field name for specifying the overlap rate between consecutive chunks based on fixed character length. */ |
| 28 | + public static final String OVERLAP_RATE_FIELD = "overlap_rate"; |
| 29 | + |
| 30 | + // Default values for each non-runtime parameter |
| 31 | + private static final int DEFAULT_CHAR_LIMIT = 2048; // Default character limit per chunk (512 tokens * 4 chars) |
| 32 | + private static final double DEFAULT_OVERLAP_RATE = 0.0; |
| 33 | + |
| 34 | + // Parameter restrictions |
| 35 | + private static final double OVERLAP_RATE_LOWER_BOUND = 0.0; |
| 36 | + private static final double OVERLAP_RATE_UPPER_BOUND = 0.5; // Max 50% overlap |
| 37 | + |
| 38 | + // Parameter values |
| 39 | + private int charLimit; |
| 40 | + private double overlapRate; |
| 41 | + |
| 42 | + /** |
| 43 | + * Constructor that initializes the fixed character length chunker with the specified parameters. |
| 44 | + * @param parameters a map with non-runtime parameters to be parsed |
| 45 | + */ |
| 46 | + public FixedCharLengthChunker(final Map<String, Object> parameters) { |
| 47 | + parseParameters(parameters); |
| 48 | + } |
| 49 | + |
| 50 | + /** |
| 51 | + * Parse the parameters for fixed character length algorithm. |
| 52 | + * Throw IllegalArgumentException when parameters are invalid. |
| 53 | + * |
| 54 | + * @param parameters a map with non-runtime parameters as the following: |
| 55 | + * 1. char_limit: the character limit for each chunked passage |
| 56 | + * 2. overlap_rate: the overlapping degree for each chunked passage, indicating how many characters come from the previous passage |
| 57 | + * Here are requirements for non-runtime parameters: |
| 58 | + * 1. char_limit must be a positive integer |
| 59 | + * 2. overlap_rate must be within range [0, 0.5] |
| 60 | + */ |
| 61 | + @Override |
| 62 | + public void parseParameters(Map<String, Object> parameters) { |
| 63 | + this.charLimit = parsePositiveIntegerWithDefault(parameters, CHAR_LIMIT_FIELD, DEFAULT_CHAR_LIMIT); |
| 64 | + this.overlapRate = parseDoubleWithDefault(parameters, OVERLAP_RATE_FIELD, DEFAULT_OVERLAP_RATE); |
| 65 | + |
| 66 | + if (overlapRate < OVERLAP_RATE_LOWER_BOUND || overlapRate > OVERLAP_RATE_UPPER_BOUND) { |
| 67 | + throw new IllegalArgumentException( |
| 68 | + String.format( |
| 69 | + Locale.ROOT, |
| 70 | + "Parameter [%s] must be between %s and %s, but was %s", |
| 71 | + OVERLAP_RATE_FIELD, |
| 72 | + OVERLAP_RATE_LOWER_BOUND, |
| 73 | + OVERLAP_RATE_UPPER_BOUND, |
| 74 | + overlapRate |
| 75 | + ) |
| 76 | + ); |
| 77 | + } |
| 78 | + } |
| 79 | + |
| 80 | + /** |
| 81 | + * Return the chunked passages for fixed character length algorithm. |
| 82 | + * Throw IllegalArgumentException when runtime parameters are invalid. |
| 83 | + * |
| 84 | + * @param content input string |
| 85 | + * @param runtimeParameters a map for runtime parameters, containing the following runtime parameters: |
| 86 | + * 1. max_chunk_limit: field level max chunk limit |
| 87 | + * 2. chunk_string_count: number of non-empty strings (including itself) which need to be chunked later |
| 88 | + */ |
| 89 | + @Override |
| 90 | + public List<String> chunk(final String content, final Map<String, Object> runtimeParameters) { |
| 91 | + int runtimeMaxChunkLimit = parseInteger(runtimeParameters, MAX_CHUNK_LIMIT_FIELD); |
| 92 | + int chunkStringCount = parseInteger(runtimeParameters, CHUNK_STRING_COUNT_FIELD); |
| 93 | + |
| 94 | + List<String> chunkResult = new ArrayList<>(); |
| 95 | + |
| 96 | + int startCharIndex = 0; |
| 97 | + int overlapCharNumber = (int) Math.floor(this.charLimit * this.overlapRate); |
| 98 | + // Ensure `chunkInterval` is positive. charLimit is positive. overlapRate is [0, 0.5]. |
| 99 | + // So, (charLimit - overlapCharNumber) >= 0.5 * charLimit, which is always > 0 if charLimit >= 1. |
| 100 | + int chunkInterval = this.charLimit - overlapCharNumber; |
| 101 | + |
| 102 | + while (startCharIndex < content.length()) { |
| 103 | + if (Chunker.checkRunTimeMaxChunkLimit(chunkResult.size(), runtimeMaxChunkLimit, chunkStringCount)) { |
| 104 | + chunkResult.add(content.substring(startCharIndex)); |
| 105 | + break; |
| 106 | + } |
| 107 | + |
| 108 | + int endPosition; |
| 109 | + // Check if the current chunk will extend to or past the end of the content |
| 110 | + if (startCharIndex + this.charLimit >= content.length()) { |
| 111 | + endPosition = content.length(); // Ensure chunk goes to the very end |
| 112 | + chunkResult.add(content.substring(startCharIndex, endPosition)); |
| 113 | + break; |
| 114 | + } else { |
| 115 | + endPosition = startCharIndex + this.charLimit; |
| 116 | + chunkResult.add(content.substring(startCharIndex, endPosition)); |
| 117 | + } |
| 118 | + |
| 119 | + startCharIndex += chunkInterval; |
| 120 | + } |
| 121 | + |
| 122 | + return chunkResult; |
| 123 | + } |
| 124 | + |
| 125 | + @Override |
| 126 | + public String getAlgorithmName() { |
| 127 | + return ALGORITHM_NAME; |
| 128 | + } |
| 129 | +} |
0 commit comments