Package gannuWSD.algorithms

Source Code of gannuWSD.algorithms.SupervisedWSD

package gannuWSD.algorithms;

import gannuCF.classifiers.Classifier;
import gannuNLP.data.AmbiguousWord;
import gannuNLP.data.Input;
import gannuNLP.data.Sense;
import gannuNLP.dictionaries.Dictionary;
import gannuUtil.Util;
import gannuWSD.testing.Decision;

import java.io.File;
import java.util.ArrayList;

/**
*
* Class for using supervised WSD with our own implementation of classifiers.
* Set the classifier parameter with a string in the format "classifier:gannu.classifier.className;".
* @author Francisco Viveros-Jiménez
*
*/
public class SupervisedWSD extends WSDAlgorithm{
 
  public SupervisedWSD()
  {
    super();
    this.name="SupervisedWSD";
  }

  @Override
  public void init(Input document) throws Exception {
    // TODO Auto-generated method stub   
    System.out.println("");
  }
 
 
  @Override
  public Decision disambiguate(AmbiguousWord target,
      ArrayList<AmbiguousWord> window) throws Exception {
    System.out.print(".");
    Decision decision=new Decision(target,window);
    // TODO Auto-generated method stub
    if(target.getSenses().size()>1)
    {
      Classifier c;
      if(this.getValue("classifier")==null)
        this.addParameters("classifier:gannuCF.classifiers.NaiveBayes;");
      File f=new File("./data/classifiers/"+this.dict.getCompleteName()+"/"+Dictionary.normalizeLemmaforFile(target.getLemma())+"_"+this.getValue("classifier")+".gcl");
      if(f.exists())
      {
        c=(Classifier)Util.loadObject(f);
      }
      else
      {
       
        c=(Classifier)Class.forName(this.getValue("classifier")).newInstance();
        c.addParameters(this.getParameterString());
        c.train(target);
        File dir=new File("./data/classifiers/"+this.dict.getCompleteName()+"/");
        if(!dir.exists())
          dir.mkdirs();
        Util.writeObject(f, c);
      }
      ArrayList<String> sample=new ArrayList<String>(window.size());     
      for(AmbiguousWord word:window)
      {
        sample.add(word.getLemma());       
      }
      float w[]=c.classify(sample);
      for(int j=0;j<target.getSenses().size();j++)
      {
        ArrayList<String> dwords=new ArrayList<String>(window.size());
        for(AmbiguousWord word:window)
        {
          if(this.overlap(target.getSenses().get(j), word.getLemma()))
            if(!dwords.contains(word.getLemma()))
              dwords.add(word.getLemma());
        }
        dwords.trimToSize();
        decision.setSense(j, w[j], dwords);
      }

    }
    else
    {
      decision.setSense(0, 1.0, new ArrayList<String>());
    }
    decision.calculateAnswer();
    return decision;
  }

  @Override
  public boolean IsUseful(AmbiguousWord target, AmbiguousWord windowWord)
  throws Exception {
    for(Sense s:target.getSenses())
      for(String word:s.getBagOfWords())
        if(word.equals(windowWord.getLemma()))
          return true;
    return false;
  }
}
TOP

Related Classes of gannuWSD.algorithms.SupervisedWSD

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.