Package org.sf.mustru.test

Source Code of org.sf.mustru.test.TestSearch

package org.sf.mustru.test;

import java.io.File;
import java.io.IOException;

import junit.framework.TestCase;

import org.apache.log4j.PropertyConfigurator;
import org.apache.lucene.search.Hits;
import org.apache.lucene.search.Query;
import org.sf.mustru.search.SearchQuery;
import org.sf.mustru.search.SearchQuestion;
import org.sf.mustru.search.SearchTools;
import org.sf.mustru.utils.Constants;
import org.sf.mustru.utils.DbTools;
import org.sf.mustru.utils.StringTools;

import com.aliasi.util.Files;
public class TestSearch extends TestCase
{
  DbTools dbt = null;
 
  protected void setUp()
  { PropertyConfigurator.configure (Constants.LOG4J_FILE);
    Constants.setDBDIR("/big/trec/db");
    Constants.setINDEXDIR("/big/trec/index");
   // Constants.setDBDIR("/home/manuk/mustru/data/db");
   // Constants.setINDEXDIR("/home/manuk/mustru/data/index");
    dbt = new DbTools(); dbt.openEnv(Constants.getDBDIR(), true);
    Constants.setDbt(dbt);
   
  }
   
  //*-- test the expansion of a query
  public void ttestExpandQuery()
  {
   System.out.println("Started testExpandQuery");
   String seQuery = "this is a sample query";
   Query query = null;
   try { query = SearchTools.expandQuery(seQuery); }
   catch (IOException ie) { System.out.println("IO Exception " + ie.getMessage() ); }
   System.out.println(" Original query: " + seQuery);
   System.out.println(" Expanded  query: " + query);
   System.out.println("Finished testExpandQuery");
  }
 
  //*-- test the fetch of similar documents
  public void ttestSimilarDocs()
  {
   System.out.println("Started testSimilarDocs");
   String docKey = "/home/manuk/html/cacr_html/webnet/node4.html";
   Query query = SearchTools.similarDocs(docKey, dbt);
   System.out.println(" Expanded  query: " + query);
   System.out.println("Finished testSimilarDocs");
  }
 
  //*-- test the generation of questions
  public void testBuildQuery()
  {
   System.out.println("Started testBuildQuery");
   SearchQuestion qp = new SearchQuestion();
   String question = "What did Richard Feynman say upon hearing he would receive the Nobel Prize in Physics?";

  // String question = "what is the capital of california?";
   System.out.println("Question: " + question);
   try { System.out.println("Generated query: " + qp.buildQuery(question)); }
   catch (IOException ie) { System.out.println("IO Error: " + ie.getMessage()); }
 
   System.out.println("Ended testBuildQuery");
  }
 
  public void ttestQuery()
  {
   System.out.println("Started testQuery");
   SearchQuery sq = new SearchQuery();
   String query = "money";
   System.out.println("Query: " + query);
   Hits hits = sq.getHits(query); sq.dumpHits(hits, false);
   String[] results = sq.getResults();
   for (int i = 0; i < results.length; i++)
    System.out.println(i + ": " + results[i]);
  
   System.out.println("Ended testQuery");
  }
 
  public void ttestQuestion()
  {
   System.out.println("Started testQuestion");
   SearchQuestion sq = new SearchQuestion();
//  String question = "What did Richard Feynman say upon hearing he would receive the Nobel Prize in Physics?";
   String question = "What is the capital of Italy ?";
   System.out.println("Question: " + question);  
  
   int ranks[] = {0, 1, 2, 3};
   String explanation = sq.explainAnswer(question, ranks);
//  String explanation = sq.explainAnswer(question);
   System.out.println("Exp: " + explanation);
   System.out.println("Ended testQuestion");
  }
 
  public void ttestPassageRetrieval()
  {
   String fileName = null;
   try
   { fileName = "";
     String text = Files.readFromFile(new File("/big/trec/data/trec4/part5/data/FT941-4782.txt"));
     text = StringTools.filterChars(text);
     SearchQuestion sq = new SearchQuestion();
     String question = " How much did Mercury spend on advertising in 1993?";
     sq.buildQuery(question);
     String bestPassage[] = SearchTools.bestPassages(text, sq.getNouns(), sq.getVerbs(), sq.getAdjectives(), sq.getBigrams(), sq.getEntities() );
     System.out.println("Best: " + bestPassage[0]);
     System.out.println("Second best: " + bestPassage[1]);
   }
   catch (IOException ie) { System.err.println("File Error: " + fileName + " " + ie.getMessage()); }
  }
 
 
  public void tearDown()
  {
   dbt.closeEnv();
  }
}
TOP

Related Classes of org.sf.mustru.test.TestSearch

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.