X-Git-Url: https://git.mdrn.pl/pylucene.git/blobdiff_plain/a2e61f0c04805cfcb8706176758d1283c7e3a55c..aaeed5504b982cf3545252ab528713250aa33eed:/lucene-java-3.5.0/lucene/src/java/org/apache/lucene/analysis/standard/ClassicTokenizer.java?ds=sidebyside diff --git a/lucene-java-3.5.0/lucene/src/java/org/apache/lucene/analysis/standard/ClassicTokenizer.java b/lucene-java-3.5.0/lucene/src/java/org/apache/lucene/analysis/standard/ClassicTokenizer.java new file mode 100644 index 0000000..eb4c993 --- /dev/null +++ b/lucene-java-3.5.0/lucene/src/java/org/apache/lucene/analysis/standard/ClassicTokenizer.java @@ -0,0 +1,234 @@ +/** + * 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.standard; + +import java.io.IOException; +import java.io.Reader; + +import org.apache.lucene.analysis.Tokenizer; +import org.apache.lucene.analysis.tokenattributes.OffsetAttribute; +import org.apache.lucene.analysis.tokenattributes.PositionIncrementAttribute; +import org.apache.lucene.analysis.tokenattributes.CharTermAttribute; +import org.apache.lucene.analysis.tokenattributes.TypeAttribute; +import org.apache.lucene.util.AttributeSource; +import org.apache.lucene.util.Version; + +/** A grammar-based tokenizer constructed with JFlex + * + *
This should be a good tokenizer for most European-language documents: + * + *
Many applications have specific tokenizer needs. If this tokenizer does + * not suit your application, please consider copying this source code + * directory to your project and maintaining your own grammar-based tokenizer. + * + * + *
You must specify the required {@link Version} + * compatibility when creating ClassicAnalyzer: + *
input
to the newly created JFlex scanner.
+ *
+ * @param input The input reader
+ *
+ * See http://issues.apache.org/jira/browse/LUCENE-1068
+ */
+ public ClassicTokenizer(Version matchVersion, Reader input) {
+ super();
+ init(input, matchVersion);
+ }
+
+ /**
+ * Creates a new ClassicTokenizer with a given {@link AttributeSource}.
+ */
+ public ClassicTokenizer(Version matchVersion, AttributeSource source, Reader input) {
+ super(source);
+ init(input, matchVersion);
+ }
+
+ /**
+ * Creates a new ClassicTokenizer with a given {@link org.apache.lucene.util.AttributeSource.AttributeFactory}
+ */
+ public ClassicTokenizer(Version matchVersion, AttributeFactory factory, Reader input) {
+ super(factory);
+ init(input, matchVersion);
+ }
+
+ private final void init(Reader input, Version matchVersion) {
+ this.scanner = new ClassicTokenizerImpl(input);
+
+ if (matchVersion.onOrAfter(Version.LUCENE_24)) {
+ replaceInvalidAcronym = true;
+ } else {
+ replaceInvalidAcronym = false;
+ }
+ this.input = input;
+ }
+
+ // this tokenizer generates three attributes:
+ // term offset, positionIncrement and type
+ private final CharTermAttribute termAtt = addAttribute(CharTermAttribute.class);
+ private final OffsetAttribute offsetAtt = addAttribute(OffsetAttribute.class);
+ private final PositionIncrementAttribute posIncrAtt = addAttribute(PositionIncrementAttribute.class);
+ private final TypeAttribute typeAtt = addAttribute(TypeAttribute.class);
+
+ /*
+ * (non-Javadoc)
+ *
+ * @see org.apache.lucene.analysis.TokenStream#next()
+ */
+ @Override
+ public final boolean incrementToken() throws IOException {
+ clearAttributes();
+ int posIncr = 1;
+
+ while(true) {
+ int tokenType = scanner.getNextToken();
+
+ if (tokenType == StandardTokenizerInterface.YYEOF) {
+ return false;
+ }
+
+ if (scanner.yylength() <= maxTokenLength) {
+ posIncrAtt.setPositionIncrement(posIncr);
+ scanner.getText(termAtt);
+ final int start = scanner.yychar();
+ offsetAtt.setOffset(correctOffset(start), correctOffset(start+termAtt.length()));
+ // This 'if' should be removed in the next release. For now, it converts
+ // invalid acronyms to HOST. When removed, only the 'else' part should
+ // remain.
+ if (tokenType == ClassicTokenizer.ACRONYM_DEP) {
+ if (replaceInvalidAcronym) {
+ typeAtt.setType(ClassicTokenizer.TOKEN_TYPES[ClassicTokenizer.HOST]);
+ termAtt.setLength(termAtt.length() - 1); // remove extra '.'
+ } else {
+ typeAtt.setType(ClassicTokenizer.TOKEN_TYPES[ClassicTokenizer.ACRONYM]);
+ }
+ } else {
+ typeAtt.setType(ClassicTokenizer.TOKEN_TYPES[tokenType]);
+ }
+ return true;
+ } else
+ // When we skip a too-long term, we still increment the
+ // position increment
+ posIncr++;
+ }
+ }
+
+ @Override
+ public final void end() {
+ // set final offset
+ int finalOffset = correctOffset(scanner.yychar() + scanner.yylength());
+ offsetAtt.setOffset(finalOffset, finalOffset);
+ }
+
+ @Override
+ public void reset(Reader reader) throws IOException {
+ super.reset(reader);
+ scanner.yyreset(reader);
+ }
+
+ /**
+ * Prior to https://issues.apache.org/jira/browse/LUCENE-1068, ClassicTokenizer mischaracterized as acronyms tokens like www.abc.com
+ * when they should have been labeled as hosts instead.
+ * @return true if ClassicTokenizer now returns these tokens as Hosts, otherwise false
+ *
+ * @deprecated Remove in 3.X and make true the only valid value
+ */
+ @Deprecated
+ public boolean isReplaceInvalidAcronym() {
+ return replaceInvalidAcronym;
+ }
+
+ /**
+ *
+ * @param replaceInvalidAcronym Set to true to replace mischaracterized acronyms as HOST.
+ * @deprecated Remove in 3.X and make true the only valid value
+ *
+ * See https://issues.apache.org/jira/browse/LUCENE-1068
+ */
+ @Deprecated
+ public void setReplaceInvalidAcronym(boolean replaceInvalidAcronym) {
+ this.replaceInvalidAcronym = replaceInvalidAcronym;
+ }
+}