Skip to content

Commit

Permalink
move validate to commonUtlis
Browse files Browse the repository at this point in the history
Signed-off-by: xinyual <[email protected]>
  • Loading branch information
xinyual committed Mar 11, 2024
1 parent 2f8557a commit e169c34
Show file tree
Hide file tree
Showing 3 changed files with 55 additions and 36 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
/*
* Copyright OpenSearch Contributors
* SPDX-License-Identifier: Apache-2.0
*/

package org.opensearch.neuralsearch.processor.chunker;

import org.apache.commons.lang3.StringUtils;
import org.apache.commons.lang3.math.NumberUtils;

import java.util.Map;

public class ChunkerUtils {
public static String validateStringParameters(
Map<String, Object> parameters,
String fieldName,
String defaultValue,
boolean allowEmpty
) {
if (!parameters.containsKey(fieldName)) {
// all parameters are optional
return defaultValue;
}
Object fieldValue = parameters.get(fieldName);
if (!(fieldValue instanceof String)) {
throw new IllegalArgumentException("Chunker parameter [" + fieldName + "] cannot be cast to [" + String.class.getName() + "]");
} else if (!allowEmpty && StringUtils.isEmpty(fieldValue.toString())) {
throw new IllegalArgumentException("Chunker parameter: " + fieldName + " should not be empty.");
}
return (String) fieldValue;
}

public static int validatePositiveIntegerParameter(Map<String, Object> parameters, String fieldName, int defaultValue) {
// this method validate that parameter is a positive integer
if (!parameters.containsKey(fieldName)) {
// all parameters are optional
return defaultValue;
}
String fieldValue = parameters.get(fieldName).toString();
if (!(NumberUtils.isParsable(fieldValue))) {
throw new IllegalArgumentException(
"fixed length parameter [" + fieldName + "] cannot be cast to [" + Number.class.getName() + "]"
);
}
if (NumberUtils.createInteger(fieldValue) <= 0) {
throw new IllegalArgumentException("fixed length parameter [" + fieldName + "] must be positive");
}
return Integer.valueOf(fieldValue);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
import java.util.List;
import java.util.ArrayList;

import static org.opensearch.neuralsearch.processor.chunker.FixedTokenLengthChunker.validateStringParameters;
import static org.opensearch.neuralsearch.processor.chunker.ChunkerUtils.validateStringParameters;

/**
* The implementation {@link Chunker} for delimiter algorithm
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@
import org.opensearch.index.analysis.AnalysisRegistry;
import org.opensearch.action.admin.indices.analyze.AnalyzeAction;
import static org.opensearch.action.admin.indices.analyze.TransportAnalyzeAction.analyze;
import static org.opensearch.neuralsearch.processor.chunker.ChunkerUtils.validatePositiveIntegerParameter;
import static org.opensearch.neuralsearch.processor.chunker.ChunkerUtils.validateStringParameters;

/**
* The implementation {@link Chunker} for fixed token length algorithm.
Expand Down Expand Up @@ -93,42 +95,9 @@ public void validateParameters(Map<String, Object> parameters) {
this.tokenConcatenator = validateStringParameters(parameters, TOKEN_CONCATENATOR_FIELD, DEFAULT_TOKEN_CONCATENATOR, true);
}

public static String validateStringParameters(
Map<String, Object> parameters,
String fieldName,
String defaultValue,
boolean allowEmpty
) {
if (!parameters.containsKey(fieldName)) {
// all parameters are optional
return defaultValue;
}
Object fieldValue = parameters.get(fieldName);
if (!(fieldValue instanceof String)) {
throw new IllegalArgumentException("Chunker parameter [" + fieldName + "] cannot be cast to [" + String.class.getName() + "]");
} else if (!allowEmpty && StringUtils.isEmpty(fieldValue.toString())) {
throw new IllegalArgumentException("Chunker parameter: " + fieldName + " should not be empty.");
}
return (String) fieldValue;
}

private int validatePositiveIntegerParameter(Map<String, Object> parameters, String fieldName, int defaultValue) {
// this method validate that parameter is a positive integer
if (!parameters.containsKey(fieldName)) {
// all parameters are optional
return defaultValue;
}
String fieldValue = parameters.get(fieldName).toString();
if (!(NumberUtils.isParsable(fieldValue))) {
throw new IllegalArgumentException(
"fixed length parameter [" + fieldName + "] cannot be cast to [" + Number.class.getName() + "]"
);
}
if (NumberUtils.createInteger(fieldValue) <= 0) {
throw new IllegalArgumentException("fixed length parameter [" + fieldName + "] must be positive");
}
return Integer.valueOf(fieldValue);
}



/**
* Return the chunked passages for fixed token length algorithm
Expand Down

0 comments on commit e169c34

Please sign in to comment.