magibney commented on a change in pull request #892: LUCENE-8972: Add ICUTransformCharFilter, to support pre-tokenizer ICU text transformation URL: https://github.com/apache/lucene-solr/pull/892#discussion_r341695837
########## File path: lucene/analysis/icu/src/test/org/apache/lucene/analysis/icu/TestICUTransformCharFilter.java ########## @@ -0,0 +1,153 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.apache.lucene.analysis.icu; + + +import java.io.IOException; +import java.io.Reader; +import java.io.StringReader; + +import org.apache.lucene.analysis.Analyzer; +import org.apache.lucene.analysis.BaseTokenStreamTestCase; +import org.apache.lucene.analysis.MockTokenizer; +import org.apache.lucene.analysis.Tokenizer; +import org.apache.lucene.analysis.core.KeywordTokenizer; + +import com.ibm.icu.text.Transliterator; +import com.ibm.icu.text.UnicodeSet; + + +/** + * Test the ICUTransformCharFilter with some basic examples. + */ +public class TestICUTransformCharFilter extends BaseTokenStreamTestCase { + + public void testBasicFunctionality() throws Exception { + checkToken(Transliterator.getInstance("Traditional-Simplified"), + "簡化字", "简化字"); + checkToken(Transliterator.getInstance("Katakana-Hiragana"), + "ヒラガナ", "ひらがな"); + checkToken(Transliterator.getInstance("Fullwidth-Halfwidth"), + "アルアノリウ", "アルアノリウ"); + checkToken(Transliterator.getInstance("Any-Latin"), + "Αλφαβητικός Κατάλογος", "Alphabētikós Katálogos"); + checkToken(Transliterator.getInstance("NFD; [:Nonspacing Mark:] Remove"), + "Alphabētikós Katálogos", "Alphabetikos Katalogos"); + checkToken(Transliterator.getInstance("Han-Latin"), + "中国", "zhōng guó"); + } + + public void testRollbackBuffer() throws Exception { + checkToken(Transliterator.getInstance("Cyrillic-Latin"), + "яяяяя", "âââââ"); // final NFC transform applied + checkToken(Transliterator.getInstance("Cyrillic-Latin"), 0, + "яяяяя", "a\u0302a\u0302a\u0302a\u0302a\u0302"); // final NFC transform never applied + checkToken(Transliterator.getInstance("Cyrillic-Latin"), 2, + "яяяяя", "ââa\u0302a\u0302a\u0302"); + checkToken(Transliterator.getInstance("Cyrillic-Latin"), 4, + "яяяяяяяяяя", "ââa\u0302a\u0302a\u0302a\u0302a\u0302a\u0302a\u0302a\u0302"); + checkToken(Transliterator.getInstance("Cyrillic-Latin"), 8, + "яяяяяяяяяяяяяяяяяяяя", "ââââââa\u0302ââââa\u0302ââââa\u0302âââ"); + } + + public void testCustomFunctionality() throws Exception { + String rules = "a > b; b > c;"; // convert a's to b's and b's to c's + checkToken(Transliterator.createFromRules("test", rules, Transliterator.FORWARD), "abacadaba", "bcbcbdbcb"); + } + + public void testCustomFunctionality2() throws Exception { + String rules = "c { a > b; a > d;"; // convert a's to b's and b's to c's + checkToken(Transliterator.createFromRules("test", rules, Transliterator.FORWARD), "caa", "cbd"); + } + + public void testOptimizer() throws Exception { + String rules = "a > b; b > c;"; // convert a's to b's and b's to c's + Transliterator custom = Transliterator.createFromRules("test", rules, Transliterator.FORWARD); + assertTrue(custom.getFilter() == null); + new ICUTransformCharFilter(new StringReader(""), custom); + assertTrue(custom.getFilter().equals(new UnicodeSet("[ab]"))); + } + + public void testOptimizer2() throws Exception { + checkToken(Transliterator.getInstance("Traditional-Simplified; CaseFold"), + "ABCDE", "abcde"); + } + + public void testOptimizerSurrogate() throws Exception { + String rules = "\\U00020087 > x;"; // convert CJK UNIFIED IDEOGRAPH-20087 to an x + Transliterator custom = Transliterator.createFromRules("test", rules, Transliterator.FORWARD); + assertTrue(custom.getFilter() == null); + new ICUTransformCharFilter(new StringReader(""), custom); + assertTrue(custom.getFilter().equals(new UnicodeSet("[\\U00020087]"))); + } + + private void checkToken(Transliterator transform, String input, String expected) throws IOException { + checkToken(transform, ICUTransformCharFilter.DEFAULT_MAX_ROLLBACK_BUFFER_CAPACITY, input, expected); + } + + private void checkToken(Transliterator transform, int maxRollbackBufferCapacity, String input, String expected) throws IOException { + final KeywordTokenizer input1 = new KeywordTokenizer(); + input1.setReader(new ICUTransformCharFilter(new StringReader(input), transform, maxRollbackBufferCapacity)); + assertTokenStreamContents(input1, new String[] { expected }); + } + + /** blast some random strings through the analyzer */ + public void testRandomStrings() throws Exception { + final Transliterator transform = Transliterator.getInstance("Any-Latin"); Review comment: Got serviceable results by just running another `testRandomStrings()` with "Latin-Katakana". This resulted in plenty of positive diffs (decreased character count) to complement the negative diffs (decreased character counts) that were generated by the "Any-Latin" transform, so I think that'll suffice? EDIT: [proposed change](https://github.com/apache/lucene-solr/pull/892/commits/11190eb76d42440bbf092499375bdf4b630a2433) ---------------------------------------------------------------- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. For queries about this service, please contact Infrastructure at: us...@infra.apache.org With regards, Apache Git Services --------------------------------------------------------------------- To unsubscribe, e-mail: issues-unsubscr...@lucene.apache.org For additional commands, e-mail: issues-h...@lucene.apache.org