Progress with AI implementation.

[Enter Post Title Here]

Contents

Purpose: 2

1- Prepare Unix Server 3

2- Develop Server Program 5

3- Develop Client Program 6

4- Test Program 8

5- Sentiment Analysis using JAVA API (TBD) 9

Purpose:

The purpose of this code is analyzing sentiment of a client or subject as part of The Content Theory Posttraumatic Growth project.

Score > 0. Is positive and below 0 is negative. Store the data for each candidate for later experiments.

  1. 1- Prepare Unix Server
  2. i. Setup Amazon EC2 Linux Instance
  1. ii. Open firewall for socket programming for the server instance. We are going to use 9090
  1. iii. Assign www.rsadatasolutions.com to 3.88.209.85  (optional)

Verify port is open using telnet (used windows machine)

If you don’t see error and blank screen displayed, it means port is opened.

‘Ctrl’ key and push the ‘] and type quit to exit.

  1. iv. Use Putty to login 
  1. v. Install java  and JDK 

sudo amazon-linux-extras enable corretto8

sudo yum install java-1.8.0-amazon-corretto-devel

#verify installation

java -version

Now the server is ready with Java installed and port 9090 opened.

  1. 2- Develop Server Program

User any Desktop IDE to develop Java server program. Compile and copy to server

In this exercise, I have used MS-Code to develop the code and compiled in Windows machine. The transferred to amazon Linux 2 system using WinSCP.

  1. 3- Develop Client Program

Develop a client Socket Program to connect with server 

www.rsadatasolutions.com 

port: 9090

sasiClient.java

/* File is: simpleClient.java Sasikumar Manickam 2019

———————————————————————-*/

import java.io.*;  // Get the Input Output libraries

import java.net.*; // Get the Java networking libraries

public class sasiClient{

  public static void main (String args[]) {

    String serverName;

    if (args.length < 1) serverName = “www.rsadatasolutions.com”;

    else serverName = args[0];

    System.out.println(“Sasi’s Simple Socket Client.\n”);

    System.out.println(“Using server: ” + serverName + “, Port: 9090”);

    BufferedReader in = new BufferedReader(new InputStreamReader(System.in));

    try {

      String name;

      do {

  System.out.print

    (“Enter a string and the server will echo it back to you. Use [quit] to stop client: “);

  System.out.flush ();

  name = in.readLine ();

  if (name.indexOf(“quit”) < 0)

    getReply(name, serverName);

      } while (name.indexOf(“quit”) < 0);

      System.out.println (“Cancelled by user request.”);

    } catch (IOException x) {x.printStackTrace ();}

  }

  static void getReply (String ToServerString, String serverName){

    Socket sock;

    BufferedReader fromServer;

    PrintStream toServer;

    String textFromServer;

    try{

      /* Open our connection to server port, choose your own port number.. */

      sock = new Socket(serverName, 9090);

      // Create filter I/O streams for the socket:

      fromServer = 

  new  BufferedReader(new InputStreamReader(sock.getInputStream()));

      toServer   = new PrintStream(sock.getOutputStream());

      // Send string to the server:

      toServer.println(ToServerString); toServer.flush(); 

      // Read up to 3 lines of response from the server, and block while synchronously waiting:

      for (int i = 1; i <=3; i++){

  textFromServer = fromServer.readLine();

  if (textFromServer != null) System.out.println(textFromServer);

      }

      sock.close();

    } catch (IOException x) {

      System.out.println (“Socket error.”);

      x.printStackTrace ();

    }

  }

}

Compile sasiClient.java

  1. 4- Test Program

Download client http://rsadatasolutions.com/wp-content/uploads/2019/05/sasiClient.zip

Unzip and run sasiClient.class using java sasiClient command

  1. 5- Sentiment Analysis using JAVA API (TBD)

Modify server code to call Google sentiment Analysis

Add: 

// Imports the Google Cloud client library

import com.google.cloud.language.v1.Document;

import com.google.cloud.language.v1.Document.Type;

import com.google.cloud.language.v1.LanguageServiceClient;

import com.google.cloud.language.v1.Sentiment;

// Add class to call google API

  Private static void analyseSentiment(String inputText) throws Exception {

    // Instantiates a client

    try (LanguageServiceClient language = LanguageServiceClient.create()) {

      // The text to analyze

      String text = inputText;

      Document doc = Document.newBuilder()

          .setContent(text).setType(Type.PLAIN_TEXT).build();

      // Detects the sentiment of the text

      Sentiment sentiment = language.analyzeSentiment(doc).getDocumentSentiment();

String score = 

String magnitude = 

return “Score:” + sentiment.getScore() + “Magnitude:” + sentiment.getMagnitude();

    }