1 package org.apache.lucene.analysis.fr;
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 java.util.Arrays;
23 import org.apache.lucene.analysis.standard.StandardTokenizer; // for javadocs
24 import org.apache.lucene.analysis.CharArraySet;
25 import org.apache.lucene.analysis.TokenStream;
26 import org.apache.lucene.analysis.TokenFilter;
27 import org.apache.lucene.analysis.tokenattributes.CharTermAttribute;
28 import org.apache.lucene.util.Version;
31 * Removes elisions from a {@link TokenStream}. For example, "l'avion" (the plane) will be
32 * tokenized as "avion" (plane).
34 * @see <a href="http://fr.wikipedia.org/wiki/%C3%89lision">Elision in Wikipedia</a>
36 public final class ElisionFilter extends TokenFilter {
37 private CharArraySet articles = CharArraySet.EMPTY_SET;
38 private final CharTermAttribute termAtt = addAttribute(CharTermAttribute.class);
39 private static final CharArraySet DEFAULT_ARTICLES = CharArraySet.unmodifiableSet(
40 new CharArraySet(Version.LUCENE_CURRENT, Arrays.asList(
41 "l", "m", "t", "qu", "n", "s", "j"), true));
43 private static char[] apostrophes = {'\'', '\u2019'};
46 * Set the stopword articles
47 * @param matchVersion the lucene backwards compatibility version
48 * @param articles a set of articles
49 * @deprecated use {@link #ElisionFilter(Version, TokenStream, Set)} instead
52 public void setArticles(Version matchVersion, Set<?> articles) {
53 this.articles = CharArraySet.unmodifiableSet(
54 CharArraySet.copy(matchVersion, articles));
58 * Set the stopword articles
59 * @param articles a set of articles
60 * @deprecated use {@link #setArticles(Version, Set)} instead
63 public void setArticles(Set<?> articles) {
64 setArticles(Version.LUCENE_CURRENT, articles);
67 * Constructs an elision filter with standard stop words
69 public ElisionFilter(Version matchVersion, TokenStream input) {
70 this(matchVersion, input, DEFAULT_ARTICLES);
74 * Constructs an elision filter with standard stop words
75 * @deprecated use {@link #ElisionFilter(Version, TokenStream)} instead
78 public ElisionFilter(TokenStream input) {
79 this(Version.LUCENE_30, input);
83 * Constructs an elision filter with a Set of stop words
84 * @deprecated use {@link #ElisionFilter(Version, TokenStream, Set)} instead
87 public ElisionFilter(TokenStream input, Set<?> articles) {
88 this(Version.LUCENE_30, input, articles);
92 * Constructs an elision filter with a Set of stop words
93 * @param matchVersion the lucene backwards compatibility version
94 * @param input the source {@link TokenStream}
95 * @param articles a set of stopword articles
97 public ElisionFilter(Version matchVersion, TokenStream input, Set<?> articles) {
99 this.articles = CharArraySet.unmodifiableSet(
100 new CharArraySet(matchVersion, articles, true));
104 * Constructs an elision filter with an array of stop words
105 * @deprecated use {@link #ElisionFilter(Version, TokenStream, Set)} instead
108 public ElisionFilter(TokenStream input, String[] articles) {
109 this(Version.LUCENE_CURRENT, input,
110 new CharArraySet(Version.LUCENE_CURRENT,
111 Arrays.asList(articles), true));
115 * Increments the {@link TokenStream} with a {@link CharTermAttribute} without elisioned start
118 public final boolean incrementToken() throws IOException {
119 if (input.incrementToken()) {
120 char[] termBuffer = termAtt.buffer();
121 int termLength = termAtt.length();
123 int minPoz = Integer.MAX_VALUE;
124 for (int i = 0; i < apostrophes.length; i++) {
125 char apos = apostrophes[i];
126 // The equivalent of String.indexOf(ch)
127 for (int poz = 0; poz < termLength ; poz++) {
128 if (termBuffer[poz] == apos) {
129 minPoz = Math.min(poz, minPoz);
135 // An apostrophe has been found. If the prefix is an article strip it off.
136 if (minPoz != Integer.MAX_VALUE
137 && articles.contains(termAtt.buffer(), 0, minPoz)) {
138 termAtt.copyBuffer(termAtt.buffer(), minPoz + 1, termAtt.length() - (minPoz + 1));