當前位置: 首頁>>代碼示例>>Java>>正文


Java TokenStream類代碼示例

本文整理匯總了Java中org.apache.lucene.analysis.TokenStream的典型用法代碼示例。如果您正苦於以下問題:Java TokenStream類的具體用法?Java TokenStream怎麽用?Java TokenStream使用的例子?那麽, 這裏精選的類代碼示例或許可以為您提供幫助。


TokenStream類屬於org.apache.lucene.analysis包,在下文中一共展示了TokenStream類的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Java代碼示例。

示例1: assertCollation

import org.apache.lucene.analysis.TokenStream; //導入依賴的package包/類
private void assertCollation(TokenStream stream1, TokenStream stream2, int comparison) throws IOException {
    CharTermAttribute term1 = stream1.addAttribute(CharTermAttribute.class);
    CharTermAttribute term2 = stream2.addAttribute(CharTermAttribute.class);

    stream1.reset();
    stream2.reset();

    assertThat(stream1.incrementToken(), equalTo(true));
    assertThat(stream2.incrementToken(), equalTo(true));
    assertThat(Integer.signum(term1.toString().compareTo(term2.toString())), equalTo(Integer.signum(comparison)));
    assertThat(stream1.incrementToken(), equalTo(false));
    assertThat(stream2.incrementToken(), equalTo(false));

    stream1.end();
    stream2.end();

    stream1.close();
    stream2.close();
}
 
開發者ID:justor,項目名稱:elasticsearch_my,代碼行數:20,代碼來源:SimpleIcuCollationTokenFilterTests.java

示例2: after

import org.apache.lucene.analysis.TokenStream; //導入依賴的package包/類
@After
public void after(){

    if(analyzer != null){
        try {
            TokenStream ts = analyzer.tokenStream("field", text);
            CharTermAttribute ch = ts.addAttribute(CharTermAttribute.class);
            ts.reset();
            int i = 0;
            while (ts.incrementToken()) {
                i++;
                System.out.print(ch.toString() + "\t");
                if(i % 7 == 0){
                    System.out.println();
                }
            }
            ts.end();
            ts.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
 
開發者ID:followwwind,項目名稱:apache,代碼行數:24,代碼來源:AnalyzerTest.java

示例3: splitStringIntoTerms

import org.apache.lucene.analysis.TokenStream; //導入依賴的package包/類
private String[] splitStringIntoTerms(String value) {
    try {
        List<String> results = new ArrayList<>();
        try (TokenStream tokens = analyzer.tokenStream("", value)) {
            CharTermAttribute term = tokens.getAttribute(CharTermAttribute.class);
            tokens.reset();
            while (tokens.incrementToken()) {
                String t = term.toString().trim();
                if (t.length() > 0) {
                    results.add(t);
                }
            }
        }
        return results.toArray(new String[results.size()]);
    } catch (IOException e) {
        throw new MemgraphException("Could not tokenize string: " + value, e);
    }
}
 
開發者ID:mware-solutions,項目名稱:memory-graph,代碼行數:19,代碼來源:ElasticsearchSearchQueryBase.java

示例4: PrefixAwareTokenFilter

import org.apache.lucene.analysis.TokenStream; //導入依賴的package包/類
public PrefixAwareTokenFilter(TokenStream prefix, TokenStream suffix) {
  super(suffix);
  this.suffix = suffix;
  this.prefix = prefix;
  prefixExhausted = false;
  
  termAtt = addAttribute(CharTermAttribute.class);
  posIncrAtt = addAttribute(PositionIncrementAttribute.class);
  payloadAtt = addAttribute(PayloadAttribute.class);
  offsetAtt = addAttribute(OffsetAttribute.class);
  typeAtt = addAttribute(TypeAttribute.class);
  flagsAtt = addAttribute(FlagsAttribute.class);

  p_termAtt = prefix.addAttribute(CharTermAttribute.class);
  p_posIncrAtt = prefix.addAttribute(PositionIncrementAttribute.class);
  p_payloadAtt = prefix.addAttribute(PayloadAttribute.class);
  p_offsetAtt = prefix.addAttribute(OffsetAttribute.class);
  p_typeAtt = prefix.addAttribute(TypeAttribute.class);
  p_flagsAtt = prefix.addAttribute(FlagsAttribute.class);
}
 
開發者ID:lamsfoundation,項目名稱:lams,代碼行數:21,代碼來源:PrefixAwareTokenFilter.java

示例5: getFilter

import org.apache.lucene.analysis.TokenStream; //導入依賴的package包/類
@Override
public Filter getFilter(Element e) throws ParserException {
  List<BytesRef> terms = new ArrayList<>();
  String text = DOMUtils.getNonBlankTextOrFail(e);
  String fieldName = DOMUtils.getAttributeWithInheritanceOrFail(e, "fieldName");

  TokenStream ts = null;
  try {
    ts = analyzer.tokenStream(fieldName, text);
    TermToBytesRefAttribute termAtt = ts.addAttribute(TermToBytesRefAttribute.class);
    BytesRef bytes = termAtt.getBytesRef();
    ts.reset();
    while (ts.incrementToken()) {
      termAtt.fillBytesRef();
      terms.add(BytesRef.deepCopyOf(bytes));
    }
    ts.end();
  }
  catch (IOException ioe) {
    throw new RuntimeException("Error constructing terms from index:" + ioe);
  } finally {
    IOUtils.closeWhileHandlingException(ts);
  }
  return new TermsFilter(fieldName, terms);
}
 
開發者ID:lamsfoundation,項目名稱:lams,代碼行數:26,代碼來源:TermsFilterBuilder.java

示例6: findGoodEndForNoHighlightExcerpt

import org.apache.lucene.analysis.TokenStream; //導入依賴的package包/類
private static int findGoodEndForNoHighlightExcerpt(int noMatchSize, Analyzer analyzer, String fieldName, String contents)
        throws IOException {
    try (TokenStream tokenStream = analyzer.tokenStream(fieldName, contents)) {
        if (!tokenStream.hasAttribute(OffsetAttribute.class)) {
            // Can't split on term boundaries without offsets
            return -1;
        }
        int end = -1;
        tokenStream.reset();
        while (tokenStream.incrementToken()) {
            OffsetAttribute attr = tokenStream.getAttribute(OffsetAttribute.class);
            if (attr.endOffset() >= noMatchSize) {
                // Jump to the end of this token if it wouldn't put us past the boundary
                if (attr.endOffset() == noMatchSize) {
                    end = noMatchSize;
                }
                return end;
            }
            end = attr.endOffset();
        }
        tokenStream.end();
        // We've exhausted the token stream so we should just highlight everything.
        return end;
    }
}
 
開發者ID:justor,項目名稱:elasticsearch_my,代碼行數:26,代碼來源:PlainHighlighter.java

示例7: lemmatize

import org.apache.lucene.analysis.TokenStream; //導入依賴的package包/類
protected String lemmatize(String query) {
	ItalianAnalyzer analyzer = new ItalianAnalyzer();
	TokenStream tokenStream = analyzer.tokenStream("label", query);
	
	
	StringBuilder sb = new StringBuilder();
	CharTermAttribute token = tokenStream.getAttribute(CharTermAttribute.class);
	try {
    	tokenStream.reset();
		while (tokenStream.incrementToken()) {
		    if (sb.length() > 0) {
		        sb.append(" ");
		    }
		    sb.append(token.toString());
		}
	} catch (IOException e) {
		// TODO Auto-generated catch block
		e.printStackTrace();
	}
	
	return sb.toString();
}
 
開發者ID:teamdigitale,項目名稱:ontonethub,代碼行數:23,代碼來源:AbstractIndexingJob.java

示例8: Field

import org.apache.lucene.analysis.TokenStream; //導入依賴的package包/類
/**
 * Create field with TokenStream value.
 * @param name field name
 * @param tokenStream TokenStream value
 * @param type field type
 * @throws IllegalArgumentException if either the name or type
 *         is null, or if the field's type is stored(), or
 *         if tokenized() is false, or if indexed() is false.
 * @throws NullPointerException if the tokenStream is null
 */
public Field(String name, TokenStream tokenStream, FieldType type) {
  if (name == null) {
    throw new IllegalArgumentException("name cannot be null");
  }
  if (tokenStream == null) {
    throw new NullPointerException("tokenStream cannot be null");
  }
  if (!type.indexed() || !type.tokenized()) {
    throw new IllegalArgumentException("TokenStream fields must be indexed and tokenized");
  }
  if (type.stored()) {
    throw new IllegalArgumentException("TokenStream fields cannot be stored");
  }
  
  this.name = name;
  this.fieldsData = null;
  this.tokenStream = tokenStream;
  this.type = type;
}
 
開發者ID:lamsfoundation,項目名稱:lams,代碼行數:30,代碼來源:Field.java

示例9: parseQueryString

import org.apache.lucene.analysis.TokenStream; //導入依賴的package包/類
private final Query parseQueryString(ExtendedCommonTermsQuery query, String queryString, String field, QueryParseContext parseContext,
        Analyzer analyzer, String lowFreqMinimumShouldMatch, String highFreqMinimumShouldMatch) throws IOException {
    // Logic similar to QueryParser#getFieldQuery
    int count = 0;
    try (TokenStream source = analyzer.tokenStream(field, queryString.toString())) {
        source.reset();
        CharTermAttribute termAtt = source.addAttribute(CharTermAttribute.class);
        BytesRefBuilder builder = new BytesRefBuilder();
        while (source.incrementToken()) {
            // UTF-8
            builder.copyChars(termAtt);
            query.add(new Term(field, builder.toBytesRef()));
            count++;
        }
    }

    if (count == 0) {
        return null;
    }
    query.setLowFreqMinimumNumberShouldMatch(lowFreqMinimumShouldMatch);
    query.setHighFreqMinimumNumberShouldMatch(highFreqMinimumShouldMatch);
    return query;
}
 
開發者ID:baidu,項目名稱:Elasticsearch,代碼行數:24,代碼來源:CommonTermsQueryParser.java

示例10: analyze

import org.apache.lucene.analysis.TokenStream; //導入依賴的package包/類
private List<String> analyze(Settings settings, String analyzerName, String text) throws IOException {
    IndexSettings idxSettings = IndexSettingsModule.newIndexSettings("test", settings);
    AnalysisModule analysisModule = new AnalysisModule(new Environment(settings), singletonList(new AnalysisPlugin() {
        @Override
        public Map<String, AnalysisProvider<TokenFilterFactory>> getTokenFilters() {
            return singletonMap("myfilter", MyFilterTokenFilterFactory::new);
        }
    }));
    IndexAnalyzers indexAnalyzers = analysisModule.getAnalysisRegistry().build(idxSettings);
    Analyzer analyzer = indexAnalyzers.get(analyzerName).analyzer();

    AllEntries allEntries = new AllEntries();
    allEntries.addText("field1", text, 1.0f);

    TokenStream stream = AllTokenStream.allTokenStream("_all", text, 1.0f, analyzer);
    stream.reset();
    CharTermAttribute termAtt = stream.addAttribute(CharTermAttribute.class);

    List<String> terms = new ArrayList<>();
    while (stream.incrementToken()) {
        String tokText = termAtt.toString();
        terms.add(tokText);
    }
    return terms;
}
 
開發者ID:justor,項目名稱:elasticsearch_my,代碼行數:26,代碼來源:CompoundAnalysisTests.java

示例11: Lucene43CompoundWordTokenFilterBase

import org.apache.lucene.analysis.TokenStream; //導入依賴的package包/類
protected Lucene43CompoundWordTokenFilterBase(TokenStream input, CharArraySet dictionary, int minWordSize, int minSubwordSize, int maxSubwordSize, boolean onlyLongestMatch) {
  super(input);
  this.tokens=new LinkedList<>();
  if (minWordSize < 0) {
    throw new IllegalArgumentException("minWordSize cannot be negative");
  }
  this.minWordSize=minWordSize;
  if (minSubwordSize < 0) {
    throw new IllegalArgumentException("minSubwordSize cannot be negative");
  }
  this.minSubwordSize=minSubwordSize;
  if (maxSubwordSize < 0) {
    throw new IllegalArgumentException("maxSubwordSize cannot be negative");
  }
  this.maxSubwordSize=maxSubwordSize;
  this.onlyLongestMatch=onlyLongestMatch;
  this.dictionary = dictionary;
}
 
開發者ID:lamsfoundation,項目名稱:lams,代碼行數:19,代碼來源:Lucene43CompoundWordTokenFilterBase.java

示例12: extractExtendedAttributes

import org.apache.lucene.analysis.TokenStream; //導入依賴的package包/類
/**
 * other attribute extract object.
 * Extracted object group by AttributeClassName
 *
 * @param stream current TokenStream
 * @param includeAttributes filtering attributes
 * @return Map&lt;key value&gt;
 */
private static Map<String, Object> extractExtendedAttributes(TokenStream stream, final Set<String> includeAttributes) {
    final Map<String, Object> extendedAttributes = new TreeMap<>();

    stream.reflectWith((attClass, key, value) -> {
        if (CharTermAttribute.class.isAssignableFrom(attClass)) {
            return;
        }
        if (PositionIncrementAttribute.class.isAssignableFrom(attClass)) {
            return;
        }
        if (OffsetAttribute.class.isAssignableFrom(attClass)) {
            return;
        }
        if (TypeAttribute.class.isAssignableFrom(attClass)) {
            return;
        }
        if (includeAttributes == null || includeAttributes.isEmpty() || includeAttributes.contains(key.toLowerCase(Locale.ROOT))) {
            if (value instanceof BytesRef) {
                final BytesRef p = (BytesRef) value;
                value = p.toString();
            }
            extendedAttributes.put(key, value);
        }
    });

    return extendedAttributes;
}
 
開發者ID:justor,項目名稱:elasticsearch_my,代碼行數:36,代碼來源:TransportAnalyzeAction.java

示例13: AlternateSpellingFilter

import org.apache.lucene.analysis.TokenStream; //導入依賴的package包/類
public AlternateSpellingFilter(TokenStream tokenStream) {
    super(tokenStream);

    this.previousTokens = new ArrayList<String>();

    this.alternateSpellings = new HashMap<String, String>();
    this.alternateSpellings.put("היא", "הוא");
}
 
開發者ID:Sefaria,項目名稱:Sefaria-ElasticSearch,代碼行數:9,代碼來源:AlternateSpellingFilter.java

示例14: createStackedTokenStream

import org.apache.lucene.analysis.TokenStream; //導入依賴的package包/類
private static TokenStream createStackedTokenStream(String source, CharFilterFactory[] charFilterFactories, TokenizerFactory tokenizerFactory, TokenFilterFactory[] tokenFilterFactories, int current) {
    Reader reader = new FastStringReader(source);
    for (CharFilterFactory charFilterFactory : charFilterFactories) {
        reader = charFilterFactory.create(reader);
    }
    Tokenizer tokenizer = tokenizerFactory.create();
    tokenizer.setReader(reader);
    TokenStream tokenStream = tokenizer;
    for (int i = 0; i < current; i++) {
        tokenStream = tokenFilterFactories[i].create(tokenStream);
    }
    return tokenStream;
}
 
開發者ID:justor,項目名稱:elasticsearch_my,代碼行數:14,代碼來源:TransportAnalyzeAction.java

示例15: testMetaphoneWords

import org.apache.lucene.analysis.TokenStream; //導入依賴的package包/類
@Test
public void testMetaphoneWords() throws Exception {
    Index index = new Index("test", "_na_");
    Settings settings = Settings.builder()
            .put(IndexMetaData.SETTING_VERSION_CREATED, Version.CURRENT)
            .put("index.analysis.filter.myStemmer.type", "br_metaphone")
            .build();

    AnalysisService analysisService = createAnalysisService(index, settings, new AnalysisMetaphonePlugin());

    TokenFilterFactory filterFactory = analysisService.tokenFilter("br_metaphone");

    Tokenizer tokenizer = new KeywordTokenizer();
    
    Map<String,String> words = buildWordList();
    
    Set<String> inputWords = words.keySet();
    for(String word : inputWords) {
        tokenizer.setReader(new StringReader(word));
        TokenStream ts = filterFactory.create(tokenizer);

        CharTermAttribute term1 = ts.addAttribute(CharTermAttribute.class);
        ts.reset();
        assertThat(ts.incrementToken(), equalTo(true));
        assertThat(term1.toString(), equalTo(words.get(word)));
        ts.close();
    }
}
 
開發者ID:anaelcarvalho,項目名稱:elasticsearch-analysis-metaphone_ptBR,代碼行數:29,代碼來源:MetaphoneTokenFilterTests.java


注:本文中的org.apache.lucene.analysis.TokenStream類示例由純淨天空整理自Github/MSDocs等開源代碼及文檔管理平台,相關代碼片段篩選自各路編程大神貢獻的開源項目,源碼版權歸原作者所有,傳播和使用請參考對應項目的License;未經允許,請勿轉載。