Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix and add ut #9

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -152,7 +152,7 @@ private List<String> chunkString(String content) {
"Unable to create the processor as the number of chunks ["
+ current_chunk_count
+ "] exceeds the maximum chunk limit ["
+ MAX_CHUNK_LIMIT_FIELD
+ max_chunk_limit
+ "]"
);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,6 @@ public DelimiterChunker() {}

public static String DELIMITER_FIELD = "delimiter";

public static String MAX_CHUNK_LIMIT_FIELD = "max_chunk_limit";

private static final int DEFAULT_MAX_CHUNK_LIMIT = 100;

@Override
public void validateParameters(Map<String, Object> parameters) {
if (parameters.containsKey(DELIMITER_FIELD)) {
Expand All @@ -28,41 +24,25 @@ public void validateParameters(Map<String, Object> parameters) {
throw new IllegalArgumentException("delimiter parameters should not be empty.");
}
}
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() + " should be integer.");
} else if ((int) maxChunkLimit < 0) {
throw new IllegalArgumentException("Parameter max_chunk_limit:" + maxChunkLimit + " is negative.");
}
}
}

@Override
public List<String> chunk(String content, Map<String, Object> parameters) {
String delimiter = (String) parameters.getOrDefault(DELIMITER_FIELD, ".");
int maxChunkingNumber = (int) parameters.getOrDefault(MAX_CHUNK_LIMIT_FIELD, -1);
List<String> chunkResult = new ArrayList<>();
int start = 0;
int end = content.indexOf(delimiter);

while (end != -1) {
addChunkResult(chunkResult, maxChunkingNumber, content.substring(start, end + delimiter.length()));
chunkResult.add(content.substring(start, end + delimiter.length()));
start = end + delimiter.length();
end = content.indexOf(delimiter, start);
}

if (start < content.length()) {
addChunkResult(chunkResult, maxChunkingNumber, content.substring(start));
chunkResult.add(content.substring(start));
}
return chunkResult;

}

private void addChunkResult(List<String> chunkResult, int maxChunkingNumber, String candidate) {
if (chunkResult.size() >= maxChunkingNumber && maxChunkingNumber > 0) {
throw new IllegalStateException("Exceed max chunk number: " + maxChunkingNumber);
}
chunkResult.add(candidate);
}
}
Loading
Loading