import java.util.List;
import java.util.Map;
import java.util.ArrayList;

import org.apache.thrift.TException;
import org.apache.thrift.protocol.TBinaryProtocol;
import org.apache.thrift.protocol.TProtocol;
import org.apache.thrift.transport.TFramedTransport;
import org.apache.thrift.transport.TSocket;
import org.apache.thrift.transport.TTransport;
  
import edu.illinois.cs.cogcomp.thrift.curator.Curator;
import edu.illinois.cs.cogcomp.thrift.curator.Record;
import edu.illinois.cs.cogcomp.thrift.base.ServiceUnavailableException;
import edu.illinois.cs.cogcomp.thrift.base.AnnotationFailedException;
import edu.illinois.cs.cogcomp.thrift.base.Span;
import edu.illinois.cs.cogcomp.thrift.base.Labeling;
import edu.illinois.cs.cogcomp.thrift.base.Clustering;
import edu.illinois.cs.cogcomp.thrift.base.Forest;
import edu.illinois.cs.cogcomp.thrift.base.Tree;
import edu.illinois.cs.cogcomp.thrift.base.Node;

public class CuratorDemo {
    public static void main(String[] args) {

        String hostname = "myhost.cs.uiuc.edu";
        int port = 9090;
        

        TTransport transport = new TSocket(hostname, port);
        transport = new TFramedTransport(transport);
        TProtocol protocol = new TBinaryProtocol(transport);

        Curator.Client client = new Curator.Client(protocol);
       

        String text = "With less than 11 weeks to go to the final round of climate "
            + "talks in Copenhagen, the UN chief, Ban Ki-Moon did not bother to hide "
            + "his frustration in his opening remarks. \"The world's glaciers are "
            + "now melting faster than human progress to protect them -- or us,\" he "
            + "said. Others shared his gloom. \"Today we are on a path to failure,"
            + "\" said France's Nicolas Sarkozy.";

        Record record = null;
        try {
            transport.open();
            record = client.provide("ner", text, false);
            transport.close();
        } catch (ServiceUnavailableException e) {
        	if (transport.isOpen())
        		transport.close();
            e.printStackTrace();
        } catch (AnnotationFailedException e) {
        	if (transport.isOpen())
        		transport.close();
            e.printStackTrace();
        } catch (TException e) {
        	if (transport.isOpen())
        		transport.close();
            e.printStackTrace();
        }

        String rawText = record.getRawText();
        String identifier = record.getIdentifier();
        Map<String, Labeling> labelViews = record.getLabelViews();
        Map<String, Clustering> clusterViews = record.getClusterViews();
        Map<String, Forest> parseViews = record.getParseViews();
            

        for (Span span : labelViews.get("ner").getLabels()) {
            System.out.println(span.getLabel() + " : "
                    + text.substring(span.getStart(), span.getEnding()));
        }

        
        List<String> sentences = new ArrayList<String>();
        sentences.add("BHP Billiton , based in Australia , is offering $ 130 for each "
                      + "share of Potash , a 16 percent premium to its closing price "
                      + "Monday .");
        sentences.add("But Potash 's shares , which trade mainly on the New York Stock "
                      + "Exchange , surged 28 percent to $ 143.17 .");
        text = sentences.get(0) + " " + sentences.get(1);

        try {
            transport.open();
            record = client.wsprovide("chunk", sentences, false);
            transport.close();
        } catch (ServiceUnavailableException e) {
        	if (transport.isOpen())
        		transport.close();
            e.printStackTrace();
        } catch (AnnotationFailedException e) {
        	if (transport.isOpen())
        		transport.close();
            e.printStackTrace();
        } catch (TException e) {
        	if (transport.isOpen())
        		transport.close();
            e.printStackTrace();
        }

        boolean whitespaced = record.isWhitespaced();

        for (Span span : record.getLabelViews().get("chunk").getLabels()) {
            System.out.println(span.getLabel() + " : "
                    + text.substring(span.getStart(), span.getEnding()));
        }

    }
}
