forked from opensearch-project/neural-search
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1 from xinyual/addDelimiterChunker
Add delimiter chunker
- Loading branch information
Showing
3 changed files
with
128 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
84 changes: 84 additions & 0 deletions
84
src/test/java/org/opensearch/neuralsearch/processor/chunker/DelimiterChunkerTests.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,84 @@ | ||
/* | ||
* Copyright OpenSearch Contributors | ||
* SPDX-License-Identifier: Apache-2.0 | ||
*/ | ||
package org.opensearch.neuralsearch.processor.chunker; | ||
|
||
import org.junit.Assert; | ||
import org.junit.Test; | ||
|
||
import java.util.List; | ||
import java.util.Map; | ||
|
||
import static junit.framework.TestCase.assertEquals; | ||
import static org.junit.Assert.assertThrows; | ||
import static org.opensearch.neuralsearch.processor.chunker.DelimiterChunker.DELIMITER_FIELD; | ||
|
||
public class DelimiterChunkerTests { | ||
|
||
@Test | ||
public void testChunkerWithNoDelimiterField() { | ||
DelimiterChunker chunker = new DelimiterChunker(); | ||
String content = "a\nb\nc\nd"; | ||
Map<String, Object> inputParameters = Map.of("", ""); | ||
Exception exception = assertThrows(IllegalArgumentException.class, () -> chunker.validateParameters(inputParameters)); | ||
Assert.assertEquals("You must contain field:" + DELIMITER_FIELD + " in your parameter", exception.getMessage()); | ||
} | ||
|
||
@Test | ||
public void testChunkerWithDelimiterFieldNotString() { | ||
DelimiterChunker chunker = new DelimiterChunker(); | ||
String content = "a\nb\nc\nd"; | ||
Map<String, Object> inputParameters = Map.of(DELIMITER_FIELD, List.of("")); | ||
Exception exception = assertThrows(IllegalArgumentException.class, () -> chunker.validateParameters(inputParameters)); | ||
Assert.assertEquals("delimiter parameters: " + List.of("") + " must be string", exception.getMessage()); | ||
} | ||
|
||
@Test | ||
public void testChunker() { | ||
DelimiterChunker chunker = new DelimiterChunker(); | ||
String content = "a\nb\nc\nd"; | ||
Map<String, Object> inputParameters = Map.of(DELIMITER_FIELD, "\n"); | ||
List<String> chunkResult = chunker.chunk(content, inputParameters); | ||
assertEquals(4, chunkResult.size()); | ||
assertEquals(7, cntLength(chunkResult)); | ||
} | ||
|
||
@Test | ||
public void testChunkerWithDelimiterEnd() { | ||
DelimiterChunker chunker = new DelimiterChunker(); | ||
String content = "a\nb\nc\nd\n"; | ||
Map<String, Object> inputParameters = Map.of(DELIMITER_FIELD, "\n"); | ||
List<String> chunkResult = chunker.chunk(content, inputParameters); | ||
assertEquals(4, chunkResult.size()); | ||
assertEquals(8, cntLength(chunkResult)); | ||
} | ||
|
||
@Test | ||
public void testChunkerWithOnlyDelimiter() { | ||
DelimiterChunker chunker = new DelimiterChunker(); | ||
String content = "\n"; | ||
Map<String, Object> inputParameters = Map.of(DELIMITER_FIELD, "\n"); | ||
List<String> chunkResult = chunker.chunk(content, inputParameters); | ||
assertEquals(1, chunkResult.size()); | ||
assertEquals(1, cntLength(chunkResult)); | ||
} | ||
|
||
@Test | ||
public void testChunkerWithAllDelimiters() { | ||
DelimiterChunker chunker = new DelimiterChunker(); | ||
String content = "\n\n\n"; | ||
Map<String, Object> inputParameters = Map.of(DELIMITER_FIELD, "\n"); | ||
List<String> chunkResult = chunker.chunk(content, inputParameters); | ||
assertEquals(3, chunkResult.size()); | ||
assertEquals(3, cntLength(chunkResult)); | ||
} | ||
|
||
private int cntLength(List<String> outputs) { | ||
int totalLength = 0; | ||
for (String output : outputs) { | ||
totalLength += output.length(); | ||
} | ||
return totalLength; | ||
} | ||
} |
17 changes: 17 additions & 0 deletions
17
src/test/resources/processor/DelimiterChunkerProcessor.json
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
{ | ||
"description": "An example delimiter chunker pipeline", | ||
"processors" : [ | ||
{ | ||
"chunking": { | ||
"field_map": { | ||
"body_chunk1": { | ||
"delimiter": { | ||
"delimiter": "\n" | ||
}, | ||
"output_field": "body_chunk2" | ||
} | ||
} | ||
} | ||
} | ||
] | ||
} |