public virtual CharsRef Analyze(string text, CharsRef reuse)
{
IOException priorException = null;
TokenStream ts = analyzer.TokenStream("", text);
try
{
var termAtt = ts.AddAttribute<ICharTermAttribute>();
var posIncAtt = ts.AddAttribute<IPositionIncrementAttribute>();
ts.Reset();
reuse.Length = 0;
while (ts.IncrementToken())
{
int length = termAtt.Length;
if (length == 0)
{
throw new System.ArgumentException("term: " + text + " analyzed to a zero-length token");
}
if (posIncAtt.PositionIncrement != 1)
{
throw new System.ArgumentException("term: " + text + " analyzed to a token with posinc != 1");
}
reuse.Grow(reuse.Length + length + 1); // current + word + separator
int end = reuse.Offset + reuse.Length;
if (reuse.Length > 0)
{
reuse.Chars[end++] = SynonymMap.WORD_SEPARATOR;
reuse.Length++;
}
Array.Copy(termAtt.Buffer(), 0, reuse.Chars, end, length);
reuse.Length += length;
}
ts.End();
}
catch (IOException e)
{
priorException = e;
}
finally
{
IOUtils.CloseWhileHandlingException(priorException, ts);
}
if (reuse.Length == 0)
{
throw new System.ArgumentException("term: " + text + " was completely eliminated by analyzer");
}
return reuse;
}
}