summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorMaurice Laveaux <m.laveaux@student.tue.nl>2014-05-15 15:34:16 +0200
committerMaurice Laveaux <m.laveaux@student.tue.nl>2014-05-15 15:34:16 +0200
commitf7bbf62d33774b200972549afaf05240565fc2d3 (patch)
tree9dd9aa8af87c30689f02c24dd259a6b45a1f53e0 /src
parentfbcb596f8aff9a91362f8c16ae3d0d3f426e12b6 (diff)
downloadGoldfarmer-f7bbf62d33774b200972549afaf05240565fc2d3.tar.gz
Changed some formatting and javadoc.
* removed the public static ConnectionBuilder in main. * Changed comments to JavaDoc so it is better readable.
Diffstat (limited to 'src')
-rw-r--r--src/main/Analyzor.java102
-rw-r--r--src/main/FarmShell.java109
-rw-r--r--src/main/Main.java38
3 files changed, 140 insertions, 109 deletions
diff --git a/src/main/Analyzor.java b/src/main/Analyzor.java
index 8f11186..1f1dbfa 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;
@@ -15,31 +16,44 @@ 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;
-
- //reads the lexicons
- void readLexicon() throws FileNotFoundException{
+ /**
+ * A map for the unigrams to their weight.
+ */
+ private final HashMap<String, Double> unimap = new HashMap();
+
+ /**
+ * A map for the bigrams to their weight.
+ */
+ private final HashMap<String, Double> bimap = new HashMap();
+
+ /**
+ * The results gathered from the query
+ */
+ private ResultSet data;
- File uniFile = new File("unigrams-pmilexicon.txt"); // get uni
- File biFile = new File("bigrams-pmilexicon.txt"); // get bi
+ /**
+ * The connection to the database.
+ */
+ private Connection connection;
- Scanner uniScanner = new Scanner(uniFile);
- Scanner biScanner = new Scanner(biFile);
+ private ConnectionBuilder builder;
+
+ public Analyzor(ConnectionBuilder builder) {
+ this.builder = builder;
+ }
+
+ //reads the lexicons
+ 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()) {
@@ -47,6 +61,7 @@ public class Analyzor {
if (uniScanner.hasNextLine()) {
uniScanner.nextLine();
}
+ // NumberFormatException is not handled.
}
//fill the map of bigrams
@@ -55,56 +70,55 @@ public class Analyzor {
if (biScanner.hasNextLine()) {
biScanner.nextLine();
}
+ // NumberFormatException is not handled.
}
}
-
- //query the database
- //fills the ResultSet
- void Query(String query){
-
+
+ /**
+ * Querires the database with some query and handles the results.
+ *
+ * @param query A query to execute on the database.
+ */
+ public void Query(String query) {
PreparedStatement statement;
-
+
try {
- connection = Main.cb.create();
+ // Connection is not persistent this way.
+ connection = builder.create();
+
statement = connection.prepareStatement(query);
data = statement.executeQuery();
-
-
- }
- catch(SQLException ex){
- System.err.println("could not make a connection with the database"+ex);
+ } catch (SQLException ex) {
+ System.err.println("could not make a connection with the database" + ex);
}
}
- //analyzes the tweet on their positivity
- //this is just a base version
- void sentimentAnalysis() {
-
-
-
- try{
+ /**
+ * analyzes the tweet on their positivity based on the ratings from the
+ * unigrams and bigrams.
+ */
+ public void sentimentAnalysis() {
+ try {
readLexicon();
- }
- catch(FileNotFoundException ex){
+ } catch (FileNotFoundException ex) {
System.out.println("could not find the lexicons, please try again");
return;
}
-
+
Double value;
String text;
try {
- //for all tuples
while (data.next()) {
//get the text
text = data.getString("text");
// test is the tweet text you are going to analyze
String[] words = text.split("\\s+"); // text splitted into separate words
double positiverate = 0; // positive rating
-
+
// Rate the text with unigrams
for (String word : words) {
value = unimap.get(word);
- if(value != null){
+ if (value != null) {
positiverate += unimap.get(word);
}
}
@@ -118,7 +132,7 @@ public class Analyzor {
}
NamedPreparedStatement m_insertRating;
m_insertRating = new NamedPreparedStatement(connection, QueryUtils.insertRating);
- QueryUtils.setInsertParams(m_insertRating, data.getLong("tweetid"),data.getString("brand"), (int)(positiverate * 10));
+ QueryUtils.setInsertParams(m_insertRating, data.getLong("tweetid"), data.getString("brand"), (int) (positiverate * 10));
m_insertRating.executeUpdate();
//don't print the rate
//System.out.println(text + ": " + (int) (positiverate * 10));
diff --git a/src/main/FarmShell.java b/src/main/FarmShell.java
index daab973..80dce2e 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.util.Arrays;
import java.util.NoSuchElementException;
@@ -11,14 +12,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.
*/
@@ -67,7 +77,6 @@ public class FarmShell {
* otherwise.
*/
public boolean execute(String[] args) {
-
try {
Command command = Command.fromString(args[0]);
@@ -85,11 +94,55 @@ public class FarmShell {
// another satisfied customer, next!
return true;
}
+
+ private void execute(Command command, String[] params) throws IOException {
+ if (params.length < command.getParamCount()) {
+ throw new IllegalArgumentException("Expected "
+ + command.getParamCount() + " parameters, got only "
+ + params.length);
+ }
+ switch (command) {
+ case query:
+ //make a new Analyzor
+ analyzor.Query(params[0]);
+ break;
+ case filterbots:
+ System.out.println("not yet implemented");
+ break;
+ case sentiment:
+ analyzor.sentimentAnalysis();
+ 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"),
+ filterbots("Filters and marks the bots in all the users"),
sentiment("analyzes all tweets on positivity (about a brand)"),
exit("Returns to shell"),
help("Get help");
@@ -130,46 +183,4 @@ public class FarmShell {
"",
"Available commands:"
};
-
- private void execute(Command command, String[] params) throws IOException {
- if (params.length < command.getParamCount()) {
- throw new IllegalArgumentException("Expected "
- + command.getParamCount() + " parameters, got only "
- + params.length);
- }
- switch (command) {
- case query:
- //make a new Analyzor
- analyzor = new Analyzor();
- analyzor.Query(params[0]);
- break;
- case filterbots:
- System.out.println("not yet implemented");
- break;
- case sentiment:
- analyzor.sentimentAnalysis();
- 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];
}