summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorMaurice Laveaux <m.laveaux@student.tue.nl>2014-05-15 16:02:00 +0200
committerMaurice Laveaux <m.laveaux@student.tue.nl>2014-05-15 16:02:00 +0200
commit2f774cf6b2d5b32217f27ec4061deb3d7e0cde4b (patch)
tree44214c0a6f46348da4bc2143c6c7a56bd455d139 /src
parent014e93b9bb9c9957aa6ea28053833bc7a700cfee (diff)
downloadGoldfarmer-2f774cf6b2d5b32217f27ec4061deb3d7e0cde4b.tar.gz
Committed the initial BrandChecker class.
* Made some changes to the Javadoc of classes. * Removed the public static field, bad OOP.
Diffstat (limited to 'src')
-rw-r--r--src/analysis/BrandChecker.java61
-rw-r--r--src/main/Analyzor.java146
-rw-r--r--src/main/FarmShell.java11
3 files changed, 161 insertions, 57 deletions
diff --git a/src/analysis/BrandChecker.java b/src/analysis/BrandChecker.java
new file mode 100644
index 0000000..6b57a39
--- /dev/null
+++ b/src/analysis/BrandChecker.java
@@ -0,0 +1,61 @@
+/*
+ * 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 analysis;
+
+import java.io.FileInputStream;
+import java.io.FileNotFoundException;
+import java.io.InputStream;
+import java.util.List;
+import java.util.logging.Level;
+import java.util.logging.Logger;
+
+/**
+ * This class obtains a text and returns the brands that are contained in this
+ * text.
+ *
+ * @author Maurice Laveaux
+ */
+public class BrandChecker {
+
+ /**
+ * @param filename The filename that contains all the brands.
+ */
+ public BrandChecker(final String filename) {
+ try {
+ readFile(filename);
+ } catch (FileNotFoundException ex) {
+ Logger.getLogger(BrandChecker.class.getName()).log(Level.SEVERE, null, ex);
+ }
+ }
+
+ /**
+ * Get the brands that are in some text.
+ *
+ * @param text Any valid text.
+ * @return The list of brands that are contained in this text or null.
+ */
+ public List<String> getBrands(String text) {
+
+
+
+ return null;
+ }
+
+ private void readFile(final String filename) throws FileNotFoundException {
+
+ InputStream inFile = new FileInputStream(filename);
+
+ }
+}
diff --git a/src/main/Analyzor.java b/src/main/Analyzor.java
index 1f1dbfa..c84f6cd 100644
--- a/src/main/Analyzor.java
+++ b/src/main/Analyzor.java
@@ -10,6 +10,7 @@ import database.NamedPreparedStatement;
import database.QueryUtils;
import java.io.File;
import java.io.FileNotFoundException;
+import java.io.IOException;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
@@ -24,25 +25,28 @@ import java.util.Scanner;
public class Analyzor {
/**
- * A map for the unigrams to their weight.
+ * The map that matches single words to their weights.
*/
private final HashMap<String, Double> unimap = new HashMap();
-
+
/**
- * A map for the bigrams to their weight.
+ * The map that matches word pairs to their weights.
*/
private final HashMap<String, Double> bimap = new HashMap();
/**
- * The results gathered from the query
+ * The results of the query (can be null)
*/
- private ResultSet data;
-
+ 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) {
@@ -75,70 +79,100 @@ public class Analyzor {
}
/**
- * Querires the database with some query and handles the results.
- *
- * @param query A query to execute on the database.
+ * 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) {
- PreparedStatement statement;
+ void Query(String query) throws SQLException {
- try {
- // 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);
- }
+ PreparedStatement statement;
+ //make a connection to the database and execute the query
+ connection = builder.create();
+ statement = connection.prepareStatement(query);
+ data = statement.executeQuery();
}
/**
- * analyzes the tweet on their positivity based on the ratings from the
- * unigrams and bigrams.
+ * Run a sentiment analysis and fill the database with the output.
+ *
+ * @throws SQLException
+ * @throws IOException
*/
- public void sentimentAnalysis() {
- try {
- readLexicon();
- } catch (FileNotFoundException ex) {
- System.out.println("could not find the lexicons, please try again");
+ public void sentimentAnalysis() throws SQLException, IOException {
+ //read the lexicons
+ readLexicon();
+
+ //go to the start of te dataset
+ if (data == null) {
+ System.err.println("data is empty, try querying first");
return;
}
+ data.beforeFirst();
Double value;
String text;
- try {
- 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) {
- positiverate += unimap.get(word);
- }
+
+ //for all tuples
+ while (data.next()) {
+ //get the text
+ text = data.getString("text");
+ text = replacePunct(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) {
+ positiverate += unimap.get(word);
}
- // Rate the text with bigrams
- for (int i = 0; i < words.length - 1; i++) {
- String pair = words[i] + " " + words[i + 1];
- value = bimap.get(pair);
- if (value != null) {
- positiverate += bimap.get(pair);
- }
+ }
+ // Rate the text with bigrams
+ for (int i = 0; i < words.length - 1; i++) {
+ String pair = words[i] + " " + words[i + 1];
+ value = bimap.get(pair);
+ if (value != null) {
+ positiverate += bimap.get(pair);
}
- NamedPreparedStatement m_insertRating;
- m_insertRating = new NamedPreparedStatement(connection, QueryUtils.insertRating);
- 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));
}
- } catch (SQLException ex) {
- System.err.println("text not found");
+ //insert the rating into the database
+ NamedPreparedStatement m_insertRating;
+ m_insertRating = new NamedPreparedStatement(connection, QueryUtils.insertRating);
+ 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));
}
}
+
+ //makes a wordcloud of the tweets in the ResultSet data
+ private void makeWordCloud() throws SQLException {
+ //go to the start of the ResultSet data
+ if (data == null) {
+ System.err.println("data is empty, try querying first");
+ return;
+ }
+
+ //make the hashmap with the words and their frequency
+ HashMap<String, Integer> wordcloud = new HashMap<>();
+
+ //set the pointer at the start of the ResultSet
+ data.beforeFirst();
+
+ while(data.next()){
+ //get the text
+
+ }
+ }
+
+ //replaces punctuation so it will be splitted
+ private String replacePunct(String text) {
+ text = text.replaceAll("https?://\\S*", "");
+ text = text.replaceAll("[!?):;\"']", " $0");
+ text = text.replaceAll("[.,-](\\s|$)", " $0");
+ text = text.replaceAll("\\s[(\"']", "$0 ");
+ return text;
+ }
}
diff --git a/src/main/FarmShell.java b/src/main/FarmShell.java
index 80dce2e..69e29a9 100644
--- a/src/main/FarmShell.java
+++ b/src/main/FarmShell.java
@@ -2,6 +2,7 @@ package main;
import database.ConnectionBuilder;
import java.io.IOException;
+import java.sql.SQLException;
import java.util.Arrays;
import java.util.NoSuchElementException;
import java.util.Scanner;
@@ -59,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()) {
@@ -90,12 +97,14 @@ public class FarmShell {
} catch (NoSuchElementException ex) {
// thrown by the "exit" command to signal exit
return false;
+ } catch (SQLException ex){
+ System.err.println("such " + ex);
}
// another satisfied customer, next!
return true;
}
- private void execute(Command command, String[] params) throws IOException {
+ private void execute(Command command, String[] params) throws IOException, SQLException {
if (params.length < command.getParamCount()) {
throw new IllegalArgumentException("Expected "
+ command.getParamCount() + " parameters, got only "