diff options
author | Peter Wu <peter@lekensteyn.nl> | 2014-05-01 23:56:00 +0200 |
---|---|---|
committer | Peter Wu <peter@lekensteyn.nl> | 2014-05-01 23:56:00 +0200 |
commit | d68b9ac03cf2dbaf55d1e848c9df0a4b5957fc4e (patch) | |
tree | cbb2f932f4924b2ec9c8284f34c61bf4dc623e60 | |
parent | cb296b188093df4480cb720d3b5f4ab425ac6ec9 (diff) | |
download | TwitterDataAnalytics-d68b9ac03cf2dbaf55d1e848c9df0a4b5957fc4e.tar.gz |
Add command to print active keywords
-rw-r--r-- | src/io/StreamImpl.java | 15 | ||||
-rw-r--r-- | src/main/TweetShell.java | 22 | ||||
-rw-r--r-- | src/mining/Stream.java | 10 |
3 files changed, 47 insertions, 0 deletions
diff --git a/src/io/StreamImpl.java b/src/io/StreamImpl.java index fadd335..426d1fa 100644 --- a/src/io/StreamImpl.java +++ b/src/io/StreamImpl.java @@ -96,6 +96,21 @@ public class StreamImpl implements Stream { } @Override + public Set<String> getKeywords(boolean active) { + HashSet<String> retKeywords = new HashSet<>(); + if (active) { + // return keywords from the active connection + if (workerContainer != null) { + String keywordsStr = workerContainer.getWorker().getKeywords(); + retKeywords.addAll(Arrays.asList(keywordsStr.split(","))); + } + } else { + retKeywords.addAll(keywords); + } + return retKeywords; + } + + @Override public void commit() throws IOException { String keywordsStr = StringUtils.join(keywords, ","); /* do not reconnect if a connection already exists for the keywords */ diff --git a/src/main/TweetShell.java b/src/main/TweetShell.java index e1bf5b3..0c6b975 100644 --- a/src/main/TweetShell.java +++ b/src/main/TweetShell.java @@ -7,6 +7,7 @@ import java.io.IOException; import java.util.Arrays; import java.util.NoSuchElementException; import java.util.Scanner; +import java.util.Set; import java.util.logging.Level; import java.util.logging.Logger; import mining.Stream; @@ -149,6 +150,7 @@ public class TweetShell implements TwitterApi.PinSupplier { add("Adds a keyword to search", 1), del("Deletes a keyword from search", 1), + keywords("Display currently active keywords"), commit("Activate the stream or apply the stream keyword changes"), close("Close the stream"), exit("Returns to shell"), @@ -205,6 +207,26 @@ public class TweetShell implements TwitterApi.PinSupplier { case del: getStream().unwatchKeyword(params[0]); break; + case keywords: + Set<String> active = getStream().getKeywords(true); + Set<String> queued = getStream().getKeywords(false); + System.out.println("Keywords:"); + for (String keyword : active) { + System.out.print(" \"" + keyword + "\""); + if (!queued.contains(keyword)) { + System.out.println(" (will be removed)"); + } else { + System.out.println(); + } + } + queued.removeAll(active); + for (String keyword : queued) { + System.out.println(" \"" + keyword + "\" (will be added)"); + } + if (!queued.equals(active)) { + System.out.println("To apply changes, run 'commit'"); + } + break; case commit: getStream().commit(); break; diff --git a/src/mining/Stream.java b/src/mining/Stream.java index 36fba1b..1822bc2 100644 --- a/src/mining/Stream.java +++ b/src/mining/Stream.java @@ -1,6 +1,7 @@ package mining; import java.io.IOException; +import java.util.Set; /** * Provides access to a stream. The implementor is supposed to provide means @@ -44,4 +45,13 @@ public interface Stream { * @return true if connection can be made, false otherwise. */ public boolean isValid(); + + /** + * Determine keywords for the search query. + * + * @param active true to return keywords actually in use, false to return + * keywords that are queued for the next iteration. + * @return A list of keywords, possibly empty if there are none. + */ + public Set<String> getKeywords(boolean active); } |