Skip to content

Commit

Permalink
address code comment
Browse files Browse the repository at this point in the history
  • Loading branch information
deemoliu committed Feb 23, 2024
1 parent 02eff88 commit 9fa29e7
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 20 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -578,7 +578,7 @@ public static String[] split(String input, String delimiter, int limit) {
* @return generate an array of prefix strings of the string that are shorter than the specified length.
*/
@ScalarFunction
public static String[] prefixes(String input, int maxlength) {
public static String[] uniquePrefixes(String input, int maxlength) {
ObjectSet<String> prefixSet = new ObjectLinkedOpenHashSet<>();
for (int prefixLength = 1; prefixLength <= maxlength && prefixLength <= input.length(); prefixLength++) {
prefixSet.add(input.substring(0, prefixLength));
Expand All @@ -589,17 +589,17 @@ public static String[] prefixes(String input, int maxlength) {
/**
* @param input an input string for prefix strings generations.
* @param maxlength the max length of the prefix strings for the string.
* @param regexChar the character for regex matching to be added to prefix strings generated. e.g. '^'
* @param prefix the prefix to be prepended to prefix strings generated. e.g. '^' for regex matching
* @return generate an array of prefix matchers of the string that are shorter than the specified length.
*/
@ScalarFunction
public static String[] prefixMatchers(String input, int maxlength, String regexChar) {
if (regexChar == null) {
return prefixes(input, maxlength);
public static String[] uniquePrefixesWithPrefix(String input, int maxlength, String prefix) {
if (prefix == null) {
return uniquePrefixes(input, maxlength);
}
ObjectSet<String> prefixSet = new ObjectLinkedOpenHashSet<>();
for (int prefixLength = 1; prefixLength <= maxlength && prefixLength <= input.length(); prefixLength++) {
prefixSet.add(regexChar + input.substring(0, prefixLength));
prefixSet.add(prefix + input.substring(0, prefixLength));
}
return prefixSet.toArray(new String[0]);
}
Expand All @@ -610,7 +610,7 @@ public static String[] prefixMatchers(String input, int maxlength, String regexC
* @return generate an array of suffix strings of the string that are shorter than the specified length.
*/
@ScalarFunction
public static String[] suffixes(String input, int maxlength) {
public static String[] uniqueSuffixes(String input, int maxlength) {
ObjectSet<String> suffixSet = new ObjectLinkedOpenHashSet<>();
for (int suffixLength = 1; suffixLength <= maxlength && suffixLength <= input.length(); suffixLength++) {
suffixSet.add(input.substring(input.length() - suffixLength));
Expand All @@ -621,17 +621,17 @@ public static String[] suffixes(String input, int maxlength) {
/**
* @param input an input string for suffix strings generations.
* @param maxlength the max length of the suffix strings for the string.
* @param regexChar the character for regex matching to be added to suffix strings generated. e.g. '$'
* @param suffix the suffix string to be appended for suffix strings generated. e.g. '$' for regex matching.
* @return generate an array of suffix matchers of the string that are shorter than the specified length.
*/
@ScalarFunction
public static String[] suffixMatchers(String input, int maxlength, String regexChar) {
if (regexChar == null) {
return suffixes(input, maxlength);
public static String[] uniqueSuffixesWithSuffix(String input, int maxlength, String suffix) {
if (suffix == null) {
return uniqueSuffixes(input, maxlength);
}
ObjectSet<String> suffixSet = new ObjectLinkedOpenHashSet<>();
for (int suffixLength = 1; suffixLength <= maxlength && suffixLength <= input.length(); suffixLength++) {
suffixSet.add(input.substring(input.length() - suffixLength) + regexChar);
suffixSet.add(input.substring(input.length() - suffixLength) + suffix);
}
return suffixSet.toArray(new String[0]);
}
Expand All @@ -642,7 +642,7 @@ public static String[] suffixMatchers(String input, int maxlength, String regexC
* @return generate an array of ngram of the string that length are exactly matching the specified length.
*/
@ScalarFunction
public static String[] ngrams(String input, int length) {
public static String[] uniqueNgrams(String input, int length) {
if (length == 0 || length > input.length()) {
return new String[0];
}
Expand All @@ -660,7 +660,7 @@ public static String[] ngrams(String input, int length) {
* @return generate an array of ngram of the string that length are within the specified range [minGram, maxGram].
*/
@ScalarFunction
public static String[] ngrams(String input, int minGram, int maxGram) {
public static String[] uniqueNgrams(String input, int minGram, int maxGram) {
ObjectSet<String> ngramSet = new ObjectLinkedOpenHashSet<>();
for (int n = minGram; n <= maxGram && n <= input.length(); n++) {
if (n == 0) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -64,15 +64,15 @@ public static Object[][] ngramTestCases() {
@Test(dataProvider = "prefixAndSuffixTestCases")
public void testPrefixAndSuffix(String input, int length, String[] expectedPrefix, String[] expectedSuffix,
String[] expectedPrefixWithRegexChar, String[] expectedSuffixWithRegexChar) {
assertEquals(StringFunctions.prefixes(input, length), expectedPrefix);
assertEquals(StringFunctions.suffixes(input, length), expectedSuffix);
assertEquals(StringFunctions.prefixMatchers(input, length, "^"), expectedPrefixWithRegexChar);
assertEquals(StringFunctions.suffixMatchers(input, length, "$"), expectedSuffixWithRegexChar);
assertEquals(StringFunctions.uniquePrefixes(input, length), expectedPrefix);
assertEquals(StringFunctions.uniqueSuffixes(input, length), expectedSuffix);
assertEquals(StringFunctions.uniquePrefixesWithPrefix(input, length, "^"), expectedPrefixWithRegexChar);
assertEquals(StringFunctions.uniqueSuffixesWithSuffix(input, length, "$"), expectedSuffixWithRegexChar);
}

@Test(dataProvider = "ngramTestCases")
public void testNGram(String input, int minGram, int maxGram, String[] expectedExactNGram, String[] expectedNGram) {
assertEquals(StringFunctions.ngrams(input, maxGram), expectedExactNGram);
assertEquals(StringFunctions.ngrams(input, minGram, maxGram), expectedNGram);
assertEquals(StringFunctions.uniqueNgrams(input, maxGram), expectedExactNGram);
assertEquals(StringFunctions.uniqueNgrams(input, minGram, maxGram), expectedNGram);
}
}

0 comments on commit 9fa29e7

Please sign in to comment.