## org.apache.lucene.index.memory
Class AnalyzerUtil

    java.lang.Object
      org.apache.lucene.index.memory.AnalyzerUtil

    public class AnalyzerUtil

extends java.lang.Object

Various fulltext analysis utilities avoiding redundant code in several classes.

Method Summary

`static org.apache.lucene.analysis.Analyzer`

`getLoggingAnalyzer(org.apache.lucene.analysis.Analyzer child, java.io.PrintStream log, java.lang.String logName)`
Returns a simple analyzer wrapper that logs all tokens produced by the underlying child analyzer to the given log stream (typically System.err); Otherwise behaves exactly like the child analyzer, delivering the very same tokens; useful for debugging purposes on custom indexing and/or querying.

`static org.apache.lucene.analysis.Analyzer` `getMaxTokenAnalyzer(org.apache.lucene.analysis.Analyzer child, int maxTokens)` Returns an analyzer wrapper that returns at most the first `maxTokens` tokens from the underlying child analyzer, ignoring all remaining tokens.
`static java.lang.String[]` `getMostFrequentTerms(org.apache.lucene.analysis.Analyzer analyzer, java.lang.String text, int limit)` Returns (frequency:term) pairs for the top N distinct terms (aka words), sorted descending by frequency (and ascending by term, if tied).
`static java.lang.String[]` `getParagraphs(java.lang.String text, int limit)` Returns at most the first N paragraphs of the given text.
`static org.apache.lucene.analysis.Analyzer` `getPorterStemmerAnalyzer(org.apache.lucene.analysis.Analyzer child)` Returns an English stemming analyzer that stems tokens from the underlying child analyzer according to the Porter stemming algorithm.
`static java.lang.String[]` `getSentences(java.lang.String text, int limit)` Returns at most the first N sentences of the given text.
`static org.apache.lucene.analysis.Analyzer` `getSynonymAnalyzer(org.apache.lucene.analysis.Analyzer child, SynonymMap synonyms, int maxSynonyms)` Returns an analyzer wrapper that wraps the underlying child analyzer's token stream into a `SynonymTokenFilter`.

`static org.apache.lucene.analysis.Analyzer`

`getTokenCachingAnalyzer(org.apache.lucene.analysis.Analyzer child)`
Returns an analyzer wrapper that caches all tokens generated by the underlying child analyzer's token streams, and delivers those cached tokens on subsequent calls to `tokenStream(String fieldName, Reader reader)` if the fieldName has been seen before, altogether ignoring the Reader parameter on cache lookup.

Methods inherited from class java.lang.Object
`clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait`

Method Detail

### getLoggingAnalyzer

    public static org.apache.lucene.analysis.Analyzer getLoggingAnalyzer(org.apache.lucene.analysis.Analyzer child,
                                                                         java.io.PrintStream log,
                                                                         java.lang.String logName)

  Returns a simple analyzer wrapper that logs all tokens produced by the underlying child analyzer to the given log stream (typically System.err); Otherwise behaves exactly like the child analyzer, delivering the very same tokens; useful for debugging purposes on custom indexing and/or querying.

  Parameters:
    `child` - the underlying child analyzer
    `log` - the print stream to log to (typically System.err)
    `logName` - a name for this logger (typically "log" or similar)
  Returns:
    a logging analyzer

### getMaxTokenAnalyzer

    public static org.apache.lucene.analysis.Analyzer getMaxTokenAnalyzer(org.apache.lucene.analysis.Analyzer child,
                                                                          int maxTokens)

  Returns an analyzer wrapper that returns at most the first `maxTokens` tokens from the underlying child analyzer, ignoring all remaining tokens.

  Parameters:
    `child` - the underlying child analyzer
    `maxTokens` - the maximum number of tokens to return from the underlying analyzer (a value of Integer.MAX_VALUE indicates unlimited)
  Returns:
    an analyzer wrapper

### getPorterStemmerAnalyzer

    public static org.apache.lucene.analysis.Analyzer getPorterStemmerAnalyzer(org.apache.lucene.analysis.Analyzer child)

  Returns an English stemming analyzer that stems tokens from the underlying child analyzer according to the Porter stemming algorithm. The child analyzer must deliver tokens in lower case for the stemmer to work properly.

  Background: Stemming reduces token terms to their linguistic root form e.g. reduces "fishing" and "fishes" to "fish", "family" and "families" to "famili", as well as "complete" and "completion" to "complet". Note that the root form is not necessarily a meaningful word in itself, and that this is not a bug but rather a feature, if you lean back and think about fuzzy word matching for a bit.

  See the Lucene contrib packages for stemmers (and stop words) for German, Russian and many more languages.

  Parameters:
    `child` - the underlying child analyzer
  Returns:
    an analyzer wrapper

### getSynonymAnalyzer

    public static org.apache.lucene.analysis.Analyzer getSynonymAnalyzer(org.apache.lucene.analysis.Analyzer child,
                                                                         SynonymMap synonyms,
                                                                         int maxSynonyms)

  Returns an analyzer wrapper that wraps the underlying child analyzer's token stream into a `SynonymTokenFilter`.

  Parameters:
    `child` - the underlying child analyzer
    `synonyms` - the map used to extract synonyms for terms
    `maxSynonyms` - the maximum number of synonym tokens to return per underlying token word (a value of Integer.MAX_VALUE indicates unlimited)
  Returns:
    a new analyzer

### getTokenCachingAnalyzer

    public static org.apache.lucene.analysis.Analyzer getTokenCachingAnalyzer(org.apache.lucene.analysis.Analyzer child)

  Returns an analyzer wrapper that caches all tokens generated by the underlying child analyzer's token streams, and delivers those cached tokens on subsequent calls to `tokenStream(String fieldName, Reader reader)` if the fieldName has been seen before, altogether ignoring the Reader parameter on cache lookup.

  If Analyzer / TokenFilter chains are expensive in terms of I/O or CPU, such caching can help improve performance if the same document is added to multiple Lucene indexes, because the text analysis phase need not be performed more than once.

  Caveats:

  - Caching the tokens of large Lucene documents can lead to out of memory exceptions.
  - The Token instances delivered by the underlying child analyzer must be immutable.
  - The same caching analyzer instance must not be used for more than one document because the cache is not keyed on the Reader parameter.

  Parameters:
    `child` - the underlying child analyzer
  Returns:
    a new analyzer

### getMostFrequentTerms

    public static java.lang.String[] getMostFrequentTerms(org.apache.lucene.analysis.Analyzer analyzer,
                                                          java.lang.String text,
                                                          int limit)

  Returns (frequency:term) pairs for the top N distinct terms (aka words), sorted descending by frequency (and ascending by term, if tied).

  Example XQuery:

       declare namespace util = "java:org.apache.lucene.index.memory.AnalyzerUtil";
       declare namespace analyzer = "java:org.apache.lucene.index.memory.PatternAnalyzer";

       for $pair in util:get-most-frequent-terms(
          analyzer:EXTENDED_ANALYZER(), doc("samples/shakespeare/othello.xml"), 10)
       return <word word="{substring-after($pair, ':')}" frequency="{substring-before($pair, ':')}"/>

  Parameters:
    `analyzer` - the analyzer to use for splitting text into terms (aka words)
    `text` - the text to analyze
    `limit` - the maximum number of pairs to return; zero indicates "as many as possible".
  Returns:
    an array of (frequency:term) pairs in the form of (freq0:term0, freq1:term1, ..., freqN:termN). Each pair is a single string separated by a ':' delimiter.

### getParagraphs

    public static java.lang.String[] getParagraphs(java.lang.String text,
                                                   int limit)

  Returns at most the first N paragraphs of the given text. Delimiting characters are excluded from the results. Each returned paragraph is whitespace-trimmed via String.trim(), potentially an empty string.

  Parameters:
    `text` - the text to tokenize into paragraphs
    `limit` - the maximum number of paragraphs to return; zero indicates "as many as possible".
  Returns:
    the first N paragraphs

### getSentences

    public static java.lang.String[] getSentences(java.lang.String text,
                                                  int limit)

  Returns at most the first N sentences of the given text. Delimiting characters are excluded from the results. Each returned sentence is whitespace-trimmed via String.trim(), potentially an empty string.

  Parameters:
    `text` - the text to tokenize into sentences
    `limit` - the maximum number of sentences to return; zero indicates "as many as possible".
  Returns:
    the first N sentences

Copyright © 2000-2011 Apache Software Foundation. All Rights Reserved.