Apache Lucene open-source search software
LUCENE-503: New ThaiAnalyzer and ThaiWordFilter
Chris M. Hostetter2123b476df53
diff --git a/CHANGES.txt b/CHANGES.txtindex fffd8681f28..92ea6ac48b9 100644--- a/CHANGES.txt+++ b/CHANGES.txt@@ -6,7 +6,8 @@ Trunk (not yet released)New features- 1.+ 1. New ThaiAnalyzer and ThaiWordFilter in contrib/analyzers+ (Samphan Raruenrom va Chris Hostetter)API Changesdiff --git a/contrib/analyzers/src/java/org/apache/lucene/analysis/th/ThaiAnalyzer.java b/contrib/analyzers/src/java/org/apache/lucene/analysis/th/ThaiAnalyzer.javanew file mode 100644index 00000000000..8447143a374--- /dev/null+++ b/contrib/analyzers/src/java/org/apache/lucene/analysis/th/ThaiAnalyzer.java@@ -0,0 +1,40 @@+package org.apache.lucene.analysis.th;++/**+ * Copyright 2006 The Apache Software Foundation+ *+ * Licensed 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.+ */++import java.io.Reader;+import org.apache.lucene.analysis.Analyzer;+import org.apache.lucene.analysis.StopAnalyzer;+import org.apache.lucene.analysis.StopFilter;+import org.apache.lucene.analysis.TokenStream;+import org.apache.lucene.analysis.standard.StandardFilter;+import org.apache.lucene.analysis.standard.StandardTokenizer;++/**+ * Analyzer for Thai language. It uses java.text.BreakIterator to break words.+ * @author Samphan Raruenrom <[email protected]> for To-Be-One Technology Co., Ltd.+ * @version 0.2+ */+public class ThaiAnalyzer extends Analyzer {+ public TokenStream tokenStream(String fieldName, Reader reader) {+ TokenStream ts = new StandardTokenizer(reader);+ ts = new StandardFilter(ts);+ ts = new ThaiWordFilter(ts);+ ts = new StopFilter(ts, StopAnalyzer.ENGLISH_STOP_WORDS);+ return ts;+ }+}diff --git a/contrib/analyzers/src/java/org/apache/lucene/analysis/th/ThaiWordFilter.java b/contrib/analyzers/src/java/org/apache/lucene/analysis/th/ThaiWordFilter.javanew file mode 100644index 00000000000..da9e9b57304--- /dev/null+++ b/contrib/analyzers/src/java/org/apache/lucene/analysis/th/ThaiWordFilter.java@@ -0,0 +1,71 @@+package org.apache.lucene.analysis.th;++/**+ * Copyright 2006 The Apache Software Foundation+ *+ * Licensed 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.+ */++import java.io.IOException;+import java.util.Locale;+import java.lang.Character.UnicodeBlock;+import org.apache.lucene.analysis.Token;+import org.apache.lucene.analysis.TokenFilter;+import org.apache.lucene.analysis.TokenStream;+import java.text.BreakIterator;++/**+ * TokenFilter that use java.text.BreakIterator to break each+ * Token that is Thai into separate Token(s) for each Thai word.+ * @author Samphan Raruenrom <[email protected]> for To-Be-One Technology Co., Ltd.+ * @version 0.2+ */+public class ThaiWordFilter extends TokenFilter {++ private BreakIterator breaker = null;+ private Token thaiToken = null;++ public ThaiWordFilter(TokenStream input) {+ super(input);+ breaker = BreakIterator.getWordInstance(new Locale("th"));+ }++ public Token next() throws IOException {+ if (thaiToken != null) {+ String text = thaiToken.termText();+ int start = breaker.current();+ int end = breaker.next();+ if (end != BreakIterator.DONE) {+ return new Token(text.substring(start, end),+ thaiToken.startOffset()+start, thaiToken.startOffset()+end, thaiToken.type());+ }+ thaiToken = null;+ }+ Token tk = input.next();+ if (tk == null) {+ return null;+ }+ String text = tk.termText();+ if (UnicodeBlock.of(text.charAt(0)) != UnicodeBlock.THAI) {+ return new Token(text.toLowerCase(), tk.startOffset(), tk.endOffset(), tk.type());+ }+ thaiToken = tk;+ breaker.setText(text);+ int end = breaker.next();+ if (end != BreakIterator.DONE) {+ return new Token(text.substring(0, end),+ thaiToken.startOffset(), thaiToken.startOffset()+end, thaiToken.type());+ }+ return null;+ }+}diff --git a/contrib/analyzers/src/test/org/apache/lucene/analysis/th/TestThaiAnalyzer.java b/contrib/analyzers/src/test/org/apache/lucene/analysis/th/TestThaiAnalyzer.javanew file mode 100644index 00000000000..ef6cf26170f--- /dev/null+++ b/contrib/analyzers/src/test/org/apache/lucene/analysis/th/TestThaiAnalyzer.java@@ -0,0 +1,69 @@+package org.apache.lucene.analysis.th;++/**+ * Copyright 2006 The Apache Software Foundation+ *+ * Licensed 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.+ */++import java.io.StringReader;+import junit.framework.TestCase;+import org.apache.lucene.analysis.Analyzer;+import org.apache.lucene.analysis.Token;+import org.apache.lucene.analysis.TokenStream;++/**+ * Test case for ThaiAnalyzer, modified from TestFrenchAnalyzer+ *+ * @author Samphan Raruenrom <[email protected]>+ * @version 0.1+ */++public class TestThaiAnalyzer extends TestCase {++ public void assertAnalyzesTo(Analyzer a, String input, String[] output)+ throws Exception {++ TokenStream ts = a.tokenStream("dummy", new StringReader(input));++ for (int i = 0; i < output.length; i++) {+ Token t = ts.next();+ assertNotNull(t);+ assertEquals(t.termText(), output[i]);+ }+ assertNull(ts.next());+ ts.close();+ }++ public void testAnalyzer() throws Exception {+ ThaiAnalyzer analyzer = new ThaiAnalyzer();++ assertAnalyzesTo(analyzer, "", new String[] {});++ assertAnalyzesTo(+ analyzer,+ "การที่ได้ต้องแสดงว่างานดี",+ new String[] { "การ", "ที่", "ได้", "ต้อง", "แสดง", "ว่า", "งาน", "ดี"});++ assertAnalyzesTo(+ analyzer,+ "บริษัทชื่อ XY&Z - คุยกับ [email protected]",+ new String[] { "บริษัท", "ชื่อ", "xy&z", "คุย", "กับ", "[email protected]" });++ // English stop words+ assertAnalyzesTo(+ analyzer,+ "ประโยคว่า The quick brown fox jumped over the lazy dogs",+ new String[] { "ประโยค", "ว่า", "quick", "brown", "fox", "jumped", "over", "lazy", "dogs" });+ }+}