Eplebit

Ravendb NGram updated

Feb 28th, 2013
935
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 4.02 KB | None | 0 0
  1. using System;
  2. using System.IO;
  3. using Lucene.Net.Analysis;
  4. using Lucene.Net.Analysis.Standard;
  5. using Lucene.Net.Analysis.Tokenattributes;
  6. using Version = Lucene.Net.Util.Version;
  7.  
  8. namespace Analyzers
  9. {
  10.     public class NGramAnalyzer : Analyzer
  11.     {
  12.         public override TokenStream TokenStream(string fieldName, TextReader reader)
  13.         {
  14.             var tokenizer = new StandardTokenizer(Version.LUCENE_30, reader) {MaxTokenLength = 255};
  15.             TokenStream filter = new StandardFilter(tokenizer);
  16.             filter = new LowerCaseFilter(filter);
  17.             filter = new StopFilter(false, filter, StandardAnalyzer.STOP_WORDS_SET);
  18.             return new NGramTokenFilter(filter, 2, 6);
  19.         }
  20.     }
  21.  
  22.     public class NGramTokenFilter : TokenFilter
  23.     {
  24.         public static int DefaultMinNgramSize = 1;
  25.         public static int DefaultMaxNgramSize = 2;
  26.  
  27.         private readonly int _maxGram;
  28.         private readonly int _minGram;
  29.         private readonly IOffsetAttribute _offsetAtt;
  30.         private readonly ITermAttribute _termAtt;
  31.  
  32.         private int _curGramSize;
  33.         private int _curPos;
  34.         private char[] _curTermBuffer;
  35.         private int _curTermLength;
  36.         private int _tokStart;
  37.  
  38.         /**
  39.              * Creates NGramTokenFilter with given min and max n-grams.
  40.              * <param name="input"><see cref="TokenStream"/> holding the input to be tokenized</param>
  41.              * <param name="minGram">the smallest n-gram to generate</param>
  42.              * <param name="maxGram">the largest n-gram to generate</param>
  43.              */
  44.  
  45.         public NGramTokenFilter(TokenStream input, int minGram, int maxGram)
  46.             : base(input)
  47.         {
  48.             if (minGram < 1)
  49.             {
  50.                 throw new ArgumentException("minGram must be greater than zero");
  51.             }
  52.             if (minGram > maxGram)
  53.             {
  54.                 throw new ArgumentException("minGram must not be greater than maxGram");
  55.             }
  56.             _minGram = minGram;
  57.             _maxGram = maxGram;
  58.  
  59.             _termAtt = AddAttribute<ITermAttribute>();
  60.             _offsetAtt = AddAttribute<IOffsetAttribute>();
  61.         }
  62.  
  63.         /**
  64.              * Creates NGramTokenFilter with default min and max n-grams.
  65.              * <param name="input"><see cref="TokenStream"/> holding the input to be tokenized</param>
  66.              */
  67.  
  68.         public NGramTokenFilter(TokenStream input)
  69.             : this(input, DefaultMinNgramSize, DefaultMaxNgramSize)
  70.         {
  71.         }
  72.  
  73.         /** Returns the next token in the stream, or null at EOS. */
  74.  
  75.         public override bool IncrementToken()
  76.         {
  77.             while (true)
  78.             {
  79.                 if (_curTermBuffer == null)
  80.                 {
  81.                     if (!input.IncrementToken())
  82.                     {
  83.                         return false;
  84.                     }
  85.                     _curTermBuffer = (char[]) _termAtt.TermBuffer().Clone();
  86.                     _curTermLength = _termAtt.TermLength();
  87.                     _curGramSize = _minGram;
  88.                     _curPos = 0;
  89.                     _tokStart = _offsetAtt.StartOffset;
  90.                 }
  91.                 while (_curGramSize <= _maxGram)
  92.                 {
  93.                     while (_curPos + _curGramSize <= _curTermLength)
  94.                     {
  95.                         // while there is input
  96.                         ClearAttributes();
  97.                         _termAtt.SetTermBuffer(_curTermBuffer, _curPos, _curGramSize);
  98.                         _offsetAtt.SetOffset(_tokStart + _curPos, _tokStart + _curPos + _curGramSize);
  99.                         _curPos++;
  100.                         return true;
  101.                     }
  102.                     _curGramSize++; // increase n-gram size
  103.                     _curPos = 0;
  104.                 }
  105.                 _curTermBuffer = null;
  106.             }
  107.         }
  108.  
  109.         public override void Reset()
  110.         {
  111.             base.Reset();
  112.             _curTermBuffer = null;
  113.         }
  114.     }
  115. }
Advertisement
Add Comment
Please, Sign In to add comment