当前位置: 首页>>代码示例>>Java>>正文


Java SynonymFilter类代码示例

本文整理汇总了Java中org.apache.lucene.analysis.synonym.SynonymFilter的典型用法代码示例。如果您正苦于以下问题:Java SynonymFilter类的具体用法?Java SynonymFilter怎么用?Java SynonymFilter使用的例子?那么, 这里精选的类代码示例或许可以为您提供帮助。


SynonymFilter类属于org.apache.lucene.analysis.synonym包,在下文中一共展示了SynonymFilter类的9个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。

示例1: testMaxPosition3WithSynomyms

import org.apache.lucene.analysis.synonym.SynonymFilter; //导入依赖的package包/类
public void testMaxPosition3WithSynomyms() throws IOException {
  for (final boolean consumeAll : new boolean[]{true, false}) {
    MockTokenizer tokenizer = new MockTokenizer(new StringReader("one two three four five"), MockTokenizer.WHITESPACE, false);
    // if we are consuming all tokens, we can use the checks, otherwise we can't
    tokenizer.setEnableChecks(consumeAll);

    SynonymMap.Builder builder = new SynonymMap.Builder(true);
    builder.add(new CharsRef("one"), new CharsRef("first"), true);
    builder.add(new CharsRef("one"), new CharsRef("alpha"), true);
    builder.add(new CharsRef("one"), new CharsRef("beguine"), true);
    CharsRefBuilder multiWordCharsRef = new CharsRefBuilder();
    SynonymMap.Builder.join(new String[]{"and", "indubitably", "single", "only"}, multiWordCharsRef);
    builder.add(new CharsRef("one"), multiWordCharsRef.get(), true);
    SynonymMap.Builder.join(new String[]{"dopple", "ganger"}, multiWordCharsRef);
    builder.add(new CharsRef("two"), multiWordCharsRef.get(), true);
    SynonymMap synonymMap = builder.build();
    TokenStream stream = new SynonymFilter(tokenizer, synonymMap, true);
    stream = new LimitTokenPositionFilter(stream, 3, consumeAll);

    // "only", the 4th word of multi-word synonym "and indubitably single only" is not emitted, since its position is greater than 3.
    assertTokenStreamContents(stream,
        new String[]{"one", "first", "alpha", "beguine", "and", "two", "indubitably", "dopple", "three", "single", "ganger"},
        new int[]{1, 0, 0, 0, 0, 1, 0, 0, 1, 0, 0});
  }
}
 
开发者ID:europeana,项目名称:search,代码行数:26,代码来源:TestLimitTokenPositionFilter.java

示例2: main

import org.apache.lucene.analysis.synonym.SynonymFilter; //导入依赖的package包/类
@SuppressWarnings("resource")
public static void main(String[] args) throws Exception {
    final Tokenizer tok = new WhitespaceTokenizer();
    tok.setReader(new StringReader("dark sea green sea green"));

    final SynonymMap.Builder builder = new SynonymMap.Builder(true);
    addSynonym("dark sea green", "color", builder);
    addSynonym("green", "color", builder);
    addSynonym("dark sea", "color", builder);
    addSynonym("sea green", "color", builder);
    final SynonymMap synMap = builder.build();
    final TokenStream ts = new SynonymFilter(tok, synMap, true);

    final CharTermAttribute termAtt = ts.addAttribute(CharTermAttribute.class);
    final PositionIncrementAttribute posIncrAtt = ts.addAttribute(PositionIncrementAttribute.class);
    final PositionLengthAttribute posLengthAtt = ts.addAttribute(PositionLengthAttribute.class);

    ts.reset();
    int pos = -1;
    while (ts.incrementToken()) {
        pos += posIncrAtt.getPositionIncrement();
        System.out.println("term=" + termAtt + ", pos=" + pos + ", posLen=" + posLengthAtt.getPositionLength());
    }
    ts.end();
    ts.close();
}
 
开发者ID:shaie,项目名称:lucenelab,代码行数:27,代码来源:SynonymFilterExample.java

示例3: testRandomStrings

import org.apache.lucene.analysis.synonym.SynonymFilter; //导入依赖的package包/类
/** blast some random strings through the analyzer */
public void testRandomStrings() throws Exception {
  final int numIters = atLeast(10);
  for (int i = 0; i < numIters; i++) {
    SynonymMap.Builder b = new SynonymMap.Builder(random().nextBoolean());
    final int numEntries = atLeast(10);
    for (int j = 0; j < numEntries; j++) {
      add(b, randomNonEmptyString(), randomNonEmptyString(), random().nextBoolean());
    }
    final SynonymMap map = b.build();
    final boolean ignoreCase = random().nextBoolean();
    
    final Analyzer analyzer = new Analyzer() {
      @Override
      protected TokenStreamComponents createComponents(String fieldName, Reader reader) {
        Tokenizer tokenizer = new MockTokenizer(reader, MockTokenizer.SIMPLE, true);
        TokenStream stream = new SynonymFilter(tokenizer, map, ignoreCase);
        return new TokenStreamComponents(tokenizer, new RemoveDuplicatesTokenFilter(stream));
      }
    };

    checkRandomData(random(), analyzer, 200);
  }
}
 
开发者ID:europeana,项目名称:search,代码行数:25,代码来源:TestRemoveDuplicatesTokenFilter.java

示例4: testMaxPosition3WithSynomyms

import org.apache.lucene.analysis.synonym.SynonymFilter; //导入依赖的package包/类
public void testMaxPosition3WithSynomyms() throws IOException {
  MockTokenizer tokenizer = new MockTokenizer(new StringReader("one two three four five"), MockTokenizer.WHITESPACE, false);
  tokenizer.setEnableChecks(false); // LimitTokenPositionFilter doesn't consume the entire stream that it wraps
  
  SynonymMap.Builder builder = new SynonymMap.Builder(true);
  builder.add(new CharsRef("one"), new CharsRef("first"), true);
  builder.add(new CharsRef("one"), new CharsRef("alpha"), true);
  builder.add(new CharsRef("one"), new CharsRef("beguine"), true);
  CharsRef multiWordCharsRef = new CharsRef();
  SynonymMap.Builder.join(new String[] { "and", "indubitably", "single", "only" }, multiWordCharsRef);
  builder.add(new CharsRef("one"), multiWordCharsRef, true);
  SynonymMap.Builder.join(new String[]{"dopple", "ganger"}, multiWordCharsRef);
  builder.add(new CharsRef("two"), multiWordCharsRef, true);
  SynonymMap synonymMap = builder.build();
  TokenStream stream = new SynonymFilter(tokenizer, synonymMap, true);
  stream = new LimitTokenPositionFilter(stream, 3); // consumeAllTokens defaults to false
  
  // "only", the 4th word of multi-word synonym "and indubitably single only" is not emitted, since its position is greater than 3.
  assertTokenStreamContents(stream, 
      new String[] { "one", "first", "alpha", "beguine", "and", "two", "indubitably", "dopple", "three", "single", "ganger" },
      new int[]    {     1,       0,       0,         0,    0,     1,              0,        0,       1,       0,         0 });
  
}
 
开发者ID:jimaguere,项目名称:Maskana-Gestor-de-Conocimiento,代码行数:24,代码来源:TestLimitTokenPositionFilter.java

示例5: createAnalyzers

import org.apache.lucene.analysis.synonym.SynonymFilter; //导入依赖的package包/类
@Before
public void createAnalyzers() throws Exception {
   queryAnalyzer = new Analyzer() {
      @Override
      protected TokenStreamComponents createComponents(String fieldName) {
         // White space tokenizer, to lower case tokenizer.
         return new TokenStreamComponents(new MockTokenizer());
      }
   };

   SynonymMap.Builder builder = new SynonymMap.Builder(true);
   builder.add(new CharsRef("test"), new CharsRef("synonym1"), false);
   builder.add(new CharsRef("test"), new CharsRef("synonym2"), false);
   final SynonymMap synonyms = builder.build();

   synonymAnalyzer = new Analyzer() {
      @Override
      protected TokenStreamComponents createComponents(String fieldName) {
         // White space tokenizer, to lower case tokenizer.
         MockTokenizer tokenizer = new MockTokenizer();
         // Filter for adding synonyms
         TokenStream result = new SynonymFilter(tokenizer, synonyms, true);
         // Filter all non-synonyms, because the synonym filter outputs the
         // original token too.
         result = new TypeTokenFilter(result, Collections.singleton(SynonymFilter.TYPE_SYNONYM),
               true);
         return new TokenStreamComponents(tokenizer, result);
      }
   };
}
 
开发者ID:renekrie,项目名称:querqy,代码行数:31,代码来源:AnalyzingQuerqyParserTest.java

示例6: testSynonyms

import org.apache.lucene.analysis.synonym.SynonymFilter; //导入依赖的package包/类
/** test that we can parse and use the solr syn file */
public void testSynonyms() throws Exception {
  SynonymFilterFactory factory = new SynonymFilterFactory();
  Map<String,String> args = new HashMap<String,String>();
  args.put("synonyms", "synonyms.txt");
  factory.setLuceneMatchVersion(TEST_VERSION_CURRENT);
  factory.init(args);
  factory.inform(new ClasspathResourceLoader(getClass()));
  TokenStream ts = factory.create(new MockTokenizer(new StringReader("GB"), MockTokenizer.WHITESPACE, false));
  assertTrue(ts instanceof SynonymFilter);
  assertTokenStreamContents(ts, 
      new String[] { "GB", "gib", "gigabyte", "gigabytes" },
      new int[] { 1, 0, 0, 0 });
}
 
开发者ID:pkarmstr,项目名称:NYBC,代码行数:15,代码来源:TestSynonymFilterFactory.java

示例7: create

import org.apache.lucene.analysis.synonym.SynonymFilter; //导入依赖的package包/类
@Override
public TokenStream create(TokenStream tokenStream) {
    // fst is null means no synonyms
    return synonymMap.fst == null ? tokenStream : new SynonymFilter(tokenStream, synonymMap, ignoreCase);
}
 
开发者ID:justor,项目名称:elasticsearch_my,代码行数:6,代码来源:SynonymTokenFilterFactory.java

示例8: create

import org.apache.lucene.analysis.synonym.SynonymFilter; //导入依赖的package包/类
@Override
public TokenStream create(TokenStream input) {
  // if the fst is null, it means there's actually no synonyms... just return the original stream
  // as there is nothing to do here.
  return map.fst == null ? input : new SynonymFilter(input, map, ignoreCase);
}
 
开发者ID:lamsfoundation,项目名称:lams,代码行数:7,代码来源:FSTSynonymFilterFactory.java

示例9: create

import org.apache.lucene.analysis.synonym.SynonymFilter; //导入依赖的package包/类
@Override
public TokenStream create(TokenStream tokenStream) {
    return synonymMap.fst == null ? tokenStream : new SynonymFilter(tokenStream, synonymMap, true);
}
 
开发者ID:jianfengye,项目名称:elasticsearch-synonym-remote,代码行数:5,代码来源:FileRemoteSynonymTokenFilterFactory.java


注:本文中的org.apache.lucene.analysis.synonym.SynonymFilter类示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。