List of usage examples for twitter4j FilterQuery language
String[] language
To view the source code for twitter4j FilterQuery language.
Click Source Link
From source file:crawlertwitter.TwitterDataStream.java
public void startTwitter(String[] keywords) { for (int i = 0; i < keywords.length; i++) { this.label += "_" + keywords[i]; }/* www . ja v a2 s . c o m*/ twitterStream.addListener(listener); System.out.println("Starting down Twitter stream..."); FilterQuery query = new FilterQuery(); // query.track(keywords); query.language(this.lang); // by filter twitterStream.filter(query); // twitterStream.sample(); }
From source file:edu.csupomona.nlp.tool.crawler.Twitter.java
/** * Query with given keywords. Crawling will start immediately. * @param filename File name for the query result * @param keywords Array of keywords *//*from w w w. j a v a2s . c om*/ public void query(String filename, String[] keywords) { // prepare for the new query queryDone_ = false; // construct file name filename_ = filename; // init tweet list tweet_ = new ArrayList<>(); // init id set idSet_ = loadSet(); // calculate ETA time Calendar cal = Calendar.getInstance(); etaTime.setTimeInMillis(cal.getTimeInMillis() + hourLimit_ * 3600 * 1000); // debug info System.out.println("Querying for => " + filename_); // construct FilterQuery FilterQuery fQuery = new FilterQuery(); fQuery.track(keywords); // track specified keywords String[] languages = { lang_ }; fQuery.language(languages); // track specified language // start streaming with FilterQuery ts_.filter(fQuery); }
From source file:nlptexthatespeechdetection.dataCollection.GetTwitterDoc2VecTrainingData.java
public static void main(String[] args) throws FileNotFoundException, IOException { File dir = new File(folderName); if (!dir.exists()) dir.mkdir();// www.ja v a 2 s .c o m if (!dir.isDirectory()) { System.out.println(folderName + " is not a directory"); return; } System.out.println("number of tweets required: "); int numTweetsRequired = (new Scanner(System.in)).nextInt(); String path = folderName + "/" + fileName; File file = new File(path); if (!file.exists()) file.createNewFile(); FileWriter writer = new FileWriter(path, true); TwitterStream twitterStream = new TwitterStreamFactory().getInstance(); StatusListener listener = new StatusListener() { int numTweets = 0; @Override public void onStatus(Status status) { if (status.getLang().equals("in")) { try { String statusText = status.getText(); writer.write("\n"); writer.write(statusText); numTweets++; System.out.println("numTweets: " + numTweets); if (numTweets >= numTweetsRequired) { writer.close(); System.exit(0); } } catch (IOException ex) { Logger.getLogger(GetTwitterDoc2VecTrainingData.class.getName()).log(Level.SEVERE, null, ex); } } } @Override public void onDeletionNotice(StatusDeletionNotice statusDeletionNotice) { // System.out.println("Got a status deletion notice id:" + statusDeletionNotice.getStatusId()); } @Override public void onTrackLimitationNotice(int numberOfLimitedStatuses) { System.out.println("Got track limitation notice:" + numberOfLimitedStatuses); } @Override public void onScrubGeo(long userId, long upToStatusId) { System.out.println("Got scrub_geo event userId:" + userId + " upToStatusId:" + upToStatusId); } @Override public void onStallWarning(StallWarning warning) { System.out.println("Got stall warning:" + warning); } @Override public void onException(Exception ex) { ex.printStackTrace(); } }; twitterStream.addListener(listener); FilterQuery filterQuery = new FilterQuery(); filterQuery.track(new String[] { "a", "i", "u", "e", "o" }); filterQuery.language("in"); twitterStream.filter(filterQuery); }
From source file:nlptexthatespeechdetection.dataCollection.TwitterStreamingAnnotator.java
public static void main(String[] args) throws NotDirectoryException { Scanner sc = new Scanner(System.in); System.out.println("Nama Anda (sebagai anotator): "); String namaAnotator = sc.nextLine(); AnnotatedDataFolder annotatedDataFolder = new AnnotatedDataFolder(dataFolderName); TwitterStream twitterStream = new TwitterStreamFactory().getInstance(); StatusListener listener = new StatusListener() { @Override/*www . j a va2 s .c o m*/ public void onStatus(Status status) { if (status.getLang().equals("in")) { System.out.println(); System.out.println(); System.out.println("=======ANOTASI======="); System.out.println("status: " + status.getText()); System.out.println(); System.out.println("is this a hate speech?(y/n. any other if you do not know)"); String annotatorResponse = sc.nextLine().trim().toLowerCase(); Date date = new Date(); String dateString = dateFormat.format(date); try { if (annotatorResponse.equals("y")) { String filePath = annotatedDataFolder.saveHateSpeechString(namaAnotator, dateString, status.getText()); System.out.println("Saved data to: " + filePath); } else if (annotatorResponse.equals("n")) { String filePath = annotatedDataFolder.saveNotHateSpeechString(namaAnotator, dateString, status.getText()); System.out.println("Saved data to: " + filePath); } System.out.println("thank you!"); } catch (FileNotFoundException ex) { ex.printStackTrace(); } catch (IOException ex) { Logger.getLogger(TwitterStreamingAnnotator.class.getName()).log(Level.SEVERE, null, ex); } } else { System.out.println("ignoring non-indonesian tweet"); } // if (status.getGeoLocation() != null) { // System.out.println("@" + status.getUser().getScreenName() + " - " + status.getText() + " * " + status.getId() + " $ " + status.getGeoLocation().toString()); // } // if (status.getLang().equals("id")) { // System.out.println("@" + status.getUser().getScreenName() + " - " + status.getText() + " * " + status.getId() + " # " + status.getLang() + " $ " + (status.getGeoLocation() == null ? "NULLGEO" : status.getGeoLocation().toString())); // } } @Override public void onDeletionNotice(StatusDeletionNotice statusDeletionNotice) { // System.out.println("Got a status deletion notice id:" + statusDeletionNotice.getStatusId()); } @Override public void onTrackLimitationNotice(int numberOfLimitedStatuses) { System.out.println("Got track limitation notice:" + numberOfLimitedStatuses); } @Override public void onScrubGeo(long userId, long upToStatusId) { System.out.println("Got scrub_geo event userId:" + userId + " upToStatusId:" + upToStatusId); } @Override public void onStallWarning(StallWarning warning) { System.out.println("Got stall warning:" + warning); } @Override public void onException(Exception ex) { ex.printStackTrace(); } }; twitterStream.addListener(listener); FilterQuery filterQuery = new FilterQuery(); filterQuery.track(new String[] { "a", "i", "u", "e", "o" }); filterQuery.language("in"); twitterStream.filter(filterQuery); }
From source file:nlptexthatespeechdetection.NLPTextHateSpeechDetection.java
/** * @param args the command line arguments *///from w ww . j a va2s . com public static void main(String[] args) throws TwitterException, NotDirectoryException, IOException { HateSpeechClassifier1 classifier = new HateSpeechClassifier1(); AnnotatedDataFolder data = new AnnotatedDataFolder("data"); boolean overSampling = false; classifier.train(data.getDateSortedLabeledData(overSampling)); TwitterStream twitterStream = new TwitterStreamFactory().getInstance(); StatusListener listener = new StatusListener() { int numHateSpeech = 0; int numTweets = 0; @Override public void onStatus(Status status) { if (status.getLang().equals("in")) { numTweets++; if (classifier.isHateSpeech(status.getText(), 0.5)) { System.out.println("@" + status.getUser().getScreenName() + " - " + status.getText() + " * " + status.getId() + " # " + status.getLang() + " $ " + (status.getGeoLocation() == null ? "NULLGEO" : status.getGeoLocation().toString())); System.out.println(); System.out.println("lang: " + status.getLang()); System.out.println("number of detected hate speech: " + numHateSpeech); System.out.println("total number of streamed tweets: " + numTweets); System.out.println(); System.out.println(); numHateSpeech++; } } else { System.out.println("ignoring non-Indonesian tweet"); } // if (status.getGeoLocation() != null) { // System.out.println("@" + status.getUser().getScreenName() + " - " + status.getText() + " * " + status.getId() + " $ " + status.getGeoLocation().toString()); // } // if (status.getLang().equals("id")) { // System.out.println("@" + status.getUser().getScreenName() + " - " + status.getText() + " * " + status.getId() + " # " + status.getLang() + " $ " + (status.getGeoLocation() == null ? "NULLGEO" : status.getGeoLocation().toString())); // } } @Override public void onDeletionNotice(StatusDeletionNotice statusDeletionNotice) { // System.out.println("Got a status deletion notice id:" + statusDeletionNotice.getStatusId()); } @Override public void onTrackLimitationNotice(int numberOfLimitedStatuses) { System.out.println("Got track limitation notice:" + numberOfLimitedStatuses); } @Override public void onScrubGeo(long userId, long upToStatusId) { System.out.println("Got scrub_geo event userId:" + userId + " upToStatusId:" + upToStatusId); } @Override public void onStallWarning(StallWarning warning) { System.out.println("Got stall warning:" + warning); } @Override public void onException(Exception ex) { ex.printStackTrace(); } }; twitterStream.addListener(listener); FilterQuery filterQuery = new FilterQuery(); filterQuery.track(new String[] { "a", "i", "u", "e", "o" }); filterQuery.language("in"); twitterStream.filter(filterQuery); twitterStream.sample(); }
From source file:org.apache.asterix.external.util.TwitterUtil.java
License:Apache License
public static FilterQuery getFilterQuery(Map<String, String> configuration) throws AsterixException { String locationValue = null;//from w ww. java 2 s. c om // For backward compatibility if (configuration.containsKey(ConfigurationConstants.KEY_LOCATIONS)) { locationValue = configuration.get(ConfigurationConstants.KEY_LOCATIONS); } else { locationValue = configuration.get(ConfigurationConstants.KEY_LOCATION); } String langValue = configuration.get(ConfigurationConstants.LANGUAGES); String trackValue = configuration.get(ConfigurationConstants.TRACK); FilterQuery filterQuery = null; // Terms to track if (trackValue != null) { String keywords[] = null; filterQuery = new FilterQuery(); if (trackValue.contains(",")) { keywords = trackValue.trim().split(",\\s*"); } else { keywords = new String[] { trackValue }; } filterQuery = filterQuery.track(keywords); } // Language filtering parameter if (langValue != null) { if (filterQuery == null) { filterQuery = new FilterQuery(); } String languages[]; if (langValue.contains(",")) { languages = langValue.trim().split(",\\s*"); } else { languages = new String[] { langValue }; } filterQuery = filterQuery.language(languages); } // Location filtering parameter if (locationValue != null) { double[][] locations = getBoundingBoxes(locationValue); if (locations != null) { if (filterQuery == null) { filterQuery = new FilterQuery(); } filterQuery = filterQuery.locations(locations); } } // Filtering level: none, low or medium (defaul=none) if (filterQuery != null) { String filterValue = configuration.get(ConfigurationConstants.FILTER_LEVEL); if (filterValue != null) { filterQuery = filterQuery.filterLevel(filterValue); } } return filterQuery; }
From source file:org.apache.storm.starter.spout.Q2TwitterSampleSpout.java
License:Apache License
@Override public void open(Map conf, TopologyContext context, SpoutOutputCollector collector) { queue = new LinkedBlockingQueue<Status>(1000); _collector = collector;/*from w ww . j av a 2s . com*/ StatusListener listener = new StatusListener() { @Override public void onStatus(Status status) { queue.offer(status); } @Override public void onDeletionNotice(StatusDeletionNotice sdn) { } @Override public void onTrackLimitationNotice(int i) { } @Override public void onScrubGeo(long l, long l1) { } @Override public void onException(Exception ex) { } @Override public void onStallWarning(StallWarning arg0) { // TODO Auto-generated method stub } }; _twitterStream = new TwitterStreamFactory(new ConfigurationBuilder().setJSONStoreEnabled(true).build()) .getInstance(); _twitterStream.addListener(listener); _twitterStream.setOAuthConsumer(consumerKey, consumerSecret); AccessToken token = new AccessToken(accessToken, accessTokenSecret); _twitterStream.setOAuthAccessToken(token); if (keyWords.length == 0) { _twitterStream.sample(); } else { FilterQuery query = new FilterQuery().track(keyWords); query.language(new String[] { "en" }); query.track(new String[] { "job", "vote", "local", "Clinton", "Trump", "debate", "tech", "TheWalkingDead", "android", "Election", "USA", "GOT7", "Empire", "Apple", "ISIS", "election", "Football", "iPhone", "Amazon", "uber" }); _twitterStream.filter(query); } }
From source file:org.onepercent.utils.twitterstream.agent.src.main.java.com.cloudera.flume.source.TwitterSource.java
License:Apache License
/** * Start processing events. This uses the Twitter Streaming API to sample * Twitter, and process tweets./*from ww w .jav a 2 s .c om*/ */ @Override public void start() { // The channel is the piece of Flume that sits between the Source and Sink, // and is used to process events. final ChannelProcessor channel = getChannelProcessor(); final Map<String, String> headers = new HashMap<String, String>(); // The StatusListener is a twitter4j API, which can be added to a Twitter // stream, and will execute methods every time a message comes in through // the stream. StatusListener listener = new StatusListener() { // The onStatus method is executed every time a new tweet comes in. public void onStatus(Status status) { // The EventBuilder is used to build an event using the headers and // the raw JSON of a tweet logger.debug(status.getUser().getScreenName() + ": " + status.getText()); headers.put("timestamp", String.valueOf(status.getCreatedAt().getTime())); Event event = EventBuilder.withBody(DataObjectFactory.getRawJSON(status).getBytes(), headers); channel.processEvent(event); } // This listener will ignore everything except for new tweets public void onDeletionNotice(StatusDeletionNotice statusDeletionNotice) { } public void onTrackLimitationNotice(int numberOfLimitedStatuses) { } public void onScrubGeo(long userId, long upToStatusId) { } public void onException(Exception ex) { } public void onStallWarning(StallWarning warning) { } }; logger.debug("Setting up Twitter sample stream using consumer key {} and" + " access token {}", new String[] { consumerKey, accessToken }); // Set up the stream's listener (defined above), twitterStream.addListener(listener); // Set up a filter to pull out industry-relevant tweets if (keywords.length == 0) { logger.debug("Starting up Twitter sampling..."); twitterStream.sample(); } else if (keywords.length > 0) { if (languages.length == 0) { logger.debug("Starting up Twitter Keyword filtering..."); FilterQuery query = new FilterQuery().track(keywords); twitterStream.filter(query); } else { logger.debug("Starting up Twitter Keyword and Language filtering..."); FilterQuery query = new FilterQuery(); query.track(keywords); query.language(languages); twitterStream.filter(query); } } super.start(); }
From source file:org.selman.tweetamo.TweetamoClient.java
License:Apache License
public static void main(String[] args) throws Exception { if (args.length != 2) { System.out.println("Usage: [language] [search topic]"); }// w w w.j a v a 2 s. c o m kinesisClient = new AmazonKinesisClient(new ClasspathPropertiesFileCredentialsProvider()); waitForStreamToBecomeAvailable(STREAM_NAME); LOG.info("Publishing tweets to stream : " + STREAM_NAME); StatusListener listener = new StatusListener() { public void onStatus(Status status) { try { PutRecordRequest putRecordRequest = new PutRecordRequest(); putRecordRequest.setStreamName(STREAM_NAME); putRecordRequest.setData(TweetSerializer.toBytes(status)); putRecordRequest.setPartitionKey(status.getUser().getScreenName()); PutRecordResult putRecordResult = kinesisClient.putRecord(putRecordRequest); LOG.info("Successfully putrecord, partition key : " + putRecordRequest.getPartitionKey() + ", ShardID : " + putRecordResult.getShardId()); } catch (Exception e) { LOG.error("Failed to putrecord", e); } } public void onDeletionNotice(StatusDeletionNotice statusDeletionNotice) { } public void onTrackLimitationNotice(int numberOfLimitedStatuses) { } public void onException(Exception ex) { ex.printStackTrace(); } @Override public void onScrubGeo(long arg0, long arg1) { } @Override public void onStallWarning(StallWarning arg0) { } }; ClasspathTwitterCredentialsProvider provider = new ClasspathTwitterCredentialsProvider(); TwitterCredentials credentials = provider.getTwitterCredentials(); ConfigurationBuilder cb = new ConfigurationBuilder(); cb.setDebugEnabled(true).setOAuthConsumerKey(credentials.getConsumerKey()) .setOAuthConsumerSecret(credentials.getConsumerSecret()) .setOAuthAccessToken(credentials.getAccessToken()) .setOAuthAccessTokenSecret(credentials.getAccessTokenSecret()); TwitterStream twitterStream = new TwitterStreamFactory(cb.build()).getInstance(); twitterStream.addListener(listener); FilterQuery filterQuery = new FilterQuery(); filterQuery.language(new String[] { args[0] }); filterQuery.track(new String[] { args[1] }); twitterStream.filter(filterQuery); }
From source file:org.twitter.sample.main.Stream.java
public void getTweetsFromTwitter() { StatusListener listener = new StatusListener() { private boolean logQueueFull = true; @Override/* w ww .ja v a 2 s. co m*/ public void onException(Exception e) { log.error(e); } @Override public void onDeletionNotice(StatusDeletionNotice notice) { } @Override public void onScrubGeo(long arg0, long arg1) { } @Override public void onStatus(Status status) { db.insert(status); } @Override public void onTrackLimitationNotice(int notice) { log.warn("*** TRACK LIMITATION REACHED: " + notice + " ***"); } @Override public void onStallWarning(StallWarning arg0) { log.warn("*** STALL WARNING: " + arg0); } }; ConfigurationBuilder twitterConfig = new ConfigurationBuilder(); twitterConfig.setDebugEnabled(false); twitterConfig.setOAuthAccessTokenSecret(access_token_secret); twitterConfig.setOAuthAccessToken(access_token); twitterConfig.setOAuthConsumerKey(consumer_key); twitterConfig.setOAuthConsumerSecret(consumer_secret); twitterConfig.setJSONStoreEnabled(true); TwitterStreamFactory fact = new TwitterStreamFactory(twitterConfig.build()); this.twitterStream = fact.getInstance(); this.twitterStream.addListener(listener); FilterQuery filterQuery = new FilterQuery(); filterQuery.language(new String[] { "en" }); this.twitterStream.filter(filterQuery); this.twitterStream.sample(); }