Skip to content

Apache Lucene open-source search software

LUCENE-8966: The Korean analyzer split tokens on boundaries between digits and alphabetic characters.

jimczic8f36238ab0d
diff --git a/lucene/CHANGES.txt b/lucene/CHANGES.txt
index c3d17498699..8a50c36359b 100644
--- a/lucene/CHANGES.txt
+++ b/lucene/CHANGES.txt
@@ -135,6 +135,8 @@ Improvements
* LUCENE-8976: Use exact distance between point and bounding rectangle in FloatPointNearestNeighbor. (Ignacio Vera)
+* LUCENE-8966: The Korean analyzer now split tokens on boundaries between digits and alphabetic characters. (Jim Ferenczi)
+
Optimizations
* LUCENE-8922: DisjunctionMaxQuery more efficiently leverages impacts to skip
diff --git a/lucene/analysis/nori/src/java/org/apache/lucene/analysis/ko/KoreanTokenizer.java b/lucene/analysis/nori/src/java/org/apache/lucene/analysis/ko/KoreanTokenizer.java
index b5c310a169a..3efccaf575e 100644
--- a/lucene/analysis/nori/src/java/org/apache/lucene/analysis/ko/KoreanTokenizer.java
+++ b/lucene/analysis/nori/src/java/org/apache/lucene/analysis/ko/KoreanTokenizer.java
@@ -754,6 +754,7 @@ public final class KoreanTokenizer extends Tokenizer {
unknownWordLength = 1;
UnicodeScript scriptCode = UnicodeScript.of(firstCharacter);
final boolean isPunct = isPunctuation(firstCharacter);
+ final boolean isDigit = Character.isDigit(firstCharacter);
for (int posAhead = pos + 1; unknownWordLength < MAX_UNKNOWN_WORD_LENGTH; posAhead++) {
int next = buffer.get(posAhead);
if (next == -1) {
@@ -768,8 +769,11 @@ public final class KoreanTokenizer extends Tokenizer {
|| chType == Character.NON_SPACING_MARK;
if (sameScript
- && isPunctuation(ch, chType) == isPunct
- && characterDefinition.isGroup(ch)) {
+ // split on punctuation
+ && isPunctuation(ch, chType) == isPunct
+ // split on digit
+ && Character.isDigit(ch) == isDigit
+ && characterDefinition.isGroup(ch)) {
unknownWordLength++;
} else {
break;
diff --git a/lucene/analysis/nori/src/test/org/apache/lucene/analysis/ko/TestKoreanTokenizer.java b/lucene/analysis/nori/src/test/org/apache/lucene/analysis/ko/TestKoreanTokenizer.java
index 2e9639ce6b9..a9c5bb771c6 100644
--- a/lucene/analysis/nori/src/test/org/apache/lucene/analysis/ko/TestKoreanTokenizer.java
+++ b/lucene/analysis/nori/src/test/org/apache/lucene/analysis/ko/TestKoreanTokenizer.java
@@ -108,6 +108,22 @@ public class TestKoreanTokenizer extends BaseTokenStreamTestCase {
};
}
+ public void testSeparateNumber() throws IOException {
+ assertAnalyzesTo(analyzer, "44사이즈",
+ new String[]{"44", "사이즈"},
+ new int[]{0, 2},
+ new int[]{2, 5},
+ new int[]{1, 1}
+ );
+
+ assertAnalyzesTo(analyzer, "9.9사이즈",
+ new String[]{"9", "9", "사이즈"},
+ new int[]{0, 2, 3},
+ new int[]{1, 3, 6},
+ new int[]{1, 1, 1}
+ );
+ }
+
public void testSpaces() throws IOException {
assertAnalyzesTo(analyzer, "화학 이외의 것",
new String[]{"화학", "이외", "의", "것"},