gr.uoc.nlp.opinion.dispatcher.HttpEventHandler.java Source code

Java tutorial

Introduction

Here is the source code for gr.uoc.nlp.opinion.dispatcher.HttpEventHandler.java

Source

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package gr.uoc.nlp.opinion.dispatcher;

import com.sun.net.httpserver.HttpExchange;
import com.sun.net.httpserver.HttpHandler;
import gr.uoc.nlp.opinion.analysis.FileConversion;
import gr.uoc.nlp.opinion.analysis.arguments.AnalyzeArguments;
import gr.uoc.nlp.opinion.analysis.sentiment.SentimentDetection;
import gr.uoc.nlp.opinion.analysis.suggestion.AnalyzeSuggestions;
import gr.uoc.nlp.opinion.analysis.suggestion.SentenceSimilarity;
import gr.uoc.nlp.opinion.ilsptagger.POSTagDocuments;
import gr.uoc.nlp.opinion.ilsptagger.ParseXML;
import gr.uoc.nlp.opinion.main.LoadSettings;
import gr.uoc.nlp.opinion.mysql.LoadTrainingSet;
import gr.uoc.nlp.opinion.mysql.MysqlConnect;
import gr.uoc.nlp.opinion.mysql.queries.QueryInsert;
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.logging.Level;
import java.util.logging.Logger;
import org.json.simple.JSONArray;
import org.json.simple.JSONObject;
import org.json.simple.parser.JSONParser;
import org.json.simple.parser.ParseException;
import weka.core.Instances;

/**
 *
 * @author smyrgeorge
 */

public class HttpEventHandler implements HttpHandler {

    private static final LoadSettings settings = new LoadSettings();

    private static final String POSTagger = settings.getTagger_location_folder();
    private static final String TrainSetPath = settings.getTrain_set_location();
    private static final boolean ElasticSearch = settings.getUse_ElasticSearch();

    /**
     *
     * @param t
     * @throws IOException
     */
    @Override
    public void handle(HttpExchange t) throws IOException {

        StringBuilder input = new StringBuilder();
        try (InputStreamReader isr = new InputStreamReader(t.getRequestBody(), "utf-8");
                BufferedReader br = new BufferedReader(isr)) {

            int b;

            //get input documente
            while ((b = br.read()) != -1) {
                input.append((char) b);
            }

            System.out.println("Input Document:");
            System.out.println(input);
        }

        //prepare response
        String response;

        //choose scenario
        if (t.getRequestURI().getPath().contains("classify")) {
            response = this.classify(input.toString());

            System.out.println("Classification Results:");
            System.out.println(response);
        }

        else if (t.getRequestURI().getPath().contains("initialize")) {
            response = this.initialize();

            System.out.println(response);
        } else if (t.getRequestURI().getPath().contains("train")) {
            response = this.train(input.toString());

            System.out.println(response);
        } else {
            response = "Cannot find requested URL!";
        }

        //append header to packet
        t.getResponseHeaders().add("Content-Type", "application/json");

        if (t.getLocalAddress().getAddress().toString().equals("/127.0.0.1")) {
            t.getResponseHeaders().add("Access-Control-Allow-Origin", "http://localhost");
        } else {
            String remoteAddress = "http://snf-680262.vm.okeanos.grnet.gr";
            t.getResponseHeaders().add("Access-Control-Allow-Origin", remoteAddress);
        }

        //set packet code and length
        t.sendResponseHeaders(200, response.getBytes().length);

        //write response to socket
        try (OutputStream os = t.getResponseBody()) {
            os.write(response.getBytes());
            os.close();
        }

    }

    private String train(String json) {

        //Connect to database, needed always:
        MysqlConnect mysql = new MysqlConnect(settings.getMysql_username(), settings.getMysql_password(),
                settings.getMysql_ip());
        mysql.connect();

        //Initialize JSON Parser
        JSONParser parser = new JSONParser();
        try {

            //Parse the JSON
            Object obj = parser.parse(json);
            JSONObject jsonObject = (JSONObject) obj;

            //Get document from JSON
            String document = (String) jsonObject.get("document");

            //Parse the doc_sentences array
            ArrayList<String> doc_sentences = new ArrayList<>();
            JSONArray doc_sentencesJSONArray = (JSONArray) jsonObject.get("doc_sentences");
            Iterator<JSONObject> iterator = doc_sentencesJSONArray.iterator();
            while (iterator.hasNext()) {
                JSONObject doc_sentenceJsonObject = (JSONObject) iterator.next();
                String doc_sentence = (String) doc_sentenceJsonObject.get("s");
                doc_sentences.add(doc_sentence);
            }

            //Parse the arguments array
            ArrayList<String> arguments = new ArrayList<>();
            JSONObject argumentsJsonObject = (JSONObject) jsonObject.get("arguments");
            JSONArray argumentsJSONArray = (JSONArray) argumentsJsonObject.get("sentences");
            iterator = argumentsJSONArray.iterator();
            while (iterator.hasNext()) {
                JSONObject argumentJsonObject = (JSONObject) iterator.next();
                String argument = (String) argumentJsonObject.get("s");
                arguments.add(argument);
            }

            //Parse the suggestions array
            ArrayList<String> suggestions = new ArrayList<>();
            JSONObject suggestionsJsonObject = (JSONObject) jsonObject.get("suggestions");
            JSONArray suggestionsJSONArray = (JSONArray) suggestionsJsonObject.get("sentences");
            iterator = suggestionsJSONArray.iterator();
            while (iterator.hasNext()) {
                JSONObject suggestionJsonObject = (JSONObject) iterator.next();
                String suggestion = (String) suggestionJsonObject.get("s");
                suggestions.add(suggestion);
            }

            //Get the opinion
            JSONObject opinionJsonObject = (JSONObject) jsonObject.get("opinion");
            String opinion = (String) opinionJsonObject.get("sentiment");

            //Update database
            QueryInsert qi = new QueryInsert(mysql);
            qi.queryInsertTrasnsaction(document, doc_sentences, arguments, suggestions, opinion);

        } catch (ParseException ex) {
            Logger.getLogger(HttpEventHandler.class.getName()).log(Level.SEVERE, null, ex);
        }

        return "System updated!";
    }

    private String initialize() {

        //Connect to database, needed always:
        MysqlConnect mysql = new MysqlConnect(settings.getMysql_username(), settings.getMysql_password(),
                settings.getMysql_ip());
        mysql.connect();

        //POS Tag comments from database:
        POSTagDocuments parse = new POSTagDocuments(mysql);
        try {
            parse.startTagger(POSTagger, false, settings.getIlsp_tagger_ip());//true,false = call tagger

            //Parse .xml files on specific folder:
            ParseXML xml = new ParseXML(parse.getDir(), mysql);
            xml.startParse();

            //Upload to database:
            xml.upload(ElasticSearch);//true == upload database + elasticSearch

            //Insert train Set from .csv file:        
            LoadTrainingSet lts = new LoadTrainingSet(mysql, TrainSetPath);
            lts.parseCSV();

            //Sentence Similarity
            SentenceSimilarity ss = new SentenceSimilarity(mysql, ElasticSearch);
            ss.sentencesFromDatabase();

            //Sentiment Analysis
            SentimentDetection sentiment = new SentimentDetection(mysql, "nltk", true);

            //Delete Folder containing .xml files (optional):
            parse.deleteTmpFolder();

            return "Initialize successful!";
        } catch (Exception e) {
            //Delete Folder containing .xml files (optional):
            parse.deleteTmpFolder();
        }

        return "Error Happened!";
    }

    private String classify(String document) throws IOException {

        //Connect to database, needed always:
        MysqlConnect mysql = new MysqlConnect(settings.getMysql_username(), settings.getMysql_password(),
                settings.getMysql_ip());
        mysql.connect();

        //POS Tag comments from database:
        POSTagDocuments parse = new POSTagDocuments(mysql);
        parse.startTagger(POSTagger, true, "temp_doc", document, settings.getIlsp_tagger_ip());//true,false = call tagger

        //Parse .xml files on specific folder:
        ParseXML xml = new ParseXML(parse.getDir(), mysql, "temp_doc");
        xml.startParse();

        //create empty JSONObject and append arguments
        JSONObject json = classifyArguments(mysql, parse, xml);
        //append suggestions to JSONObjsect
        json = classifySuggestions(json, mysql, parse, xml);
        //append overallapinion to JSONObjsect
        json = classifyOpinion(json, mysql, document);
        //append sentences to JSONObjsect
        json = getSentences(json, xml, "temp_doc");

        //append initial document to JSONObjsect
        json.put("document", document);

        //Delete Folder containing .xml files (optional):
        parse.deleteTmpFolder();

        return json.toJSONString();
    }

    private JSONObject classifyArguments(MysqlConnect mysql, POSTagDocuments parse, ParseXML xml)
            throws IOException {

        //create csv file in temp folder
        xml.createArgumentsCSVFile(parse.getDir());

        //get file names
        String src = parse.getDir() + xml.getCommentName() + "_arguments.csv";
        String dst = parse.getDir() + xml.getCommentName() + "_arguments.arff";

        //create .arff file for weka
        FileConversion.csvToArff(src, dst);

        //initialize argument extraction class
        AnalyzeArguments anar = new AnalyzeArguments(mysql);

        //create set with unclassified sentences
        Instances unclassified = new Instances(new BufferedReader(new FileReader(dst)));
        Instances classified = anar.classify(anar.classifier("smo"), unclassified);

        //create empty JSONObject
        JSONObject json = new JSONObject();

        //append results to JSONObject
        JSONArray jlist = new JSONArray();
        for (int i = 0; i < classified.numInstances(); i++) {
            JSONObject arg = new JSONObject();
            arg.put("s", classified.instance(i).toStringNoWeight());
            jlist.add(arg);
        }

        JSONObject arguments = new JSONObject();
        arguments.put("sentences", jlist);
        //        arguments.put("classifier_output", anar.getClassifierOutput());

        json.put("arguments", arguments);

        return json;
    }

    private JSONObject classifySuggestions(JSONObject json, MysqlConnect mysql, POSTagDocuments parse, ParseXML xml)
            throws IOException {

        //create csv file in temp folder
        xml.createSuggestionsCSVFile(parse.getDir(), ElasticSearch);

        //get file names
        String src = parse.getDir() + xml.getCommentName() + "_suggestions.csv";
        String dst = parse.getDir() + xml.getCommentName() + "_suggestions.arff";

        //create .arff file for weka
        FileConversion.csvToArff(src, dst);

        //initialize suggestion extraction class
        AnalyzeSuggestions ansu = new AnalyzeSuggestions(mysql);

        //create set with unclassified sentences
        Instances unclassified = new Instances(new BufferedReader(new FileReader(dst)));
        Instances classified = ansu.classify(ansu.classifier("randomforest"), unclassified);

        JSONArray jlist = new JSONArray();
        for (int i = 0; i < classified.numInstances(); i++) {
            JSONObject arg = new JSONObject();
            arg.put("s", classified.instance(i).toStringNoWeight());
            jlist.add(arg);
        }

        //create empty JSONObject
        JSONObject suggestions = new JSONObject();
        //append results to JSONObject
        suggestions.put("sentences", jlist);
        //        suggestions.put("classifier_output", ansu.getClassifierOutput());

        json.put("suggestions", suggestions);

        return json;
    }

    private JSONObject classifyOpinion(JSONObject json, MysqlConnect mysql, String document) {
        //        SentimentDetection sentiment= new SentimentDetection(mysql, "sentiStrength", false);
        SentimentDetection sentiment = new SentimentDetection(mysql, "nltk", false);

        JSONObject opinion = new JSONObject();
        opinion.put("sentiment", sentiment.getSentiment(document));
        //        opinion.put("english_text", sentiment.getTranslatedText());

        json.put("opinion", opinion);

        return json;
    }

    private JSONObject getSentences(JSONObject json, ParseXML xml, String name) {

        ArrayList<String> array = xml.getSentencesListSingle(name);
        JSONArray jlist = new JSONArray();

        int i = 1;
        for (String s : array) {
            JSONObject arg = new JSONObject();
            arg.put("s", s);
            jlist.add(arg);
            i++;
        }

        json.put("doc_sentences", jlist);
        return json;
    }
}