home | career | drupal | java | mac | mysql | perl | scala | uml | unix  

Lucene example source code file (NewShingleAnalyzerTask.java)

This example Lucene source code file (NewShingleAnalyzerTask.java) is included in the DevDaily.com "Java Source Code Warehouse" project. The intent of this project is to help you "Learn Java by Example" TM.

Java - Lucene tags/keywords

analyzer, analyzer, class, constructor, exception, newshingleanalyzertask, override, reflection, runtimeexception, shingleanalyzerwrapper, shingleanalyzerwrapper, string, string, stringtokenizer, stringtokenizer, util

The Lucene NewShingleAnalyzerTask.java source code

package org.apache.lucene.benchmark.byTask.tasks;

 * 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,
 * See the License for the specific language governing permissions and
 * limitations under the License.

import java.lang.reflect.Constructor;
import java.util.StringTokenizer;

import org.apache.lucene.analysis.Analyzer;
import org.apache.lucene.analysis.shingle.ShingleAnalyzerWrapper;
import org.apache.lucene.benchmark.byTask.PerfRunData;
import org.apache.lucene.util.Version;

 * Task to support benchmarking ShingleFilter / ShingleAnalyzerWrapper
 * <p>
 * <ul>
 *  <li> NewShingleAnalyzer (constructs with all defaults)
 *  <li> NewShingleAnalyzer(analyzer:o.a.l.analysis.StandardAnalyzer,maxShingleSize:2,outputUnigrams:true)
 * </ul>
 * </p>
public class NewShingleAnalyzerTask extends PerfTask {

  private String analyzerClassName = "standard.StandardAnalyzer";
  private int maxShingleSize = 2;
  private boolean outputUnigrams = true;
  public NewShingleAnalyzerTask(PerfRunData runData) {

  private void setAnalyzer() throws Exception {
    Class<? extends Analyzer> clazz = null;
    Analyzer wrappedAnalyzer;
    try {
      if (analyzerClassName == null || analyzerClassName.equals("")) {
          = "org.apache.lucene.analysis.standard.StandardAnalyzer"; 
      if (analyzerClassName.indexOf(".") == -1 
          || analyzerClassName.startsWith("standard.")) {
        //there is no package name, assume o.a.l.analysis
        analyzerClassName = "org.apache.lucene.analysis." + analyzerClassName;
      clazz = Class.forName(analyzerClassName).asSubclass(Analyzer.class);
      // first try to use a ctor with version parameter (needed for many new 
      // Analyzers that have no default one anymore)
      Constructor<? extends Analyzer> ctor = clazz.getConstructor(Version.class);
      wrappedAnalyzer = ctor.newInstance(Version.LUCENE_CURRENT);
    } catch (NoSuchMethodException e) {
      // otherwise use default ctor
      wrappedAnalyzer = clazz.newInstance();
    ShingleAnalyzerWrapper analyzer 
      = new ShingleAnalyzerWrapper(wrappedAnalyzer, maxShingleSize);
  public int doLogic() throws Exception {
    try {
        ("Changed Analyzer to: ShingleAnalyzerWrapper, wrapping ShingleFilter over" 
         + analyzerClassName);
    } catch (Exception e) {
      throw new RuntimeException("Error creating Analyzer", e);
    return 1;
  public void setParams(String params) {
    StringTokenizer st = new StringTokenizer(params, ",");
    while (st.hasMoreTokens()) {
      String param = st.nextToken();
      StringTokenizer expr = new StringTokenizer(param, ":");
      String key = expr.nextToken();
      String value = expr.nextToken();
      if (key.equalsIgnoreCase("analyzer")) {
        analyzerClassName = value;
      } else if (key.equalsIgnoreCase("outputUnigrams")) {
        outputUnigrams = Boolean.parseBoolean(value);
      } else if (key.equalsIgnoreCase("maxShingleSize")) {
        maxShingleSize = (int)Double.parseDouble(value);
      } else {
        throw new RuntimeException("Unknown parameter " + param);

  public boolean supportsParams() {
    return true;

Other Lucene examples (source code examples)

Here is a short list of links related to this Lucene NewShingleAnalyzerTask.java source code file:

my book on functional programming


new blog posts


Copyright 1998-2019 Alvin Alexander, alvinalexander.com
All Rights Reserved.

A percentage of advertising revenue from
pages under the /java/jwarehouse URI on this website is
paid back to open source projects.