1 package org.apache.lucene.analysis.icu;
4 * Licensed to the Apache Software Foundation (ASF) under one or more
5 * contributor license agreements. See the NOTICE file distributed with
6 * this work for additional information regarding copyright ownership.
7 * The ASF licenses this file to You under the Apache License, Version 2.0
8 * (the "License"); you may not use this file except in compliance with
9 * the License. You may obtain a copy of the License at
11 * http://www.apache.org/licenses/LICENSE-2.0
13 * Unless required by applicable law or agreed to in writing, software
14 * distributed under the License is distributed on an "AS IS" BASIS,
15 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 * See the License for the specific language governing permissions and
17 * limitations under the License.
20 import java.io.IOException;
22 import org.apache.lucene.analysis.TokenFilter;
23 import org.apache.lucene.analysis.TokenStream;
24 import org.apache.lucene.analysis.tokenattributes.CharTermAttribute;
26 import com.ibm.icu.text.Normalizer;
27 import com.ibm.icu.text.Normalizer2;
30 * Normalize token text with ICU's {@link com.ibm.icu.text.Normalizer2}
32 * With this filter, you can normalize text in the following ways:
34 * <li> NFKC Normalization, Case Folding, and removing Ignorables (the default)
35 * <li> Using a standard Normalization mode (NFC, NFD, NFKC, NFKD)
36 * <li> Based on rules from a custom normalization mapping.
39 * If you use the defaults, this filter is a simple way to standardize Unicode text
40 * in a language-independent way for search:
42 * <li> The case folding that it does can be seen as a replacement for
43 * LowerCaseFilter: For example, it handles cases such as the Greek sigma, so that
44 * "Μάϊος" and "ΜΆΪΟΣ" will match correctly.
45 * <li> The normalization will standardizes different forms of the same
46 * character in Unicode. For example, CJK full-width numbers will be standardized
47 * to their ASCII forms.
48 * <li> Ignorables such as Zero-Width Joiner and Variation Selectors are removed.
49 * These are typically modifier characters that affect display.
52 * @see com.ibm.icu.text.Normalizer2
53 * @see com.ibm.icu.text.FilteredNormalizer2
55 public class ICUNormalizer2Filter extends TokenFilter {
56 private final CharTermAttribute termAtt = addAttribute(CharTermAttribute.class);
57 private final Normalizer2 normalizer;
58 private final StringBuilder buffer = new StringBuilder();
61 * Create a new Normalizer2Filter that combines NFKC normalization, Case
62 * Folding, and removes Default Ignorables (NFKC_Casefold)
64 public ICUNormalizer2Filter(TokenStream input) {
65 this(input, Normalizer2.getInstance(null, "nfkc_cf", Normalizer2.Mode.COMPOSE));
69 * Create a new Normalizer2Filter with the specified Normalizer2
71 * @param normalizer normalizer to use
73 public ICUNormalizer2Filter(TokenStream input, Normalizer2 normalizer) {
75 this.normalizer = normalizer;
79 public final boolean incrementToken() throws IOException {
80 if (input.incrementToken()) {
81 if (normalizer.quickCheck(termAtt) != Normalizer.YES) {
83 normalizer.normalize(termAtt, buffer);
84 termAtt.setEmpty().append(buffer);