user111
user111

Reputation: 85

how to use the function SynonymAnalyzer in lucene.net

i have used Lucene.Net.SynonymEngine.dll as reference in my application

i have problem using the functions like synonymAnalyzer, ISynonymEngine

i have tried using

SynonymAnalyzer syn = new SynonymAnalyzer(ISynonymEngine engine); and

Analyzer a =new SynonymAnalyzer(ISynonymEngine engine);

but neither seems to work, can anyone help ? thank you in advance...

Upvotes: 1

Views: 1747

Answers (3)

Gonzalo Gallotti
Gonzalo Gallotti

Reputation: 2525

SynonymFilter C# class

public class SynonymFilter : TokenFilter
{
    public ISynonymEngine SynonymEngine { get; private set; }       
    private Queue<string> splittedQueue = new Queue<string>();        
    private readonly ITermAttribute _termAttr;
    private readonly IPositionIncrementAttribute _posAttr;
    private readonly ITypeAttribute _typeAttr;
    private State currentState;

    public SynonymFilter(TokenStream input, ISynonymEngine synonymEngine)
        : base(input)
    {
        if (synonymEngine == null)
            throw new ArgumentNullException("synonymEngine");

        SynonymEngine = synonymEngine;
        _termAttr = AddAttribute<ITermAttribute>();
        _posAttr = AddAttribute<IPositionIncrementAttribute>();
        _typeAttr = AddAttribute<ITypeAttribute>();
    }


    public override bool IncrementToken()
    {
        if (splittedQueue.Count > 0)
        {
            string splitted = splittedQueue.Dequeue();
            RestoreState(currentState);
            _termAttr.SetTermBuffer(splitted);
            _posAttr.PositionIncrement = 0;
            return true;
        }

        if (!input.IncrementToken())
            return false;

        var currentTerm = new string(_termAttr.TermBuffer(), 0, _termAttr.TermLength());            
        IEnumerable<string> synonyms = SynonymEngine.GetSynonyms(currentTerm);

        if (synonyms == null)
        {
            return false;
        }        
        foreach (string syn in synonyms)
        {                
            if (!currentTerm.Equals(syn))
            {
                splittedQueue.Enqueue(syn);
            }
        }            
        return true;
    }
}

Upvotes: 2

Mayank Pratap
Mayank Pratap

Reputation: 185

public class SynonymAnalyzer : Analyzer
{
    public ISynonymEngine SynonymEngine { get; private set; }

    public SynonymAnalyzer(ISynonymEngine engine)
    {
        SynonymEngine = engine;
    }

    public override TokenStream TokenStream
    (string fieldName, System.IO.TextReader reader)
    {
        //create the tokenizer
        TokenStream result = new StandardTokenizer(reader);

        //add in filters
        // first normalize the StandardTokenizer
        result = new StandardFilter(result); 

        // makes sure everything is lower case
        result = new LowerCaseFilter(result);

        // use the default list of Stop Words, provided by the StopAnalyzer class.
        result = new StopFilter(result, StopAnalyzer.ENGLISH_STOP_WORDS); 

        // injects the synonyms. 
        result = new SynonymFilter(result, SynonymEngine); 

        //return the built token stream.
        return result;
    }
}

Upvotes: 3

L.B
L.B

Reputation: 116178

you can create your analyzer like below

SynonymAnalyzer sa = new SynonymAnalyzer(new XmlSynonymEngine(yourXmlFilesPath)); 

But first you should create an xml file for synonyms

<?xml version="1.0" encoding="utf-8" ?>
<synonyms>
  <group>
    <syn>fast</syn>
    <syn>quick</syn>
    <syn>rapid</syn>
  </group>

  <group>
    <syn>slow</syn>
    <syn>decrease</syn>
  </group>

  <group>
    <syn>google</syn>
    <syn>search</syn>
  </group>

  <group>
    <syn>check</syn>
    <syn>lookup</syn>
    <syn>look</syn>
  </group>

</synonyms>

------ EDIT ---------

See the primitive implementation of ISynonymEngine

public class MySynonyms : Lucene.Net.SynonymEngine.ISynonymEngine
{
    public IEnumerable<string> GetSynonyms(string word)
    {
        if (word == "quick") return  new List<string>{"fast"};
        return new List<string>();
    }
}
SynonymAnalyzer sa = new SynonymAnalyzer(new MySynonyms());

Upvotes: 2

Related Questions