diff options
Diffstat (limited to 'src/main')
-rw-r--r-- | src/main/Analyzor.java | 78 | ||||
-rw-r--r-- | src/main/FarmShell.java | 116 | ||||
-rw-r--r-- | src/main/Main.java | 38 |
3 files changed, 141 insertions, 91 deletions
diff --git a/src/main/Analyzor.java b/src/main/Analyzor.java index 51e080e..3eb93f9 100644 --- a/src/main/Analyzor.java +++ b/src/main/Analyzor.java @@ -5,6 +5,7 @@ */
package main;
+import database.ConnectionBuilder;
import database.NamedPreparedStatement;
import database.QueryUtils;
import java.io.File;
@@ -16,31 +17,47 @@ import java.sql.ResultSet; import java.sql.SQLException;
import java.util.HashMap;
import java.util.Scanner;
-import java.util.logging.Level;
-import java.util.logging.Logger;
/**
- *
- * @author s123188
+ * The sentiment analysis class that rates tweets based on a unigram and
+ * bigram set of weights.
*/
public class Analyzor {
- //maps for the lexicons
- HashMap<String, Double> unimap = new HashMap<String, Double>(); // Map for uni
- HashMap<String, Double> bimap = new HashMap<String, Double>(); // Map for bi
-
- //the resultset of the query or the import
- ResultSet data;
- Connection connection;
+ /**
+ * The map that matches single words to their weights.
+ */
+ private final HashMap<String, Double> unimap = new HashMap();
+
+ /**
+ * The map that matches word pairs to their weights.
+ */
+ private final HashMap<String, Double> bimap = new HashMap();
+
+ /**
+ * The results of the query (can be null)
+ */
+ private ResultSet data = null;
+
+ /**
+ * The connection to the database.
+ */
+ private Connection connection;
+
+ /**
+ * The connection builder to initialize the connection.
+ */
+ private ConnectionBuilder builder;
+
+ public Analyzor(ConnectionBuilder builder) {
+ this.builder = builder;
+ }
//reads the lexicons
- void readLexicon() throws FileNotFoundException {
-
- File uniFile = new File("unigrams-pmilexicon.txt"); // get uni
- File biFile = new File("bigrams-pmilexicon.txt"); // get bi
-
- Scanner uniScanner = new Scanner(uniFile);
- Scanner biScanner = new Scanner(biFile);
+ private void readLexicon() throws FileNotFoundException {
+ //TODO: fix? hardcoded filenames.
+ Scanner uniScanner = new Scanner(new File("unigrams-pmilexicon.txt"));
+ Scanner biScanner = new Scanner(new File("bigrams-pmilexicon.txt"));
//Fill the map of unigrams
while (uniScanner.hasNext()) {
@@ -48,6 +65,7 @@ public class Analyzor { if (uniScanner.hasNextLine()) {
uniScanner.nextLine();
}
+ // NumberFormatException is not handled.
}
//fill the map of bigrams
@@ -56,24 +74,32 @@ public class Analyzor { if (biScanner.hasNextLine()) {
biScanner.nextLine();
}
+ // NumberFormatException is not handled.
}
}
- //query the database
- //fills the ResultSet data
- void query(String query) throws SQLException {
+ /**
+ * Executes a query that the analyzer can analyze.
+ *
+ * @param query The query string to execute.
+ * @throws SQLException When database connection isn't available.
+ */
+ public void query(String query) throws SQLException {
PreparedStatement statement;
//make a connection to the database and execute the query
- connection = Main.cb.create();
+ connection = builder.create();
statement = connection.prepareStatement(query);
data = statement.executeQuery();
}
- //analyzes the tweet on their positivity
- //this is just a base version
- void sentimentAnalysis(String query) throws SQLException, IOException {
-
+ /**
+ * Run a sentiment analysis and fill the database with the output.
+ *
+ * @throws SQLException
+ * @throws IOException
+ */
+ public void sentimentAnalysis(String query) throws SQLException, IOException {
query(query);
//read the lexicons
diff --git a/src/main/FarmShell.java b/src/main/FarmShell.java index 93cb928..f3c8011 100644 --- a/src/main/FarmShell.java +++ b/src/main/FarmShell.java @@ -1,5 +1,6 @@ package main;
+import database.ConnectionBuilder;
import java.io.IOException;
import java.sql.SQLException;
import java.util.Arrays;
@@ -12,14 +13,23 @@ import java.util.Scanner; */
public class FarmShell {
+ /**
+ * A scanner for the stdin.
+ */
private final Scanner scanner = new Scanner(System.in);
- Analyzor analyzor;
+ /**
+ * The sentiment analysis class.
+ */
+ private final Analyzor analyzor;
- private void printPrompt() {
- System.out.print("$ ");
+ /**
+ * @param builder The connection builder for the database.
+ */
+ public FarmShell(final ConnectionBuilder builder) {
+ analyzor = new Analyzor(builder);
}
-
+
/**
* Processes commands from stdin until the exit command is received or EOF.
*/
@@ -50,6 +60,12 @@ public class FarmShell { throw new NoSuchElementException();
}
+ /**
+ * Execute a single commands.
+ *
+ * @param cmd A single line of the command.
+ * @return Whether to continue or exit the application.
+ */
public boolean execute(String cmd) {
String[] args = cmd.trim().split("\\s+", 2);
if (!args[0].isEmpty()) {
@@ -68,7 +84,6 @@ public class FarmShell { * otherwise.
*/
public boolean execute(String[] args) {
-
try {
Command command = Command.fromString(args[0]);
@@ -88,9 +103,55 @@ public class FarmShell { // another satisfied customer, next!
return true;
}
+
+ private void execute(Command command, String[] params) throws SQLException, IOException {
+ if (params.length < command.getParamCount()) {
+ throw new IllegalArgumentException("Expected "
+ + command.getParamCount() + " parameters, got only "
+ + params.length);
+ }
+ switch (command) {
+ case query:
+ System.err.println("isn't supported anymore, now enter query after analysis type");
+ break;
+ case filterbots:
+ System.out.println("not yet implemented");
+ break;
+ case sentiment:
+ analyzor.sentimentAnalysis(params[0]);
+ break;
+ case wordcloud:
+ analyzor.makeWordCloud(params[0]);
+ break;
+ case help:
+ for (String line : HELP) {
+ System.out.println(line);
+ }
+ for (Command cmd : Command.values()) {
+ System.out.printf(" %-10s", cmd.name());
+ if (!cmd.getDescription().isEmpty()) {
+ System.out.print(" " + cmd.getDescription());
+ }
+ if (cmd.getParamCount() == 1) {
+ System.out.print(" (1 arg)");
+ } else if (cmd.getParamCount() > 1) {
+ System.out.printf(" (%d args)", cmd.getParamCount());
+ }
+ System.out.println();
+ }
+ break;
+ case exit:
+ throw new NoSuchElementException();
+ default:
+ throw new AssertionError(command.name());
+ }
+ }
+
+ private void printPrompt() {
+ System.out.print("$ ");
+ }
enum Command {
-
query("make a query to the database; needed to do analysis", 1),
filterbots("marks all users as bot or not", 1),
sentiment("analyzes all tweets on positivity (about a brand)", 1),
@@ -134,47 +195,4 @@ public class FarmShell { "",
"Available commands:"
};
-
- private void execute(Command command, String[] params) throws SQLException, IOException {
- if (params.length < command.getParamCount()) {
- throw new IllegalArgumentException("Expected "
- + command.getParamCount() + " parameters, got only "
- + params.length);
- }
- switch (command) {
- case query:
- System.err.println("isn't supported anymore, now enter query after analysis type");
- break;
- case filterbots:
- System.out.println("not yet implemented");
- break;
- case sentiment:
- analyzor.sentimentAnalysis(params[0]);
- break;
- case wordcloud:
- analyzor.makeWordCloud(params[0]);
- break;
- case help:
- for (String line : HELP) {
- System.out.println(line);
- }
- for (Command cmd : Command.values()) {
- System.out.printf(" %-10s", cmd.name());
- if (!cmd.getDescription().isEmpty()) {
- System.out.print(" " + cmd.getDescription());
- }
- if (cmd.getParamCount() == 1) {
- System.out.print(" (1 arg)");
- } else if (cmd.getParamCount() > 1) {
- System.out.printf(" (%d args)", cmd.getParamCount());
- }
- System.out.println();
- }
- break;
- case exit:
- throw new NoSuchElementException();
- default:
- throw new AssertionError(command.name());
- }
- }
}
diff --git a/src/main/Main.java b/src/main/Main.java index 1766332..9102ecd 100644 --- a/src/main/Main.java +++ b/src/main/Main.java @@ -1,32 +1,37 @@ +/* + * DO WHAT THE FUCK YOU WANT TO PUBLIC LICENSE Version 2, December 2004 + * + * Copyright (C) 2004 Sam Hocevar + * + * Everyone is permitted to copy and distribute verbatim or modified copies + * of this license document, and changing it is allowed as long as the name is + * changed. + * + * DO WHAT THE FUCK YOU WANT TO PUBLIC LICENSE TERMS AND CONDITIONS FOR COPYING, + * DISTRIBUTION AND MODIFICATION + * + * 0. You just DO WHAT THE FUCK YOU WANT TO. + */ package main; import database.ConnectionBuilder; -import java.io.File; -import java.io.FileNotFoundException; -import java.io.IOException; import java.util.Arrays; -import java.util.HashMap; -import java.util.Scanner; /** * The main class containing the main method. - * - * @author Maurice Laveaux */ public class Main { - static public ConnectionBuilder cb; - public static void main(String[] args) { - Main main; try { - main = new Main(args); + Main main = new Main(args); } catch (IllegalArgumentException ex) { System.err.println(ex.getMessage()); - System.exit(1); - return; } } + + private final ConnectionBuilder cb; + private String[] leftover_params; public Main(String[] args) { @@ -35,10 +40,12 @@ public class Main { .setUsername("twitter") .setPassword("2IOC02") .setDbName("twitter"); + parseGlobalOptions(args); + try { - FarmShell shell = new FarmShell(); + FarmShell shell = new FarmShell(cb); if (leftover_params != null && leftover_params.length > 0) { shell.execute(leftover_params); } @@ -74,8 +81,7 @@ public class Main { private String getArg(String[] params, int index, String name) { if (index >= params.length) { - System.err.println("Missing argument for parameter " + name); - System.exit(1); + throw new IllegalArgumentException("Missing argument for parameter " + name); } return params[index]; } |