1 package org.apache.lucene.analysis;
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;
23 import org.apache.lucene.analysis.tokenattributes.KeywordAttribute;
24 import org.apache.lucene.analysis.tokenattributes.CharTermAttribute;
25 import org.apache.lucene.util.Version;
28 * Marks terms as keywords via the {@link KeywordAttribute}. Each token
29 * contained in the provided is marked as a keyword by setting
30 * {@link KeywordAttribute#setKeyword(boolean)} to <code>true</code>.
32 * @see KeywordAttribute
34 public final class KeywordMarkerFilter extends TokenFilter {
36 private final KeywordAttribute keywordAttr = addAttribute(KeywordAttribute.class);
37 private final CharTermAttribute termAtt = addAttribute(CharTermAttribute.class);
38 private final CharArraySet keywordSet;
41 * Create a new KeywordMarkerFilter, that marks the current token as a
42 * keyword if the tokens term buffer is contained in the given set via the
43 * {@link KeywordAttribute}.
46 * TokenStream to filter
48 * the keywords set to lookup the current termbuffer
50 public KeywordMarkerFilter(final TokenStream in,
51 final CharArraySet keywordSet) {
53 this.keywordSet = keywordSet;
57 * Create a new KeywordMarkerFilter, that marks the current token as a
58 * keyword if the tokens term buffer is contained in the given set via the
59 * {@link KeywordAttribute}.
62 * TokenStream to filter
64 * the keywords set to lookup the current termbuffer
66 public KeywordMarkerFilter(final TokenStream in, final Set<?> keywordSet) {
67 this(in, keywordSet instanceof CharArraySet ? (CharArraySet) keywordSet
68 : CharArraySet.copy(Version.LUCENE_31, keywordSet));
72 public final boolean incrementToken() throws IOException {
73 if (input.incrementToken()) {
74 if (keywordSet.contains(termAtt.buffer(), 0, termAtt.length())) {
75 keywordAttr.setKeyword(true);