Lucene.NET TokenStream.Next method disappeared

若如初见. 提交于 2020-01-16 20:34:06

问题


I have to update a project using Lucene.NET. It first time I meet this library, and I should update the references to a new version. So I did with Lucene references via a NuGet.

But actually I should in a way update some methods that disappeared in the new versions.

public abstract class AnalyzerView
{
    public abstract string Name { get; }

    public virtual string GetView(TokenStream tokenStream,out int numberOfTokens)
    {
        StringBuilder sb = new StringBuilder();

        Token token = tokenStream.Next();

        numberOfTokens = 0;

        while (token != null)
        {
            numberOfTokens++;
            sb.Append(GetTokenView(token));
            token = tokenStream.Next();
        }

        return sb.ToString();
    }

    protected abstract string GetTokenView(Token token);
}

The documentation is not very clear for me. As I understood I should use something like

while (tokenStream.IncrementToken()) { ... }

but how to obtain that following token is a mystery for me.

I saw similar question on StackOverflow, but my problem is that I need to obtain a Token rather thatn a string, because I have some code that uses tokens, like this one:

public class TermWithOffsetsView : AnalyzerView {
    public override string Name {
        get { return "Terms With Offsets"; }
    }

    protected override string GetTokenView(Token token) {
        return token.TermText() + 
            "   Start: " + token.StartOffset().ToString().PadLeft(5) + 
            "  End: " + token.EndOffset().ToString().PadLeft(5) + 
            "\r\n";
    }
}

回答1:


Finally, after some researches, implemented something like this:

public abstract class AnalyzerView
{
    public abstract string Name { get; }

    public virtual string GetView(TokenStream tokenStream,out int numberOfTokens)
    {
        StringBuilder sb = new StringBuilder();
        numberOfTokens = 0;
        while (tokenStream.IncrementToken())
        {
            numberOfTokens++;
            sb.Append(GetTokenView(tokenStream));
        }

        return sb.ToString();
    }

    protected abstract string GetTokenView(TokenStream tokenStream);
}

with the derived class like this:

public class TermWithOffsetsView : AnalyzerView {
    public override string Name {
        get { return "Terms With Offsets"; }
    }

    protected override string GetTokenView(TokenStream tokenStream) {
        ITermAttribute termAtt = tokenStream.AddAttribute<ITermAttribute>();
        IOffsetAttribute offsetAtt=tokenStream.AddAttribute<IOffsetAttribute>();

        return string.Format("{0}   Start: {1}  End: {2}{3}",
            termAtt.Term,
            offsetAtt.StartOffset.ToString().PadLeft(5),
            offsetAtt.EndOffset.ToString().PadLeft(5),
            Environment.NewLine);
    }
}


来源:https://stackoverflow.com/questions/22907008/lucene-net-tokenstream-next-method-disappeared

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!