List of usage examples for twitter4j.conf ConfigurationBuilder setOAuthAccessTokenSecret
public ConfigurationBuilder setOAuthAccessTokenSecret(String oAuthAccessTokenSecret)
From source file:Origin.Searchtweet.java
protected void processRequest(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException, TwitterException { response.setContentType("text/html;charset=UTF-8"); ConfigurationBuilder cb = new ConfigurationBuilder(); cb.setDebugEnabled(true);/*from w ww.j a v a2 s . com*/ cb.setOAuthConsumerKey(CONSUMER_KEY); cb.setOAuthConsumerSecret(CONSUMER_SECRET); cb.setOAuthAccessToken(ACCESS_TOKEN); cb.setOAuthAccessTokenSecret(ACCESS_TOKEN_SECRET); TwitterFactory tf = new TwitterFactory(cb.build()); Twitter twitter = tf.getInstance(); User user = twitter.verifyCredentials(); request.setCharacterEncoding("UTF-8"); String search = "?" + request.getParameter("searchtweet"); //String search= request.getParameter("searchtweet"); Query query = new Query(); query.setCount(100); query.setQuery(search); QueryResult queryresult = null; try { queryresult = twitter.search(query); } catch (TwitterException e1) { e1.printStackTrace(); } ArrayList<String> userID = new ArrayList<>(); for (Status tweet : queryresult.getTweets()) { userID.add(tweet.getUser().getScreenName()); } ArrayList<String> profileimg = new ArrayList<>(); for (Status tweet : queryresult.getTweets()) { profileimg.add(tweet.getUser().getBiggerProfileImageURL()); } ArrayList<String> username = new ArrayList<>(); for (Status tweet : queryresult.getTweets()) { username.add(tweet.getUser().getName() + "<br>" + "@" + tweet.getUser().getScreenName()); } ArrayList<String> usernameURL = new ArrayList<>(); for (Status tweet : queryresult.getTweets()) { username.add(tweet.getUser().getURL()); } SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd HH:mm:ss"); ArrayList<String> resulttweet = new ArrayList<>(); for (Status tweet : queryresult.getTweets()) { resulttweet.add(tweet.getText() + "<br>" + sdf.format(tweet.getCreatedAt())); } request.setAttribute("search", search); request.setAttribute("userID", userID); request.setAttribute("profileimg", profileimg); request.setAttribute("username", username); request.setAttribute("resulttweet", resulttweet); request.setAttribute("usernameURL", usernameURL); request.getRequestDispatcher("/searchtweet.jsp").forward(request, response); }
From source file:Origin.Timeline.java
protected void processRequest(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { response.setContentType("text/html;charset=UTF-8"); ConfigurationBuilder cb = new ConfigurationBuilder(); cb.setDebugEnabled(true);/* w w w .j a va 2 s . c o m*/ cb.setOAuthConsumerKey(CONSUMER_KEY); cb.setOAuthConsumerSecret(CONSUMER_SECRET); cb.setOAuthAccessToken(ACCESS_TOKEN); cb.setOAuthAccessTokenSecret(ACCESS_TOKEN_SECRET); try { Twitter twitter = new TwitterFactory(cb.build()).getInstance(); User user = twitter.verifyCredentials(); Paging paging = new Paging(1, 200); ResponseList<Status> userstatus = twitter.getHomeTimeline(paging); request.setAttribute("userstatus", userstatus); request.getRequestDispatcher("/timeline.jsp").forward(request, response); } catch (TwitterException te) { te.printStackTrace(); System.out.println("Failed to get timeline: " + te.getMessage()); System.exit(-1); } }
From source file:Origin.Tweet.java
protected void processRequest(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException, TwitterException { response.setContentType("text/html;charset=UTF-8"); ConfigurationBuilder cb = new ConfigurationBuilder(); cb.setDebugEnabled(true);//w ww .j a v a2 s .c om cb.setOAuthConsumerKey(CONSUMER_KEY); cb.setOAuthConsumerSecret(CONSUMER_SECRET); cb.setOAuthAccessToken(ACCESS_TOKEN); cb.setOAuthAccessTokenSecret(ACCESS_TOKEN_SECRET); try { Twitter twitter = new TwitterFactory(cb.build()).getInstance(); User user = twitter.verifyCredentials(); HttpSession session = request.getSession(); request.setCharacterEncoding("UTF-8"); //String tweet = encode(request.getParameter("tweet")); String tweet = request.getParameter("tweet"); Status status = twitter.updateStatus(tweet); session.setAttribute("tweet", tweet); request.getRequestDispatcher("/tweet.jsp").forward(request, response); } catch (TwitterException e1) { e1.printStackTrace(); } }
From source file:Principal.Tracker_Twitter.java
License:Minecraft Mod Public
public void guardarResultados_Twitter(List<ObjetoBuscar> lista, BD base, int contadorBase, int TokenIndice) throws NoSuchAlgorithmException, KeyManagementException { int nuevoContadorBase = 0; if (contadorBase >= lista.size()) { System.out.println("Termino en:" + contadorBase); } else {// w w w .jav a 2s . c o m System.out.println("---------------------------------------------------------------------:P"); TrustManager[] trustAllCerts = { new X509TrustManager() { public X509Certificate[] getAcceptedIssuers() { return null; } public void checkClientTrusted(X509Certificate[] certs, String authType) { } public void checkServerTrusted(X509Certificate[] certs, String authType) { } } }; SSLContext sc = SSLContext.getInstance("SSL"); sc.init(null, trustAllCerts, new SecureRandom()); HttpsURLConnection.setDefaultSSLSocketFactory(sc.getSocketFactory()); HostnameVerifier allHostsValid = new HostnameVerifier() { public boolean verify(String hostname, SSLSession session) { return true; } }; HttpsURLConnection.setDefaultHostnameVerifier(allHostsValid); System.out.println("Token : " + TokenIndice); System.out.println(consumerKey[TokenIndice]); try { ConfigurationBuilder cb = new ConfigurationBuilder(); cb.setOAuthConsumerKey(consumerKey[TokenIndice]); cb.setOAuthConsumerSecret(this.consumerSecret[TokenIndice]); cb.setOAuthAccessToken(this.token[TokenIndice]); cb.setOAuthAccessTokenSecret(this.tokenSecret[TokenIndice]); Twitter unauthenticatedTwitter = new TwitterFactory(cb.build()).getInstance(); for (int numtTweets = contadorBase; numtTweets < contadorBase + 5; numtTweets++) { if (((ObjetoBuscar) lista.get(numtTweets)).getUrl().equals("") || ((ObjetoBuscar) lista.get(numtTweets)).getUrl() == null) { } else { System.out.println("Usuario: " + ((ObjetoBuscar) lista.get(numtTweets)).getUrl()); String usuariosinArroba = ((ObjetoBuscar) lista.get(numtTweets)).getUrl().replace("@", ""); System.out.println("" + usuariosinArroba); try { User usuario = unauthenticatedTwitter.showUser(usuariosinArroba); List<Status> ret = unauthenticatedTwitter.getRetweetsOfMe(); List<Status> favoritos = unauthenticatedTwitter.getFavorites(); Paging paging = new Paging(1, 1000); ResponseList<Status> statuses = unauthenticatedTwitter.getUserTimeline(usuario.getId(), paging); System.out.println("Followers: " + usuario.getFollowersCount()); System.out.println("Yo sigo: " + usuario.getFriendsCount()); List<String> listaTweets = new ArrayList(); List<Long> ListaRettewts = new ArrayList(); List<Integer> ListaFavoritos = new ArrayList(); List<Integer> ListaMenciones = new ArrayList(); List<Date> ListaFecha = new ArrayList(); List<Long> ListaIds = new ArrayList(); for (Status sta : statuses) { ListaIds.add(Long.valueOf(sta.getId())); listaTweets.add(sta.getText()); ListaRettewts.add(Long.valueOf(Long.parseLong(sta.getRetweetCount() + ""))); ListaMenciones.add(Integer.valueOf(sta.getUserMentionEntities().length)); ListaFecha.add(sta.getCreatedAt()); ListaFavoritos.add(Integer.valueOf(sta.getFavoriteCount())); } for (int i = 0; i < listaTweets.size(); i++) { base.guardarTrackTwitter_Log((Long) ListaIds.get(i), ((ObjetoBuscar) lista.get(numtTweets)).getUrl(), (String) listaTweets.get(i), (Date) ListaFecha.get(i), (Long) ListaRettewts.get(i), ((Integer) ListaFavoritos.get(i)).intValue(), ((Integer) ListaMenciones.get(i)).intValue()); } } catch (Exception e) { System.err.println("Entro al Try por :" + e); } nuevoContadorBase = numtTweets; } } System.out.println("Numero de Contador Base:" + nuevoContadorBase); guardarResultados_Twitter(lista, base, nuevoContadorBase + 1, TokenIndice + 1); } catch (NumberFormatException e) { System.err.println("Fallo por :" + e); } } }
From source file:Recursos.Notificacion.java
/** * Constructor de la clase Notificacion/*from w ww . j a va 2s. co m*/ * * inicializa todos los medios para hacer la coneccion de la cuenta del * twitter conn la aplicaccion */ public Notificacion() { ConfigurationBuilder cb = new ConfigurationBuilder(); cb.setOAuthConsumerKey("STFH7e9yQ8K7pIgs7oZSmsUB8"); cb.setOAuthConsumerSecret("6Uj5BMITSyd17UNFxTLi47k39KkOMuwIszCgXkc8AC3mNJEp3U"); cb.setOAuthAccessToken("133844036-u3qdbqgsxlSMeNWa5NzFrSCbnjAI7ExrRDHFrWw9"); cb.setOAuthAccessTokenSecret("5m7rBhbEQmll0bGtPGC3sqE3mo3emjmrjKrCBg26t9jhG"); twitter = new TwitterFactory(cb.build()).getInstance(); }
From source file:social.controller.PostToSocial.java
@Override public void processRequest(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { super.processRequest(request, response); boolean face = false; boolean twit = false; try {// w w w . j a v a2 s . c o m getSqlMethodsInstance().session = request.getSession(); Integer user_id = (Integer) getSqlMethodsInstance().session.getAttribute("UID"); String htmlString = (String) getSqlMethodsInstance().session.getAttribute("htmlString"); String isFacebook = request.getParameter("isFacebook"); String isTwitter = request.getParameter("isTwitter"); String getImageFile = request.getParameter("imageToPost"); String getFile = request.getParameter("imagePost"); String url = request.getParameter("url"); String file_image_path = AppConstants.LAYOUT_IMAGES_HOME + File.separator + getImageFile; // String file_image_path = getServletContext().getRealPath("") + "/temp/"+getImageFile; String imagePostURL = ServletUtil.getServerName(request.getServletContext()); //String imagePostURL = AppConstants.LAYOUT_IMAGES_HOME + getImageFile; if (isFacebook.equalsIgnoreCase("true")) { String accessToken = request.getParameter("accesstoken"); String posttext = request.getParameter("postText"); String title = request.getParameter("title"); String description = request.getParameter("description"); String url1 = request.getParameter("url"); facebook = new FacebookFactory().getInstance(); facebook.setOAuthAppId("592852577521569", "a87cc0c30d792fa5dd0aaef6b43994ef"); facebook.setOAuthPermissions("publish_actions, publish_pages,manage_pages"); // File file = new File(file_image_path); facebook.setOAuthAccessToken(new AccessToken(accessToken)); if (title == "") { Media media = new Media(new File(file_image_path)); PhotoUpdate update = new PhotoUpdate(media); update.message(posttext); facebook.postPhoto(update); } else { logger.info(title); PostUpdate post = new PostUpdate(posttext).picture(new URL( imagePostURL + "DownloadImage?image_type=LAYOUT_IMAGES&image_name=" + getImageFile)) .name(title).link(new URL(url1)).description(description); facebook.postFeed(post); } try { getSqlMethodsInstance().setSocialPostHistory(user_id, htmlString, false, true, getImageFile); } catch (Exception ex) { Logger.getLogger(PostToSocial.class.getName()).log(Level.SEVERE, null, ex.getCause()); Logger.getLogger(PostToSocial.class.getName()).log(Level.SEVERE, null, ex.getMessage()); } } if (isTwitter.equalsIgnoreCase("true")) { try { AccessToken accTok = null; String shortUrl = ""; ConfigurationBuilder twitterConfigBuilder = new ConfigurationBuilder(); twitterConfigBuilder.setDebugEnabled(true); twitterConfigBuilder.setOAuthConsumerKey("K7TJ3va8cyAeh6oN3Hia91S2o"); twitterConfigBuilder .setOAuthConsumerSecret("IWUt2aDVTHgUc8N0qI0cF1Z1dTAEQ7CSgnBymZNr3BPSmtkNHL"); twitterConfigBuilder.setOAuthAccessToken(request.getParameter("twittweraccestoken")); twitterConfigBuilder.setOAuthAccessTokenSecret(request.getParameter("twitterTokenSecret")); Twitter twitter = new TwitterFactory(twitterConfigBuilder.build()).getInstance(); String statusMessage = request.getParameter("text").replace("bit.ly/1XOkJo", ""); shortUrl = request.getParameter("shorturl"); if (shortUrl.length() > 0) { String StatusMessageWithoutUrl = statusMessage.substring(0, statusMessage.length()); if (StatusMessageWithoutUrl.length() + shortUrl.length() < 140) { statusMessage = StatusMessageWithoutUrl + " " + shortUrl; } else { int urlLength = shortUrl.length() + 1; int statusLength = 115 - urlLength; statusMessage = StatusMessageWithoutUrl.substring(0, statusLength); statusMessage = statusMessage + " " + shortUrl; } } File file = new File(file_image_path); int count = statusMessage.length(); StatusUpdate status = new StatusUpdate(statusMessage); // set the image to be uploaded here. status.setMedia(file); twitter.updateStatus(status); try { getSqlMethodsInstance().setSocialPostHistory(user_id, htmlString, false, true, getImageFile); } catch (Exception ex) { Logger.getLogger(PostToSocial.class.getName()).log(Level.SEVERE, null, ex.getCause()); Logger.getLogger(PostToSocial.class.getName()).log(Level.SEVERE, null, ex.getMessage()); } } catch (TwitterException te) { PrintWriter out1 = response.getWriter(); out1.println("Twitter Exception: " + te.getMessage()); Logger.getLogger(PostToSocial.class.getName()).log(Level.SEVERE, null, te.getCause()); Logger.getLogger(PostToSocial.class.getName()).log(Level.SEVERE, null, te.getMessage()); } catch (Exception e) { Logger.getLogger(PostToSocial.class.getName()).log(Level.SEVERE, null, e); Logger.getLogger(PostToSocial.class.getName()).log(Level.SEVERE, null, e.getMessage()); } } } catch (FacebookException e) { Logger.getLogger(PostToSocial.class.getName()).log(Level.SEVERE, null, e.getCause()); Logger.getLogger(PostToSocial.class.getName()).log(Level.SEVERE, null, e.getMessage()); } }
From source file:source.TwitterSource.java
License:Apache License
/** * The initialization method for the Source. The context contains all the * Flume configuration info, and can be used to retrieve any configuration * values necessary to set up the Source. * * @param context Key-value store used to pass configuration information * throughout the system./* w ww . j a va 2 s . c o m*/ */ @Override public void configure(Context context) { consumerKey = context.getString(TwitterSourceConstants.CONSUMER_KEY); consumerSecret = context.getString(TwitterSourceConstants.CONSUMER_SECRET); accessToken = context.getString(TwitterSourceConstants.ACCESS_TOKEN); accessTokenSecret = context.getString(TwitterSourceConstants.ACCESS_TOKEN_SECRET); String swString = context.getString(TwitterSourceConstants.SW_LNG_LAT); String neString = context.getString(TwitterSourceConstants.NE_LNG_LAT); if (swString != null && neString != null) { String[] sw = swString.split(","); String[] ne = neString.split(","); if (sw.length == 2 && ne.length == 2) { for (int i = 0; i < 2; i++) { locations[0][i] = Double.parseDouble(sw[i].trim()); locations[1][i] = Double.parseDouble(ne[i].trim()); } } else { locations = null; } } else { locations = null; } String keywordString = context.getString(TwitterSourceConstants.KEYWORDS); if (keywordString != null) { keywords = keywordString.split(","); for (int i = 0; i < keywords.length; i++) { keywords[i] = keywords[i].trim(); } } ConfigurationBuilder cb = new ConfigurationBuilder(); cb.setOAuthConsumerKey(consumerKey); cb.setOAuthConsumerSecret(consumerSecret); cb.setOAuthAccessToken(accessToken); cb.setOAuthAccessTokenSecret(accessTokenSecret); cb.setJSONStoreEnabled(true); cb.setIncludeEntitiesEnabled(true); cb.setIncludeRTsEnabled(true); twitterStream = new TwitterStreamFactory(cb.build()).getInstance(); }
From source file:traffickarmasent.newgetpage.java
public static void main(String[] args) throws IOException { // gets Twitter instance with default credentials Twitter twitter = new TwitterFactory().getInstance(); try {/*from ww w .ja va 2 s . c om*/ ConfigurationBuilder cb = new ConfigurationBuilder(); cb.setDebugEnabled(true); cb.setOAuthConsumerKey("GPtsu5cjC08KTOEojEoaHw"); cb.setOAuthConsumerSecret("SsgeXn73bN4CXUYtJfEdKOwBxVTmAEPvmFo3q2CX45w"); cb.setOAuthAccessToken("154196958-J1Gqy86jmQ6YSoFVVq69bmbJB0acGxiDEocxtvre"); cb.setOAuthAccessTokenSecret("DpTJr3huuDy2qMwsCMgsTn5yNbi0oQzSDGhDDWQsLog"); TwitterFactory tf = new TwitterFactory(cb.build()); Twitter twitter1 = tf.getInstance(); List<Status> statuses; String user; String[] users = { "livetrafficsyd", "trafficnsw", "sydtraffic_cs", "WazeTrafficSYD", "livetrafficnsw" }; Date[] d_users = { new Date(99, 2, 12), new Date(99, 2, 12), new Date(99, 2, 12), new Date(99, 2, 12), new Date(99, 2, 12) }; while (true) { for (int i = 0; i < users.length; i++) { statuses = twitter1.getUserTimeline(users[i]); for (int j = statuses.size() - 1; j >= 0; j--) { Status st = statuses.get(j); if (d_users[i].before(st.getCreatedAt())) { String message = removeUrl(st.getText()); File file = new File("out_sydney_new.txt"); //if file doesnt exists, then create it if (!file.exists()) { file.createNewFile(); } //true = append file FileWriter fileWritter = new FileWriter(file.getName(), true); BufferedWriter bufferWritter = new BufferedWriter(fileWritter); bufferWritter.write(message + "\n"); bufferWritter.close(); System.out.println("Done"); System.out.println("@" + st.getUser().getScreenName() + " - " + st.getText()); d_users[i] = st.getCreatedAt(); } } } try { Thread.sleep(300000); //1000 milliseconds is one second. } catch (InterruptedException ex) { Thread.currentThread().interrupt(); } System.out.println("firse"); } } catch (TwitterException te) { te.printStackTrace(); System.out.println("Failed to get timeline: " + te.getMessage()); System.exit(-1); } }
From source file:traffickarmasent.TweetCollection.java
public static void main(String[] args) throws FileNotFoundException, IOException, Exception { // loading slang dictionary with key as slang and value as its full form slangMap = new HashMap<String, String>(); BufferedReader slangRead = new BufferedReader(new FileReader("extras/out.txt")); String line = ""; while ((line = slangRead.readLine()) != null) { String parts[] = line.split("\t"); slangMap.put(parts[0], parts[1]); }//from w w w . j a va 2 s. c o m slangRead.close(); //loading entity list BufferedReader htm_in = new BufferedReader(new FileReader("extras/html_ent.txt")); entityList = new ArrayList<String>(); while ((line = htm_in.readLine()) != null) { entityList.add(line); } FileInputStream fos1 = new FileInputStream(new File("extras/hash1.dat")); // loading emoticon dictionary, with key as emoticon and value as its sentiment score ObjectInputStream out1 = new ObjectInputStream(fos1); emohash1 = (HashMap<String, Double>) out1.readObject(); //System.out.println(hm1); FileInputStream fos2 = new FileInputStream(new File("extras/hash2.dat")); // loading emoticon dictionary, with key as emoticon and value as its sentiment score ObjectInputStream out2 = new ObjectInputStream(fos2); emohash2 = (HashMap<String, Double>) out2.readObject(); //System.out.println(hm2); //loading senti-wordnet FileReader fr2 = new FileReader("extras/SentiWordNet_scores_final.txt"); BufferedReader br2 = new BufferedReader(fr2); String str2; senti_map = new HashMap<String, Double>(); while ((str2 = br2.readLine()) != null) { StringTokenizer st = new StringTokenizer(str2, "^"); senti_map.put(st.nextToken(), Double.parseDouble(st.nextToken())); } String serializedClassifier = "english.all.3class.distsim.crf.ser.gz"; //NER configuration AbstractSequenceClassifier classifier = CRFClassifier.getClassifierNoExceptions(serializedClassifier); DocumentBuilder db = DocumentBuilderFactory.newInstance().newDocumentBuilder(); InputSource is = new InputSource(); MaxentTagger tagger = new MaxentTagger("taggers/english-left3words-distsim.tagger"); ConfigurationBuilder cb = new ConfigurationBuilder(); cb.setDebugEnabled(true); cb.setOAuthConsumerKey("ufulV3imKoYNzdh58LotTC1YD"); cb.setOAuthConsumerSecret("2A781ma736HTenAXXYn9tRIelQYJkbCqY0GLi7W71ZwwDmNU59"); cb.setOAuthAccessToken("2564905075-MY9osfHabaRnonQVHHhHeA1vCLSOhuHWjBNBiIY"); cb.setOAuthAccessTokenSecret("JsD8Woc7iiFiDSwoCwjNAb6KNEurz7tBqSj9pJV8WXabr"); twitter4j.TwitterStream twitterStream = new TwitterStreamFactory(cb.build()).getInstance(); StatusListener listener = new StatusListener() { double score = 0.0; double count = 0; ArrayList<String> locArray = new ArrayList<String>(); @Override public void onStatus(Status status) { String text = status.getText(); double geoLat = 0.0; double geoLng = 0.0; String tweetId = status.getId() + ""; String userName = status.getUser().getName(); String userId = status.getUser().getId() + ""; if (status.getGeoLocation() != null) { geoLat = status.getGeoLocation().getLatitude(); geoLng = status.getGeoLocation().getLongitude(); } tweetClean(text, status.getGeoLocation()); System.out.println(text + "\n" + tweetId + " " + userName + " " + userId); } @Override public void onDeletionNotice(StatusDeletionNotice sdn) { throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates. } @Override public void onTrackLimitationNotice(int i) { throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates. } @Override public void onScrubGeo(long l, long l1) { throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates. } @Override public void onStallWarning(StallWarning sw) { throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates. } @Override public void onException(Exception excptn) { throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates. } public void tweetClean(String message, GeoLocation loc) { try { // URL removal message = removeUrl(message); System.out.println("lalala" + message); //removing user mentions message = userMentions(message); //slang removal String[] acro = slangRemoval(message); //entity removal String[] finaldata = entityRemoval(acro); message = ""; for (String word : finaldata) { message += word + " "; } //System.out.println(message); //Ner Taggging String XmlData = classifier.classifyWithInlineXML(message); message = XmlData; //handling words to the spell_checked String[] data = message.split("<"); String val = ""; for (String word : data) { if (word.startsWith("PER") || word.startsWith("LOC")) { word = word.replaceAll("PERSON>", ""); word = word.replaceAll("/PERSON>", ""); word = word.replaceAll("LOCATION>", ""); word = word.replaceAll("/LOCATION>", ""); //insert word into database here locArray.add(word); } else { word = word.replaceAll("/PERSON>", ""); word = word.replaceAll("/LOCATION>", ""); //System.out.println(word); val += word; } } //System.out.println("see" + val); if (loc != null || locArray.size() > 0) { val = val.replaceAll("\\s+", " "); String[] temp = val.split(" "); String match = ""; //spell_check for (String word : temp) { //System.out.println(word); if (emohash2.containsKey(word)) { score += emohash2.get(word); count++; message = message.replace(word, ""); } Process p = Runtime.getRuntime().exec("python extras/text_blob.py " + word); BufferedReader in = new BufferedReader(new InputStreamReader(p.getInputStream())); //System.out.println(in.readLine()); match = in.readLine(); //match = spell_check(word); //System.out.println(match); if (!match.equals(word)) { message = message.replaceAll(word, match); } } System.out.println(message); //UTF-8 emoji's emojiDetection(message); //handle NerTags message = af_spellcheck(message); //System.out.println(a); //removing irrelevant chars message = removeChars(message); //System.out.println(a); //POS- TAGGING message = posTagging(message); // removing prepositions and nouns message = removePrepn(message); //System.out.println(a); sentiScores(message); System.out.println(score); System.out.println(score / count); } } catch (Exception ex) { System.out.println(ex.getMessage()); } } public String removeUrl(String message) { String urlPattern = "((https?|ftp|gopher|telnet|file|Unsure|http):((//)|(\\\\))+[\\w\\d:#@%/;$()~_?\\+-=\\\\\\.&]*)"; Pattern p = Pattern.compile(urlPattern, Pattern.CASE_INSENSITIVE); Matcher m = p.matcher(message); int i = 0; while (m.find()) { message = message.replaceAll(m.group(i), "").trim(); i++; } return message; } public String af_spellcheck(String message) { message = message.replaceAll("<PERSON>", ""); message = message.replaceAll("</PERSON>", ""); message = message.replaceAll("<LOCATION>", ""); message = message.replaceAll("</LOCATION>", ""); return message; } public String removeChars(String message) { message = message.replaceAll("\\.", ""); message = message.replaceAll("\\!", ""); message = message.replaceAll("\\$", ""); message = message.replaceAll("\\%", ""); message = message.replaceAll("\\^", ""); message = message.replaceAll("\\|", ""); message = message.replaceAll("\\+", ""); message = message.replaceAll("\\:", ""); message = message.replaceAll("\\(", ""); message = message.replaceAll("\\)", ""); message = message.replaceAll("\\*", ""); message = message.replaceAll("\\{", ""); return message; } public void sentiScores(String message) { message = message.replaceAll("_NNS", "_n"); message = message.replaceAll("_NN", "_n"); message = message.replaceAll("_RBR", "_r"); message = message.replaceAll("_RBS", "_r"); message = message.replaceAll("_RB", "_r"); message = message.replaceAll("_JJR", "_a"); message = message.replaceAll("_JJS", "_a"); message = message.replaceAll("_JJ", "_a"); message = message.replaceAll("_VBD", "_v"); message = message.replaceAll("_VBG", "_v"); message = message.replaceAll("_VBN", "_v"); message = message.replaceAll("_VBP", "_v"); message = message.replaceAll("_VBZ", "_vs"); message = message.replaceAll("_VB", "_v"); message = message.replaceAll("\\s+", " "); // System.out.println(message); String[] senti_token = message.split(" "); for (String word : senti_token) { word = word.toLowerCase(); System.out.println(word); if (senti_map.containsKey(word)) { score += senti_map.get(word); //System.out.println(score); count++; } } } public void emojiDetection(String message) { Pattern emo = Pattern.compile("[\\uD83D\\uDE01-\\uD83D\\uDE4F]"); Matcher m_emo = emo.matcher(message); while (m_emo.find()) { if (emohash1.containsKey(m_emo.group())) //System.out.println("llalala"); { score += emohash1.get(m_emo.group()); } count++; } } public String userMentions(String message) { Pattern p = Pattern.compile("\\@\\w+"); Matcher m = p.matcher(message); while (m.find()) { //System.out.println(m.group()); message = message.replaceAll(m.group(), ""); } return message; } public String[] slangRemoval(String message) { ArrayList<String> slangRemovalList = new ArrayList<String>(); String[] words = message.split(" "); for (String single : words) { if (slangMap.containsKey(single.toUpperCase())) { slangRemovalList.add(slangMap.get(single.toUpperCase())); } else { slangRemovalList.add(single); } } String[] myArray = new String[slangRemovalList.size()]; slangRemovalList.toArray(myArray); return myArray; } public String posTagging(String message) throws Exception { String tagged = tagger.tagString(message); return tagged; } public String removePrepn(String message) { String delims = " "; String[] tokens = message.split(delims); for (String word : tokens) { if (word.endsWith("_IN") || word.endsWith("_NNP") || word.endsWith("_NNPS")) { message = message.replace(word, ""); } } return message; } public String[] entityRemoval(String[] message) { List<String> finalList = new ArrayList<String>(); for (String word : message) { if (!entityList.contains(word.trim())) { finalList.add(word); } } String[] myArray = new String[finalList.size()]; finalList.toArray(myArray); return myArray; } }; FilterQuery fq = new FilterQuery(); String keywords[] = { "Mumbai traffic", "@TrafflineMUM", "TrafficMum", "MumbaiTrafficPol", "avoid traffic Mumbai", "Breakdown Mumbai traffic", "@smart_mumbaikar", "@TrafficBOM", "#StreetSmartWithTraffline mumbai", "#mumbai #TRAFFICALERT ", "#mumbai #TRAFFIC" }; fq.track(keywords); twitterStream.addListener(listener); twitterStream.filter(fq); }
From source file:twitfeedbot.BotherSomeoneServlet.java
License:Open Source License
@Override public void doGet(HttpServletRequest req, HttpServletResponse resp) throws IOException { Twitter twit;/*from w ww.j a va 2 s . c o m*/ StringBuilder builder = new StringBuilder(); User victim = null; long cursor = -1; long[] friendIDs, victimIDs; Random r = new Random(); resp.setContentType("text/html"); resp.getWriter().println("<html>"); resp.getWriter().println("<body>"); try { ConfigurationBuilder twitterConfigBuilder = new ConfigurationBuilder(); twitterConfigBuilder.setDebugEnabled(false); System.setProperty("WORDNIK_API_KEY", WORDNIK_KEY); twitterConfigBuilder.setOAuthConsumerKey(CONSUMER_KEY); twitterConfigBuilder.setOAuthConsumerSecret(CONSUMER_SECRET); twitterConfigBuilder.setOAuthAccessToken(ACCESS_TOKEN); twitterConfigBuilder.setOAuthAccessTokenSecret(ACCESS_SECRET); twit = new TwitterFactory(twitterConfigBuilder.build()).getInstance(); Trends t = twit.getPlaceTrends(1); //global trends friendIDs = twit.getFollowersIDs(twit.getId(), cursor).getIDs(); if (friendIDs.length == 0) resp.getWriter().println("Cannot find any followers to bother"); else { victimIDs = twit.getFollowersIDs(friendIDs[r.nextInt(friendIDs.length)], cursor).getIDs(); if (victimIDs.length == 0) resp.getWriter().println("Cannot find any followers to bother"); else { victim = twit.showUser(victimIDs[r.nextInt(victimIDs.length)]); builder.append("@" + victim.getScreenName()).append(" "); //Append Wordnik example sentence builder.append(WordApi.topExample(WordsApi.randomWord().getWord()).getText()); if (builder.length() > 140) //Tweets are maximum 140 characters { if (builder.lastIndexOf(";", 110) > 0) builder.setLength(builder.lastIndexOf(";", 110)); else if (builder.lastIndexOf(":", 110) > 0) builder.setLength(builder.lastIndexOf(":", 110)); else if (builder.lastIndexOf(",", 110) > 0) builder.setLength(builder.lastIndexOf(",", 110)); else builder.setLength(110); } builder.append(" " + t.getTrends()[r.nextInt(t.getTrends().length)].getName()); if (builder.length() > 140) builder.setLength(140); //Tweets are limited to 140 characters twit.updateStatus(builder.toString()); resp.getWriter().println("Tweet posted: " + builder.toString() + "<br>"); } } } catch (FileNotFoundException e) { e.printStackTrace(System.err); resp.getWriter().println("Input file(s) not found<br>"); resp.getWriter().println("<pre>"); e.printStackTrace(resp.getWriter()); resp.getWriter().println("</pre>"); } catch (TwitterException e) { resp.getWriter().println("Problem with Twitter <br>"); resp.getWriter().println("<pre>"); e.printStackTrace(resp.getWriter()); resp.getWriter().println("</pre>"); } catch (KnickerException e) { e.printStackTrace(System.err); resp.getWriter().println("Problem with Wordnik <br>"); resp.getWriter().println("<pre>"); e.printStackTrace(resp.getWriter()); resp.getWriter().println("</pre>"); } catch (Exception e) { e.printStackTrace(System.err); resp.getWriter().println("<pre>"); e.printStackTrace(resp.getWriter()); resp.getWriter().println("</pre>"); } }