Sunday, February 7, 2016

Analyzing Twitter Account


package javaapplication1;
// This sample uses the Apache HTTP client from HTTP Components (http://hc.apache.org/httpcomponents-client-ga/)
import io.indico.Indico;
import io.indico.api.results.IndicoResult;
import io.indico.api.results.BatchIndicoResult;
import io.indico.api.text.TextTag;
import io.indico.api.utils.IndicoException;
import java.io.IOException;
import java.io.StringReader;
import java.net.URI;
import java.net.URISyntaxException;
import java.nio.charset.UnsupportedCharsetException;
        import java.util.ArrayList;
        import java.util.List;
import java.util.Scanner;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import jdk.internal.org.xml.sax.InputSource;
import org.apache.http.entity.StringEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.http.entity.ContentType;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.HttpEntity;
import org.apache.http.ParseException;
import org.apache.http.client.utils.URIBuilder;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.util.EntityUtils;
        import org.jsoup.Jsoup;
        import org.jsoup.nodes.Document;
        import org.jsoup.nodes.Element;
        import org.jsoup.select.Elements;
import twitter4j.conf.*;
import twitter4j.json.*;
import twitter4j.management.*;
import twitter4j.auth.*;
import twitter4j.api.*;
import twitter4j.util.*;
import twitter4j.*;
public class JavaApplication1 {
         //   public string word1 = "";
 public static void main(String[] args) {
              ConfigurationBuilder cb = new ConfigurationBuilder();
             String alltweets = "";
System.out.println("Twitter Profile Analysis ");
cb.setOAuthConsumerKey("6zV5gjJa11GAosOQanfpkAmYs");
cb.setOAuthConsumerSecret("DrbAdwlwg9ekezyIHOxAlGQIkITHhCoWFaY3EWbqjRVtrn9u3f");
cb.setOAuthAccessToken("136941431-mVOpaxVfdULOpRT26sbFli5CuxpVBooJkjTaYZWt");
cb.setOAuthAccessTokenSecret("lfpjkJJvrgt9nt6OILVBgBlqZiPUydaq6QvdErIIXFFMT");

java.util.List statuses = null;
 Scanner s = new Scanner(System.in);
Twitter twitter = new TwitterFactory(cb.build()).getInstance();
 System.out.print("Enter Twitter userid : ");
String userName =s.nextLine();
System.out.println("Image descriptions ");
int numTweets = 100;
String[] twArray = new String[numTweets];



  try {
    statuses = twitter.getUserTimeline(userName);
  }
  catch(TwitterException e) {
  }

  for (int i=0; i<statuses.size(); i++) {
    Status status = (Status)statuses.get(i);

    //println(status.getUser().getName() + ": " + status.getText());
    twArray[i] = status.getText();
   
    try
    {
    MediaEntity[] media = status.getMediaEntities(); //get the media entities from the status
for(MediaEntity m : media){ //search trough your entities
    //System.out.println(m.getMediaURL()); //get your url!
    picture(m.getMediaURL());
}
    //twArray[i] = emoticons(twArray[i]);
    String line = twArray[i];
    line = line.toLowerCase();
             String urlPattern = "((https?|ftp|gopher|telnet|file|Unsure|http):((//)|(\\\\))+[\\w\\d:#@%/;$()~_?\\+-=\\\\\\.&]*)";
             line = line.replaceAll(urlPattern,"");
             line = emoticons(line);  
             line = line.replaceAll("rt","");
             //line = removeDuplicateCharactersFromWord(line);
                          alltweets = alltweets + " " + line;

   // System.out.println(line);
    }
    catch(Exception e)
    {
       
    }
  }
  try
  {
  String word = alltweets;
  Indico indico = new Indico("39cff84ef5d23aa2eea08d49a92c3e2a");
            IndicoResult single = indico.sentiment.predict(word);
           
            Double result = single.getSentiment();
           
            System.out.println("Sentiment "+result);
            IndicoResult texttags = indico.textTags.predict(word);
            ;
            IndicoResult language = indico.language.predict(word);
            IndicoResult political = indico.political.predict(word);
            IndicoResult keywords = indico.keywords.predict(word);
            IndicoResult ner = indico.namedEntities.predict(word);
           // System.out.println("Sentiment "+result);
            System.out.println("Text tags "+texttags.getTextTags());
            System.out.println("language "+language.getLanguage());
            System.out.println("political "+political.getPolitical());
            System.out.println("keywords "+keywords.getKeywords());
            System.out.println("ner "+ner.getNamedEntities());
  }
  catch(Exception e)
  {
     
  }
              String picturelink = "http://pbs.twimg.com/media/At_m74xCMAEU5sa.jpg";
              picture(picturelink);
              HttpClient httpclient = HttpClients.createDefault();

         //hashtag    
        try
        {
            URIBuilder builder = new URIBuilder("https://api.projectoxford.ai/text/weblm/v1.0/breakIntoWords");

            builder.setParameter("model", "title");
            builder.setParameter("text", "GHC15");
            builder.setParameter("order", "5");
            builder.setParameter("maxNumOfCandidatesReturned", "5");

            URI uri = builder.build();
            HttpPost request = new HttpPost(uri);
            request.setHeader("Ocp-Apim-Subscription-Key", "76d20627deaf47bba44eb96b69b91444");


            // Request body
            String fk = "{ 'Url': '"+ picturelink + "' }";
     StringEntity reqEntity = new StringEntity(fk, ContentType.create("application/json"));
            request.setEntity(reqEntity);

            HttpResponse response = httpclient.execute(request);
            HttpEntity entity = response.getEntity();
           
            if (entity != null)
            {
                String f = EntityUtils.toString(entity);
                System.out.println(f);
               // JSONObject obj = new JSONObject(responsejsonobj);
            }
        }
        catch (Exception e)
        {
            System.out.println(e.getMessage());
        }

        //spellcheck
         try
        {
            URIBuilder builder = new URIBuilder("https://api.projectoxford.ai/text/v1.0/spellcheck");

            builder.setParameter("mode", "proof");

            URI uri = builder.build();
            HttpPost request = new HttpPost(uri);
            request.setHeader("Content-Type", "application/x-www-form-urlencoded");
            request.setHeader("Ocp-Apim-Subscription-Key", "14d42dce7b4d4661b40ca950803d3aaf");


            // Request body
           // String gggh = "Text="+word;
            StringEntity reqEntity = new StringEntity("Text=Fiften ideas for Twitter to transform into a profitable user-centered business");
            request.setEntity(reqEntity);

            HttpResponse response = httpclient.execute(request);
            HttpEntity entity = response.getEntity();

            if (entity != null)
            {
                System.out.println(EntityUtils.toString(entity));
            }
        }
        catch (Exception e)
        {
            System.out.println(e.getMessage());
        }
   
 }
          public static void picture(String link)
          {
              String emotion_link = "https://api.projectoxford.ai/emotion/v1.0/recognize?";
              String emotion_keys = "ee87a7d4a3444436b54b773cbc35ed40";
              String cv_link = "https://api.projectoxford.ai/vision/v1/analyses?";
              String cv_keys = "879a8f7e8d7c455cbe62896a92426262";
             
              String a = out(link, emotion_link, emotion_keys);
              String b = out(link, cv_link, cv_keys);
             
              if(!(b.equals("[]")))
              {
                  System.out.println(b);
              }
              if(!(a.equals("[]")))
              {
                  System.out.println(a);
              }
          }
         public static String out(String picturelink, String apilink, String Keys)
         {
             String output = "";
             try {
  HttpClient httpclient = HttpClients.createDefault();
 
     URIBuilder builder = new URIBuilder(apilink);
     // Specify your subscription key
     builder.setParameter("subscription-key", Keys);
     // Specify values for optional parameters, as needed
     builder.setParameter("visualFeatures", "All");
     URI uri = builder.build();
   
     HttpPost request = new HttpPost(uri);
              String f = "{ 'Url': '"+ picturelink + "' }";
     StringEntity reqEntity = new StringEntity(f, ContentType.create("application/json"));
     request.setEntity(reqEntity);
     HttpResponse response = httpclient.execute(request);
     HttpEntity entity = response.getEntity();
     if (entity != null) {
       
                 String h = EntityUtils.toString(entity);
                 //System.out.println(h);
                 output = h;
               
     }
  }
           catch(Exception e)
  //catch (URISyntaxException | UnsupportedCharsetException | IOException | ParseException e)
  {
     System.out.println(e.getMessage());
  }
             return output;
         }
         public static String emoticons(String d1)
     {
          d1 = d1.toLowerCase();
          //String d = d1;
          String[] p = d1.split(" ");
         
        String h = "(([:][-][)]+)|([:][)]+)|([:][d])|([:][o][)]+)|([:][]])|([:][3])|([:][c][)]+)|([:][>])|([=][]])|([8][)]+)|([=][)]+)|([:][}])|([:][/^][)]+))";
        String h1 = "(([:][-][d])|([8][-][d])|([8][d])|([x][-][d])|([x][-][d])|([x][d])|([x][-][d])|([=][-][d])|([=][d])|([=][-][3])|([=][3])|([b][/^][d]))";
        String h2 = "(([>][:][\\[])|([:][-][(]+)|([:][(]+)|([:][:][=][c]+)|([:][c]+)|([:][-][<]+)|([:][-][\\[]+)|([:][\\[]+)|([:][{])|([/;][(]+))";
        String h3 = "(([:][-][|][|])|([:][@])|([>][:][(]+))";
        String h4 = "(([:]['][-][(]+)|([:][\"][-][(]+)|([:]['][(]+)|([:][\"][(]+))";
        String h5= "(([:]['][-][)]+)|([:][\"][-][)]+)|([:]['][)]+)|([:][\"][)]+))";
        String h6 = "(([d][:][<]+)|([d][:])|([d][8])|([d][;])|([d][=])|([d][X])|([v][.][v])|([d][-]['][:]))";
        String h7 = "(([>][:][o])|([:][-][o])|([:][o])|([8][-][0])|([o][_][o])|([o][-][o]))";
        String h8 = "(([:][*])|([:][\\^][*])|(['][}][{][']))";
        String h9 = "(([;][-][)]+)|([;][)]+)|([*][-][)]+)|([*][)]+)|([;][-])|([;][//]])|([;][d])|([;][//^][)]+)|([;][-][,]))";
        String h10 = "(([>][:][p])|([:][-][p])|([:][p])|([x][-][p])|([x][p])|([=][p])|([p][:]))";
        String h11 = "(([>][:][\\\\]+)|([>][:][/]+)|([:][-][/]+)|([:][-][.])|([:][/]+)|([:][\\\\])|([=][/])|([=][\\\\])|([:][l])|([=][l])|([:][s])|([>][.][<]))";
        String h12 = "(([:][|]+)|([:][-][|]+))";
        String h13 = "(([o][:][-][)]+)|([0][:][-][3])|([0][:][3])|([0][:][-][)]+)|([0][:][)]+)|([0][;][//`^][)]+))";
        String h14 = "(([:][-][x])|([:][x])|([:][-][#])|([:][#]))";
        String h15 = "(([>][:][)]+)|([>][;][)]+)|([>][:][-][)]+))";
        String h16 = "(([}][:][-][)]+)|([}][:][)]+)|([3][:][)]+))";
        String h17 = "(([o][/][\\\\][o])|([\\^][5])|([>][_][>][\\^])|([\\^][<][_][<]))";
        String h18 = "(([|][;][-][)]+)|([|][-][o]))";
        String h19 = "(([:][-][j]))";
        String h20 = "(([:][-][&])|([:][&]))";
        String h21 = "([#][-][)]+)";
        String h22 = "(([%][-][)]+)|([%][)]+))";
        String h23 = "(([:][-][#]+[.]+)|([:][#]+[.]+))";
        String h24 = "([<][:][-][|])";
        String h25 = "([\\\\][o][/])";
        String h26 = "(([<][3])|([<][/][3]))";
        String h27 = "([*][\\\\][0][/][*])";
        String jj = "";
        for(String d : p)
        {
        d = d.replaceAll(h4, " CRYING EMOTICON ");
        d = d.replaceAll(h5, " HAPPINESS EMOTICON ");
        d = d.replaceAll(h6, " HORROR EMOTICON ");
        d = d.replaceAll(h7, " SURPRISE EMOTICON ");
        d = d.replaceAll(h8, " KISSING EMOTICON ");
        d = d.replaceAll(h9, " WINK EMOTICON ");
        d = d.replaceAll(h10, " TONGUE EMOTICON ");
        d = d.replaceAll(h11, " ANNOYED EMOTICON ");
        d = d.replaceAll(h12," STRAIGHT FACE EMOTICON ");
        d = d.replaceAll(h13, " ANGEL EMOTICON ");
        d = d.replaceAll(h14, " SEALED LIPS EMOTICON ");
        d = d.replaceAll(h15, " EVIL EMOTICON ");
        d = d.replaceAll(h16, " DEVILLISH EMOTICON ");
        d =d.replaceAll(h17, " HIGH FIVE EMOTICON ");
        d = d.replaceAll(h18, " BORED EMOTICON ");
        d = d.replaceAll(h19," TONGUE EMOTICON ");
       d = d.replaceAll(h20, " TONGUE MOICON ");
        d = d.replaceAll(h21, " PARTIED EMOTICON ");
        d = d.replaceAll(h22, " CONFUSED EMOTICON ");
        d = d.replaceAll(h23, " BEING SICK EMOTICON ");
        d = d.replaceAll(h24, " DUMB EMOTICON ");
        d = d.replaceAll(h25, " CHEER EMOTICON ");
        d = d.replaceAll(h26, " HEART EMOTICON ");
        d = d.replaceAll(h27, " CHEER LEADER EMOTICON ");
        d = d.replaceAll(h," SMILEY EMOTICON ");
        d = d.replaceAll(h1," LAUGHING EMOTICON ");
       //  d = d.replaceAll(h1,"LAUGHINGEMOTICON");
        d = d.replaceAll(h2, " FROWNING EMOTICON ");
        d = d.replaceAll(h3, " ANGRY EMOTICON ");
         jj = jj + " "+ d;
        }
        return jj;
     }
   private static String removeDuplicateCharactersFromWord(String word1) {
         String[] g = word1.split(" ");
         String result1 = "";
    String result = new String("");
    for(String word : g)
    {
    for (int i = 0; i < word.length(); i++) {
        if (!result.contains("" + word.charAt(i))) {
            result += "" + word.charAt(i);
        }
    }
    result1 = result1 + " "+ result;
    result = "";  
}
     return result1;
     }
}

No comments:

Post a Comment