/*
 * This is sample code to show how to use XML-RPC to access the
 * biological text process tools at:
 * http://bionlp.stanford.edu/
 * 
 * See the web page for more information.
 * 
 * This script requires Java with the Apache XML-RPC package
 * installed, and an internet connection.  It will not work through a
 * firewall that blocks outgoing HTTP connections.  It has been tested
 * on Solaris and Linux and should work on other Unices.  It may work
 * on Windows as well -- please let me know if it does.  To run the
 * script, type at the command prompt:
 * 
 * > javac BioNLP.java       # To compile it.
 * > java BioNLP [optional text]
 *  
 * where [optional text] is some text to search.  If not provided, I
 * will search some sample text as a demonstration.
 * 
 * History:
 * 2003-04-15  jtc  Initial release.
 */

import java.util.*;
import org.apache.xmlrpc.XmlRpcClient;


public class BioNLP
{
    /* This should point to the XML-RPC server at bionlp.stanford.edu. */
    public static final String BIONLP_URI = new String("http://bionlp.stanford.edu/xmlrpc");

    /* This is the default text to search, if no inputs are given. */
    public static final String SAMPLE_TEXT = new String("We observed an increase in mitogen-activated protein kinase (MAPK) activity.");

    public static void main(String args[])
    {
	String text = new String();

	for(int i=0; i<args.length; i++) {
	    text = text + " " + args[i];
	}
	text = text.trim();
	if(text.length() == 0) {
	    text = SAMPLE_TEXT;
	}

	try {
	    System.out.println("Searching text:");
	    System.out.println(text);
	    System.out.println();

	    Vector abbreviations = findAbbreviations(text);
	    System.out.println("I found " + abbreviations.size() + 
			       " possible abbreviation(s).");
	    for(Enumeration e=abbreviations.elements(); e.hasMoreElements();){
		Vector data = (Vector)e.nextElement();
		System.out.println("ABBREVIATION=" + data.elementAt(1));
		System.out.println("LONG FORM=" + data.elementAt(0));
		System.out.println("SCORE=" + data.elementAt(2));
		System.out.println();
	    }
	    
	    Vector names = findGeneAndProteinNames(text);
	    System.out.println("I found " + names.size() + 
			       " possible gene or protein name(s).");
	    for(Enumeration e=names.elements(); e.hasMoreElements();){
		Vector data = (Vector)e.nextElement();
		System.out.println("NAME=" + data.elementAt(0));
		System.out.println("SCORE=" + data.elementAt(3));
		System.out.println();
	    }
	}
	catch(Exception e) {
	    e.printStackTrace();
	}

    }

    /**
     * Look for abbreviations that are defined in the text.  This
     * function takes some text as a string and returns a Vector of of
     * the abbreviations found.  Each abbreviation found is a Vector
     * of (long form, abbreviation, score).  Returns an empty Vector
     * if no abbreviations are found.
     *
     * @param   text  a String of the text to search.
     * @return        a Vector of the abbreviations found.
     */
    public static Vector findAbbreviations(String text) throws 
	org.apache.xmlrpc.XmlRpcException, java.io.IOException 
    {
	XmlRpcClient server = new XmlRpcClient(BIONLP_URI);
	Vector params = new Vector();
	params.addElement(text);
	Vector abbreviations = 
	    (Vector)server.execute("find_abbreviations", params);
	return abbreviations;
    }

    /**
     * Look for gene and protein names in the text.  This function
     * takes some text as a string and returns a Vector of of the
     * names found.  Each name found is a Vector of (name, start, end,
     * score).  Returns an empty Vector if nothing found.
     *
     * @param   text  a String of the text to search.
     * @return        a Vector of the gene and protein names found.
     */
    public static Vector findGeneAndProteinNames(String text) throws 
	org.apache.xmlrpc.XmlRpcException, java.io.IOException 
    {
	XmlRpcClient server = new XmlRpcClient(BIONLP_URI);
	Vector params = new Vector();
	params.addElement(text);
	Vector names = 
	    (Vector)server.execute("find_gene_and_protein_names", params);
	return names;
    }


}
