package gannuWSD.testing;
import gannuNLP.data.Input;
import gannuNLP.dictionaries.Dictionary;
import gannuUtil.KeyString;
import gannuWSD.algorithms.WSDAlgorithm;
import gannuWSD.windowfilters.WindowFilter;
import java.io.BufferedWriter;
import java.io.File;
import java.util.ArrayList;
/**
* Class for setting a test of a disambiguation method over a set of .sgf documents.
* @author Francisco Viveros-Jiménez
*
*/
public class Test {
/**
* Configuration strings of all the selected algorithms.
*/
ArrayList<KeyString> configurations;
/**
* Base dictionary
*/
Dictionary dict;
/**
* The set of sgf documents used as test-bed.
*/
ArrayList<Input> docs;
/**
* Summaries of the test results obtained in each document.
*/
ArrayList<Summary> summaries;
/**
* Answers obtained in each document.
*/
ArrayList<ArrayList<Decision>> answers;
/**
* Algorithm to be tested.
*/
WSDAlgorithm algorithm;
/**
* Disambiguation algorithm used as a back-off strategy.
* Use null to indicate no back-off.
*/
WSDAlgorithm backoff;
/**
* Name of the folder/file containing the tests.
*/
String testSet;
/**
* Disambiguation algorithm used for solving ties.
* Use null for leaving the ties unsolved.
*/
WSDAlgorithm tie;
/**
* SenseFilter used if any.
*/
String retrievedSenses;
/**
* Samples loaded.
*/
String KNSources;
/**
* Returns the test-bed used on this test.
* @return docs
*/
public ArrayList<Input> getDocs() {
return docs;
}
/**
* Returns the summaries of the test runs.
* @return summaries
*/
public ArrayList<Summary> getSummaries() {
return summaries;
}
/**
* Returns the decisions made on the test runs.
* @return answers
*/
public ArrayList<ArrayList<Decision>> getAnswers() {
return answers;
}
/**
* Returns the disambiguation algorithm used on this test.
* @return algorithm
*/
public WSDAlgorithm getAlgorithm() {
return algorithm;
}
/**
* Returns the back-off strategy used on this test.
* @return backoff
*/
public WSDAlgorithm getBackoff() {
return backoff;
}
/**
* Run the current test.
* @return The decisions made by the disambiguation method over the test-bed.
*/
public void run(String fileName,String summary, String detail, BufferedWriter out)throws Exception
{
for(WindowFilter filter:this.algorithm.getWindowFilters())
{
filter.setParentWSD(algorithm);
}
this.algorithm.setParameters(this.configurations.get(this.configurations.indexOf(new KeyString("wsd"))).getString());
if(this.tie!=null)
{
for(WindowFilter filter:this.tie.getWindowFilters())
{
filter.setParentWSD(tie);
}
int index=this.configurations.indexOf(new KeyString("tie"));
if(index>=0)
this.tie.setParameters(this.configurations.get(index).getString());
}
if(this.backoff!=null)
{
for(WindowFilter filter:this.backoff.getWindowFilters())
{
filter.setParentWSD(backoff);
}
int index=this.configurations.indexOf(new KeyString("backoff"));
if(index>=0)
this.backoff.setParameters(this.configurations.get(index).getString());
}
out.write("Test of algorithm "+this.algorithm
+ " with backoff "+this.backoff
+ " and over "+this.testSet+"\n");
System.out.println("Test started for algorithm "+this.algorithm
+ " with backoff "+this.backoff
+ " and over "+this.testSet);
this.answers=new ArrayList<ArrayList<Decision>>(this.docs.size());
this.summaries=new ArrayList<Summary>(this.docs.size());
long start=System.nanoTime();
for(Input doc:docs)
{
ArrayList<Decision> decisions;
if(this.algorithm.getValue("windowSize").equals("text")||this.algorithm.getValue("windowSize").equals("sentence"))
decisions=this.algorithm.solveMultiple(doc, backoff, tie, dict);
else
decisions=this.algorithm.solve(doc, backoff, tie, this.dict);
this.answers.add(decisions);
this.summaries.add(new Summary(decisions));
}
ArrayList<Test> ts=new ArrayList<Test>(2);
ts.add(this);
System.out.println("\n***Finished;");
long end=System.nanoTime();
out.write("|||p="+String.valueOf(Measures.Precision(this.summaries)[4])+"\n");
out.write("|||r="+String.valueOf(Measures.Recall(summaries)[4])+"\n");
out.write("|||c="+String.valueOf(Measures.Coverage(summaries)[4])+"\n");
out.write("|||f1="+String.valueOf(Measures.Fmeasure(summaries)[4])+"\n");
out.write("|||t="+String.valueOf(end-start)+"\n\n");
System.out.println("|||p="+String.valueOf(Measures.Precision(this.summaries)[4]));
System.out.println("|||r="+String.valueOf(Measures.Recall(summaries)[4]));
System.out.println("|||c="+String.valueOf(Measures.Coverage(summaries)[4]));
System.out.println("|||F1="+String.valueOf(Measures.Fmeasure(summaries)[4]));
System.out.println("|||Time="+String.valueOf(end-start));
XLSWriter.writeXLS(new File(fileName),ts, summary, detail,end-start);
//Free some memory
for(ArrayList<Decision> ds:this.answers)
{
for(Decision d:ds)
{
d.getDecisionWords().clear();
}
}
}
/**
* Returns the sample sets loaded.
* @return KNSources
*/
public String getKNSources() {
return this.KNSources;
}
/**
* Returns the pruning method used.
* @return retrievedSenses
*/
public String getRetrievedSenses() {
return this.retrievedSenses;
}
/**
* Returns the algorithm used for solving ties.
* @return tie
*/
public WSDAlgorithm getTie() {
return this.tie;
}
/**
* Creates a new test.
* @param docs The SEMCOR documents used as test-bed.
* @param algorithm Disambiguation method to test to.
* @param windowSize Window size to be used by the windowing method.
* @param backoff Disambiguation algorithm to be used as a back-off strategy. Use null to indicate no back-off.
* @param testSet Name of the folder/file containing the tests.
* @param tie Tie solving strategy.
* @param filters Conditions for retrieving window words from context.
* @param retrievedSenses Pruning method used.
* @param KNSources Loaded samples.
* @param dict Source dictionary.
*/
public Test(ArrayList<Input> docs, WSDAlgorithm algorithm,
WSDAlgorithm backoff,
WSDAlgorithm tie, String testSet,
String retrievedSenses, String KNSources, Dictionary dict) {
super();
this.dict=dict;
this.docs = docs;
this.algorithm = algorithm;
this.backoff = backoff;
this.testSet = testSet;
this.tie = tie;
this.retrievedSenses = retrievedSenses;
this.KNSources = KNSources;
this.configurations=new ArrayList<KeyString>();
}
/**
*
* @return this.configurations
*/
public ArrayList<KeyString> getConfigurations() {
return configurations;
}
/**
*
* @param configurations Change the current configuration strings of all the WSDAlgorithms in the chain.
*/
public void setConfigurations(ArrayList<KeyString> configurations) {
this.configurations = configurations;
}
/**
*
* @return dict
*/
public Dictionary getDictionary()
{
return this.dict;
}
}