Read from watson platform - Java Cloud

Java examples for Cloud:Waton

Description

Read from watson platform

Demo Code


import java.io.BufferedReader;
import java.io.DataInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.io.StringReader;
import java.net.HttpURLConnection;
import java.net.URL;
import java.net.URLConnection;
import java.net.URLEncoder;
import java.util.ArrayList;

import javax.json.Json;
import javax.json.JsonArray;
import javax.json.JsonObject;
import javax.json.JsonReader;
import javax.json.JsonString;

import org.json.JSONArray;
import org.json.JSONObject;
import org.json.simple.parser.JSONParser;

public class alchemyCall {
  final String apiKey = "your key";
  final String alchemyUrl = "http://gateway-a.watsonplatform.net/calls/text/TextGetRankedConcepts";
  private String charset = java.nio.charset.StandardCharsets.UTF_8.name();
  private final String USER_AGENT = "Mozilla/5.0";

  public void readFromJSONFile(String fileName) {
    try {//from   ww  w. j a v  a 2s  . co  m
      PrintWriter writer = new PrintWriter("raw_added/german_2.json", "UTF-8");

      String aLine = "";
      JSONParser parser = new JSONParser();
      Object obj = new Object();

      /* Write to JSON */

      FileInputStream fstream1 = new FileInputStream(fileName);
      DataInputStream in = new DataInputStream(fstream1);
      BufferedReader br = new BufferedReader(new InputStreamReader(in, "UTF8"));
      int index = 0;
      while ((aLine = br.readLine()) != null) {
        JSONObject newObj = new JSONObject();
        obj = parser.parse(aLine.toString());
        JSONObject jsonObject = (JSONObject) obj;
        String concept = tryCatchBs(jsonObject.get("text").toString());
        String alphaAndDigits = jsonObject.get("text").toString().replaceAll("[ ](?=[ ])|[^A-Za-z0-9 ]+", "");
        String queryTextForLanguage = alphaAndDigits;
        String[] translation = new String[5];

        queryTrans queryTransObj = new queryTrans();
        translation = queryTransObj.translate(queryTextForLanguage);
        for (int k = 0; k < translation.length; k++) {
          if (translation[k] == null) {
            translation[k] = queryTextForLanguage;
          }
        }
        JSONParser newParser = new JSONParser();
        Object newObjPar = new Object();
        newObjPar = newParser.parse(concept.toString());// varun

        JSONArray conceptObj = (JSONArray) newObjPar;
        JSONObject tempObj = new JSONObject();

        // tempObj.put(conceptObj.get(""));
        // String conceptString = "";
        ArrayList<String> conceptString = new ArrayList<String>();
        ArrayList<String> relevanceString = new ArrayList<String>();
        for (int i = 0; i < conceptObj.size(); i++) {
          JSONObject tempConceptObj = (JSONObject) conceptObj.get(i);
          // System.out.println(tempConceptObj.keySet());
          conceptString.add(tempConceptObj.get("text").toString());
          relevanceString.add(tempConceptObj.get("relevance").toString());
        }

        newObj.putAll(jsonObject);

        newObj.put("text_ar", translation[0]);
        newObj.put("text_en", translation[1]);
        newObj.put("text_de", translation[2]);
        newObj.put("text_fr", translation[3]);
        newObj.put("text_ru", translation[4]);

        newObj.put("concept_tag", conceptString);
        newObj.put("relevance_tag", relevanceString);

        // System.out.println(newObj.get("concept_tag"));

        writer.println(newObj);
        index++;
        System.out.println(index);

      }
      br.close();
      writer.close();

      // System.out.println("Should be written by now");

    } catch (Exception e) {
      System.out.println(e);
    }

  }

  public String tryCatchBs(String queryText) {

    String alphaAndDigits = queryText.replaceAll("[ ](?=[ ])|[^-_,A-Za-z0-9 ]+", "");
    queryText = alphaAndDigits;
    String response = "";
    try {
      String query = String.format("apikey=%s&text=%s`&outputMode=json", URLEncoder.encode(apiKey, charset),
          URLEncoder.encode(queryText, charset));

      response = fetchHTTPData(alchemyUrl, query);

      Object myObject = new Object(); // blank object

      JSONParser parserObj = new JSONParser();
      myObject = parserObj.parse(response.toString()); // parsed string output to json

      JSONObject jsonObject = (JSONObject) myObject;

      response = jsonObject.get("concepts").toString();

      if (!response.equals(""))
        System.out.println("Hey man will it work " + response);
      else
        // System.out.println("No response from Language detection server...");
        return response;
    } catch (Exception ex) {
      System.out.println("Exception occured while detecting language...");
      ex.printStackTrace();
    }
    return response;

  }

  public String fetchHTTPData(String URL, String query) throws IOException {
    String response = "";
    int responseCode = 0;
    HttpURLConnection httpConn = (HttpURLConnection) new URL(URL + "?" + query).openConnection();
    httpConn.setDoOutput(true); // Triggers POST.
    httpConn.setRequestProperty("Accept-Charset", charset);
    httpConn.setRequestProperty("User-Agent", USER_AGENT);
    responseCode = httpConn.getResponseCode();
    if (responseCode == 200) { // OK
      BufferedReader in = new BufferedReader(new InputStreamReader(httpConn.getInputStream()));
      String inputLine;
      StringBuffer responseBuffer = new StringBuffer();

      while ((inputLine = in.readLine()) != null) {
        responseBuffer.append(inputLine);
      }
      in.close();
      response = responseBuffer.toString();
    }

    return response;
  }

  public static void main(String[] args) {
    String queryFileName = "raw/700-900.json";// /Users/varunjoshi/IdeaProjects/getTweets/queries.txt
    queryFileName = new File("").getAbsolutePath() + "/" + queryFileName;
    // System.out.println(queryFileName);
    if (!QueryRun.IsFileExists(queryFileName)) {
      System.out.println("Please provide queries.txt file in the current directory ! File not found...");
      return;
    }
    alchemyCall alchemyCallObj = new alchemyCall();
    alchemyCallObj.readFromJSONFile(queryFileName);
    System.out.println("Function runs very fast ");
    // alchemyCallObj.runQueriesOnSolr();

  }

}

class queryTrans {
  String queryStr;
  final String detectLangEndpoint="https://translate.yandex.net/api/v1.5/tr.json/detect?";
  final String translateEndpoint="https://translate.yandex.net/api/v1.5/tr.json/translate?";
  final String api_key="trnsl.1.1.20151203T154217Z.ccc587945a4cc109.efa5a68bd521b44e9360f98a8613e343a366d583";
  String[] translation=new String[5];



  public queryTrans(){

  }

  public String detectLang(String query){
      String lang="";
      String urlStr=detectLangEndpoint+"key="+api_key+"&text="+query;
      String result="";
      try{
          URL url = new URL(urlStr);
          URLConnection conn = url.openConnection ();

          // Get the response
          BufferedReader rd = new BufferedReader(new InputStreamReader(conn.getInputStream()));
          StringBuffer sb = new StringBuffer();
          String line;
          while ((line = rd.readLine()) != null)
          {
              sb.append(line);
          }
          rd.close();
          result = sb.toString();
          JsonReader jr=Json.createReader(new StringReader(result));
          JsonObject jobj=jr.readObject();
          lang=jobj.getString("lang");
      }catch(Exception e){
          e.printStackTrace();

      }
      //System.out.println(result);
      //System.out.println(lang);
      return lang;
  }

  public String[] translate(String query){
      String[] langs={"ar","en","de","fr","ru"};
      String fl=detectLang(query);
      int i,j;
      for(i=0;i<langs.length;i++)
          if(fl.equals(langs[i]))
              break;
      if(i==langs.length){
          System.out.println(fl+" is not supported!");
          System.exit(-1);
      }
      translation[i]=queryStr;
      for(j=0;j<langs.length;j++){
          if(j==i)
              continue;
          String urlStr=translateEndpoint+"key="+api_key+"&text="+query+"&lang="+langs[j];
          String result="";
          try{
              URL url = new URL(urlStr);
              URLConnection conn = url.openConnection ();

              // Get the response
              BufferedReader rd = new BufferedReader(new InputStreamReader(conn.getInputStream()));
              StringBuffer sb = new StringBuffer();
              String line;
              while ((line = rd.readLine()) != null)
              {
                  sb.append(line);
              }
              rd.close();
              result = sb.toString();
              JsonReader jr=Json.createReader(new StringReader(result));
              JsonObject jobj=jr.readObject();
              //System.out.println(result);
              JsonArray jArr;
              jArr=jobj.getJsonArray("text");
              translation[j]=((JsonString)jArr.get(0)).getString();
          }catch(Exception e){
              e.printStackTrace();

          }
      }
      for(int k=0;k<translation.length;k++)
          System.out.println(translation[k]);

      return translation;
  }

  public static void main(String[] args){
      queryTrans q1=new queryTrans();
       q1.translate("????? ?????");
  }
}

Related Tutorials