Skip to content

Commit

Permalink
Merge pull request #7 from xinyual/addMaxChunkForDelimiter
Browse files Browse the repository at this point in the history
change logic of max chunk number
  • Loading branch information
xinyual authored Mar 1, 2024
2 parents 1aa3cb4 + 165dfa8 commit 001d421
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -31,17 +31,17 @@ public void validateParameters(Map<String, Object> parameters) {
if (parameters.containsKey(MAX_CHUNK_LIMIT_FIELD)) {
Object maxChunkLimit = parameters.get(MAX_CHUNK_LIMIT_FIELD);
if (!(maxChunkLimit instanceof Integer)) {
throw new IllegalArgumentException(
"Parameter max_chunk_limit:" + maxChunkLimit.toString() + " cannot be converted to integer."
);
throw new IllegalArgumentException("Parameter max_chunk_limit:" + maxChunkLimit.toString() + " should be integer.");
} else if ((int) maxChunkLimit <= 0) {
throw new IllegalArgumentException("Parameter max_chunk_limit:" + maxChunkLimit + " is not greater than 0.");
}
}
}

@Override
public List<String> chunk(String content, Map<String, Object> parameters) {
String delimiter = (String) parameters.get(DELIMITER_FIELD);
int maxChunkingNumber = (int) parameters.getOrDefault(MAX_CHUNK_LIMIT_FIELD, 0);
int maxChunkingNumber = (int) parameters.getOrDefault(MAX_CHUNK_LIMIT_FIELD, -1);
List<String> chunkResult = new ArrayList<>();
int start = 0;
int end = content.indexOf(delimiter);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,15 +30,23 @@ public void testChunkerWithWrongLimitFieldList() {
String content = "a\nb\nc\nd";
Map<String, Object> inputParameters = Map.of(MAX_CHUNK_LIMIT_FIELD, List.of("-1"), DELIMITER_FIELD, "\n");
Exception exception = assertThrows(IllegalArgumentException.class, () -> chunker.validateParameters(inputParameters));
Assert.assertEquals("Parameter max_chunk_limit:" + List.of("-1") + " cannot be converted to integer.", exception.getMessage());
Assert.assertEquals("Parameter max_chunk_limit:" + List.of("-1") + " should be integer.", exception.getMessage());
}

public void testChunkerWithWrongLimitField() {
DelimiterChunker chunker = new DelimiterChunker();
String content = "a\nb\nc\nd";
Map<String, Object> inputParameters = Map.of(MAX_CHUNK_LIMIT_FIELD, "1000\n", DELIMITER_FIELD, "\n");
Exception exception = assertThrows(IllegalArgumentException.class, () -> chunker.validateParameters(inputParameters));
Assert.assertEquals("Parameter max_chunk_limit:1000\n cannot be converted to integer.", exception.getMessage());
Assert.assertEquals("Parameter max_chunk_limit:1000\n should be integer.", exception.getMessage());
}

public void testChunkerWithNegativeLimit() {
DelimiterChunker chunker = new DelimiterChunker();
String content = "a\nb\nc\nd";
Map<String, Object> inputParameters = Map.of(MAX_CHUNK_LIMIT_FIELD, -1, DELIMITER_FIELD, "\n");
Exception exception = assertThrows(IllegalArgumentException.class, () -> chunker.validateParameters(inputParameters));
Assert.assertEquals("Parameter max_chunk_limit:-1 is not greater than 0.", exception.getMessage());
}

public void testChunkerWithDelimiterFieldNotString() {
Expand Down

0 comments on commit 001d421

Please sign in to comment.