List of usage examples for twitter4j.conf ConfigurationBuilder build
public Configuration build()
From source file:summarizer.NewApplication.java
private void HashtagSearchButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_HashtagSearchButtonActionPerformed // TODO add your handling code here: ConfigurationBuilder cb = new ConfigurationBuilder(); cb.setDebugEnabled(true).setOAuthConsumerKey("uEeExq1FHARMvAGTJAY0dGxPh") .setOAuthConsumerSecret("jMcofTKFsayd4bpA6HFNlgkMfiveoS3ffRSCy9FCSs9pWYqdAD") .setOAuthAccessToken("2443437752-PrbVsDrQCxthX3T7lV3dtHs3FCXFXfBBHsdNrOS") .setOAuthAccessTokenSecret("MbauBu7R6vUUKqFD9ogK7VTIfce4nmvewYsgYqBOQ2ZbC"); TwitterFactory tf = new TwitterFactory(cb.build()); Twitter twitter = tf.getInstance();//from w w w .j av a 2 s .co m String hashtag; hashtag = HashtagTextField.getText(); Query query = new Query(hashtag); query.count(100); if (TwitterComboBox.getSelectedIndex() == 0) { query.resultType(Query.ResultType.mixed); } else if (TwitterComboBox.getSelectedIndex() == 0) { query.resultType(Query.ResultType.recent); } else { query.resultType(Query.ResultType.popular); } QueryResult result = null; try { result = twitter.search(query); } catch (TwitterException ex) { Logger.getLogger(NewApplication.class.getName()).log(Level.SEVERE, null, ex); } String alltweets = "", temp = ""; for (Status status : result.getTweets()) { temp = status.getText(); temp = temp.replaceAll("[\\t\\n\\r]", " "); if (!"".equals(temp)) { if ("RT".equals(temp.substring(0, 2))) { temp = temp.split(":", 2)[1]; } alltweets += (temp + "\n"); } } InputTextArea.setText(alltweets); }
From source file:t.twitter.TTwitterModule.java
License:Open Source License
@Kroll.method public void connect(HashMap args) { KrollDict arg = new KrollDict(args); ConfigurationBuilder cb = new ConfigurationBuilder(); cb.setDebugEnabled(true).setOAuthConsumerKey(arg.optString("apikey", "")) .setOAuthConsumerSecret(arg.optString("apisecret", "")) .setOAuthAccessToken(arg.optString("accesstoken", "")) .setOAuthAccessTokenSecret(arg.optString("accesssecret", "")); AsyncTwitterFactory tf = new AsyncTwitterFactory(cb.build()); twitter = tf.getInstance();/* w w w. j a v a 2s .c o m*/ twitter.addListener(new TwitterAdapter() { @Override public void updatedStatus(Status status) { Log.d("Twitter", "text: " + status.getText()); } @Override public void searched(QueryResult result) { HashMap<String, KrollDict[]> event = new HashMap<String, KrollDict[]>(); List<Status> tweets = result.getTweets(); KrollDict[] dList = new KrollDict[tweets.size()]; // sort tweets Collections.sort(tweets, new Comparator<Status>() { public int compare(Status o1, Status o2) { if (desc) { if (o2.getId() < o1.getId()) { return 1; } else { return -1; } } else { if (o2.getId() > o1.getId()) { return 1; } else { return -1; } } } }); // return tweets to titanium int i = 0; for (Status tweet : tweets) { if (lastID == -1 || lastID < tweet.getId()) { KrollDict d = new KrollDict(); d.put("username", tweet.getUser().getScreenName()); d.put("userimage", tweet.getUser().getProfileImageURL()); d.put("text", tweet.getText()); d.put("date", tweet.getCreatedAt()); d.put("id", Long.toString(tweet.getId())); dList[i] = d; lastID = tweet.getId(); i++; } } KrollDict[] dList2 = new KrollDict[i]; // shorten array System.arraycopy(dList, 0, dList2, 0, i); event.put("tweets", dList2); success.call(getKrollObject(), event); synchronized (LOCK) { LOCK.notify(); } } @Override public void verifiedCredentials(User user) { HashMap<String, KrollDict> event = new HashMap<String, KrollDict>(); KrollDict d = new KrollDict(); d.put("user_name", user.getName()); d.put("screen_name", user.getScreenName()); d.put("image_path", user.getProfileImageURL()); event.put("user_info", d); success.call(getKrollObject(), event); synchronized (LOCK) { LOCK.notify(); } } @Override public void onException(TwitterException e, TwitterMethod method) { synchronized (LOCK) { LOCK.notify(); } Log.e("twitter", "error: " + e.getErrorMessage()); } }); Log.d("Twitter", "connected"); }
From source file:the_vaps_project.The_vaps_project.java
public static void main(String[] args)/* throws TwitterException */ { // TODO code application logic here /*//from ww w. j a v a 2 s . co m List<Status> status = twitter.getHomeTimeline(); for(Status st : status){ System.out.println(st.getUser().getName()+"--------"+st.getText()); System.out.println(st.getCreatedAt()); }*/ // The factory instance is re-useable and thread safe. /*Twitter twitter2 = tf.getSingleton(); Query query = new Query("source:twitter4j yusukey"); QueryResult result = twitter.search(query); for (Status statuss : result.getTweets()) { System.out.println("@" + statuss.getUser().getName() + ":" + statuss.getText()); }*/ TwitterCriteria criteria = null; Tweet t = null; criteria = TwitterCriteria.create() .setMaxTweets(10).setUntil("2016-11-08").setQuerySearch("#NeverTrump"); /*Scribe s = new Scribe(); s.detruireFichier("tweets.xml"); s.ouvrir("tweets.xml"); s.ecrire("<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n"); s.ecrire("<tweets>\n"); for (int i = 0; i < TweetManager.getTweets(criteria).size(); i++) { t = TweetManager.getTweets(criteria).get(i); s.ecrire("\t<tweet>\n"); s.ecrire("\t\t<text><![CDATA["+t.getText()+"]]></text>\n"); s.ecrire("\t\t<hashtags>"+t.getHashtags()+"</hashtags>\n"); s.ecrire("\t\t<date>"+t.getDate()+"</date>\n"); s.ecrire("\t\t<username>"+t.getUsername()+"</username>\n"); s.ecrire("\t\t<retweets>"+t.getRetweets()+"</retweets>\n"); s.ecrire("\t\t<favorites>"+t.getFavorites()+"</favorites>\n"); s.ecrire("\t\t<id>"+t.getId()+"</id>\n"); s.ecrire("\t\t<geo>"+t.getGeo()+"</geo>\n"); s.ecrire("\t\t<mentions>"+t.getMentions()+"</mentions>\n"); s.ecrire("\t\t<permalink>"+t.getPermalink()+"</permalink>\n"); s.ecrire("\t\t<smileys>"+t.getSmileys()+"</smileys>\n"); s.ecrire("\t\t<polarite>"+t.getPolarite()+"</polarite>\n"); s.ecrire("\t</tweet>\n"); corpus.ecrire(s.filtrerTexte(t.getText() + "\n")); } s.ecrire("</tweets>"); s.fermer();*/ /* SUPER SITE D'EXEMPLES https://github.com/yusuke/twitter4j/tree/master/twitter4j-examples/src/main/java/twitter4j/examples*/ /*PARTIE GRAPHE DES RETWEETS*/ Set<String> nameOfUsers = new HashSet<>(); for (int i = 0; i < TweetManager.getTweets(criteria).size(); i++) { t = TweetManager.getTweets(criteria).get(i); System.out.println(t.getUsername()); nameOfUsers.add(t.getUsername()); } ConfigurationBuilder cb = new ConfigurationBuilder(); cb.setDebugEnabled(true).setOAuthConsumerKey("gVaO166IGfSCun5Esj1sZ5bX3") .setOAuthConsumerSecret("KhN1L4pXCG5U6d6tee1IGUnIV1bqxieQJDxE2HOx3AGI5npQRE") .setOAuthAccessToken("822018210178469888-4Pr1KRE2rmFvof0O5IUdTlxtkGKamU4") .setOAuthAccessTokenSecret("D1thDl7k8v2s1SupU5ZGhK3U7Mphb69fpUW4JuDou5O65"); TwitterFactory tf = new TwitterFactory(cb.build()); twitter4j.Twitter twitter = tf.getInstance(); /*String[] screenNames = nameOfUsers.toArray(new String[0]); ResponseList<User> usersNames; long cursor = -1; IDs ids; try { usersNames = twitter.lookupUsers(screenNames); for(User u: usersNames){ System.out.println(u.getId()+" "+u.getName()+" "+u.getName()); ids = twitter.getFollowersIDs("username", cursor); twitter.getFollowers } } catch (TwitterException ex) { Exceptions.printStackTrace(ex); }*/ Set<DuoKey> utilisateurs = new HashSet<>(); Set<DuoKey> liens = new HashSet<>(); //User us; try { int nbpages = 0; long cursor = -1; PagableResponseList<User> listeFollowersTemp; ArrayList<User> listeFollowers = new ArrayList<User>(); for (String s : nameOfUsers) { if (!utilisateurs.contains(new DuoKey(s, "auteurTweet"))) { utilisateurs.add(new DuoKey(s, "auteurTweet")); //listeFollowersTemp = twitter.getFollowersList(s, -1); do { TimeUnit.MINUTES.sleep(1); listeFollowersTemp = twitter.getFollowersList(s, cursor); for (User u : listeFollowersTemp) { listeFollowers.add(u); } nbpages++; } while (((cursor = listeFollowersTemp.getNextCursor()) != 0) && nbpages <= 9); //4=100 followers par auteur de tweet recherch } System.out.println(s + ", mes followers sont :"); for (User u : listeFollowers) { utilisateurs.add(new DuoKey(u.getName(), "auteurTweet")); liens.add(new DuoKey(u.getName(), s)); System.out.println("\t" + u.getName()); } listeFollowers.clear(); nbpages = 0; } } catch (TwitterException | InterruptedException ex) { Exceptions.printStackTrace(ex); } Scribe sc = new Scribe(); sc.detruireFichier("graphFollowers.gexf"); sc.ouvrir("graphFollowers.gexf"); sc.ecrire("<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n"); sc.ecrire( "<gexf xmlns=\"http://www.gexf.net/1.2draft/gexf.xsd\" version=\"1.2\" xmlns:viz=\"http://www.gexf.net/1.1draft/viz\" xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xsi:schemaLocation=\"http://www.gexf.net/1.2draft http://www.gexf.net/1.2draft/gexf.xsd\">\n"); sc.ecrire("\t<meta lastmodifieddate=\"2017-02-17\">\n"); sc.ecrire("\t\t<creator>Pirboss</creator>\n"); sc.ecrire("\t\t<description>Test v2</description>\n"); sc.ecrire("\t</meta>\n"); sc.ecrire("\t<graph defaultedgetype=\"directed\" mode=\"static\">\n"); sc.ecrire("\t\t<nodes>\n"); ArrayList<DuoKey> distinctListOfUsers = new ArrayList(utilisateurs); System.out.println(distinctListOfUsers.size()); for (int i = 0; i < distinctListOfUsers.size(); i++) { sc.ecrire("\t\t\t<node id=\"" + distinctListOfUsers.get(i).getX().replace("&", "&") + "\" label=\"" + distinctListOfUsers.get(i).getX().replace("&", "&") + "\"/>\n"); } sc.ecrire("\t\t</nodes>\n"); sc.ecrire("\t\t<edges>\n"); ArrayList<DuoKey> distinctListOfLinks = new ArrayList(liens); System.out.println(distinctListOfLinks.size()); for (int i = 0; i < liens.size(); i++) { sc.ecrire("\t\t\t<edge id=\"" + i + "\" source=\"" + distinctListOfLinks.get(i).getX().replace("&", "&") + "\" target=\"" + distinctListOfLinks.get(i).getY().replace("&", "&") + "\"/>\n"); } sc.ecrire("\t\t</edges>\n"); sc.ecrire("\t</graph>\n"); sc.ecrire("</gexf>"); sc.fermer(); PreviewJFrame p = new PreviewJFrame(); p.script(); /*PARTIE GRAPHE DES MENTIONS*/ //NE PAS OUBLIE LE COUP DE REMPLACER LES .replace("&", "&") POUR LE XML /*Set<String> users = new HashSet<>(); HashMap<DuoKey, Integer> map; map = new HashMap<DuoKey, Integer>(); DuoKey duokey; for (int i = 0; i < TweetManager.getTweets(criteria).size(); i++) { System.out.println(i); t = TweetManager.getTweets(criteria).get(i); users.add(t.getUsername()); if(!t.getMentions().isEmpty()){ for(String target : t.getMentions().split(" ")){ target = target.replaceFirst("@", ""); users.add(target); duokey = new DuoKey(t.getUsername(), target); if(map.get(duokey) != null){ map.put(duokey, map.get(duokey)+1); }else{ map.put(duokey, 1); } } } } Scribe s = new Scribe(); s.detruireFichier("tweets.gexf"); s.ouvrir("tweets.gexf"); s.ecrire("<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n"); s.ecrire("<gexf xmlns=\"http://www.gexf.net/1.2draft/gexf.xsd\" version=\"1.2\" xmlns:viz=\"http://www.gexf.net/1.1draft/viz\" xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xsi:schemaLocation=\"http://www.gexf.net/1.2draft http://www.gexf.net/1.2draft/gexf.xsd\">\n"); s.ecrire("\t<meta lastmodifieddate=\"2017-02-17\">\n"); s.ecrire("\t\t<creator>Pirboss</creator>\n"); s.ecrire("\t\t<description>Test v1</description>\n"); s.ecrire("\t</meta>\n"); s.ecrire("\t<graph defaultedgetype=\"directed\" mode=\"static\">\n"); s.ecrire("\t\t<nodes>\n"); ArrayList distinctList = new ArrayList(users); for(int i=0; i<distinctList.size(); i++){ s.ecrire("\t\t\t<node id=\""+i+"\" label=\""+distinctList.get(i)+"\"/>\n"); } s.ecrire("\t\t</nodes>\n"); s.ecrire("\t\t<edges>\n"); Iterator ite = map.entrySet().iterator(); int jj=0; while (ite.hasNext()) { Map.Entry pair = (Map.Entry)ite.next(); s.ecrire("\t\t\t<edge id=\""+(jj++)+"\" source=\""+distinctList.indexOf(((DuoKey)pair.getKey()).getX())+"\" target=\""+distinctList.indexOf(((DuoKey)pair.getKey()).getY())+"\" weight=\""+pair.getValue()+"\"/>\n"); ite.remove(); // avoids a ConcurrentModificationException } s.ecrire("\t\t</edges>\n"); s.ecrire("\t</graph>\n"); s.ecrire("</gexf>"); s.fermer(); PreviewJFrame pr = new PreviewJFrame(); pr.script();*/ }
From source file:tientx.supercode.myproejectdemov3.connection.TwitterConnection.java
private TwitterConnection() { ConfigurationBuilder cb = new ConfigurationBuilder(); cb.setDebugEnabled(true).setOAuthConsumerKey(TwitterConfig.API_KEY) .setOAuthConsumerSecret(TwitterConfig.API_SECRET).setOAuthAccessToken(TwitterConfig.ACCES_TOKEN) // .setOAuthAccessTokenSecret(TwitterConfig.ACCES_TOKEN_SECRET).setHttpConnectionTimeout(100000) .setOAuthAccessTokenSecret(TwitterConfig.ACCES_TOKEN_SECRET).setHttpConnectionTimeout(100000); // .setHttpProxyHost(TwitterConfig.PROXY_HOST) // .setHttpProxyPort(TwitterConfig.PROXY_PORT); TwitterFactory factory = new TwitterFactory(cb.build()); twitter = factory.getInstance();//from www. j a v a 2 s. com }
From source file:tiofortwitter.TioForTwitter.java
/** * @param args the command line arguments *//*from ww w. j ava2 s . c o m*/ public static void main(String[] args) throws JSONException, IOException { ConfigurationBuilder cb = new ConfigurationBuilder(); cb.setDebugEnabled(true).setOAuthConsumerKey("Gkr9iZwYDALZ16OdxMp5rubBH") .setOAuthConsumerSecret("nhEwYFfiX5qp90sLLwO2eeYMxLwb3WC120lgihrocZDPWRNcUK") .setOAuthAccessToken("94107100-572UpcOkkz9kMWGaJS8YFsIGdlmJAd2cDw8y9rOnA") .setOAuthAccessTokenSecret("ST0XtXUjYgYWKHryL2feNM0VcDQQAgrov2V7nB7hq1xBC"); TwitterFactory tf = new TwitterFactory(cb.build()); Twitter twitter = tf.getInstance(); JSONObject obj = new JSONObject(); int counterTweet = 0; FileWriter file = new FileWriter("Users\\user\\IdeaProjects\\TwitterStringMatching\\input.txt"); file.flush(); try { Query query = new Query("Satria"); QueryResult result; do { result = twitter.search(query); List<Status> tweets = result.getTweets(); for (Status tweet : tweets) { counterTweet++; System.out.println("@" + tweet.getUser().getScreenName() + " - " + tweet.getText()); obj.put("user", tweet.getUser().getScreenName()); obj.put("tweets", tweet.getText()); //Tulis file ke dalam txt try { file.write(obj.toString()); System.out.println("Successfully Copied JSON Object to File..."); System.out.println("\nJSON Object: " + obj); } catch (IOException e) { e.printStackTrace(); } } } while (counterTweet < 1000); file.close(); System.exit(0); } catch (TwitterException te) { te.printStackTrace(); System.out.println("Failed to search tweets: " + te.getMessage()); System.exit(-1); } System.exit(0); }
From source file:toninbot.ToninBot.java
/** * @param args the command line arguments *//*from w w w . ja v a 2 s. c om*/ public static void main(String[] args) { AccessToken accessToken = new AccessToken(Credenciales.token, Credenciales.tokenSecret); ConfigurationBuilder builder = new ConfigurationBuilder(); builder.setOAuthConsumerKey(Credenciales.consumerKey); builder.setOAuthConsumerSecret(Credenciales.consumerSecret); Configuration configuration = builder.build(); TwitterStreamFactory twStreamFactory = new TwitterStreamFactory(configuration); TwitterStream twitterStream = twStreamFactory.getInstance(); twitterStream.setOAuthAccessToken(accessToken); ToninStatusListener listener = new ToninStatusListener(); twitterStream.addListener(listener); FilterQuery filtre = new FilterQuery(); filtre.follow(184742273L, 2841338087L);//Allegue y proyectoPSIa1 //filtre.follow(2841338087L);//proyectoPSIa1 twitterStream.filter(filtre); }
From source file:toninbot.ToninStatusListener.java
public ToninStatusListener() { ConfigurationBuilder builder = new ConfigurationBuilder(); builder.setOAuthConsumerKey(Credenciales.consumerKey); builder.setOAuthConsumerSecret(Credenciales.consumerSecret); Configuration configuration = builder.build(); AccessToken accessToken = new AccessToken(Credenciales.token, Credenciales.tokenSecret); TwitterFactory twFactory = new TwitterFactory(configuration); twitter = twFactory.getInstance();//from w w w.ja v a2 s .co m twitter.setOAuthAccessToken(accessToken); randomGenerator = new Random(); //La lista de tweets respuestas = new ArrayList(); respuestas.add("Fora de horario! Este tweet esta fora de horario!"); respuestas.add("Eeeeh!! Estas fora de horario!"); respuestas.add("FORA DE HORARIO!"); }
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 {/* www .j a v a 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 v a2 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:tweekan.kotikan.com.tweekan.twitter.BasicTwitterClient.java
License:Open Source License
public BasicTwitterClient() { ConfigurationBuilder cb = new ConfigurationBuilder(); cb.setDebugEnabled(true).setOAuthConsumerKey(Twitter.consumerKey) .setOAuthConsumerSecret(Twitter.consumerSecret).setOAuthAccessToken(Twitter.accessToken) .setOAuthAccessTokenSecret(Twitter.accessTokenSecret); TwitterFactory twitterFactory = new TwitterFactory(cb.build()); instance = twitterFactory.getInstance(); }