From 627b3dd7aa3597eb049f9996492198686dc138d2 Mon Sep 17 00:00:00 2001 From: s123188 Date: Thu, 15 May 2014 14:50:53 +0200 Subject: better exception handling (a.o. removed goToFirstRow), started with wordcloud --- src/main/Analyzor.java | 130 ++++++++++++++++++++++-------------------------- src/main/FarmShell.java | 5 +- 2 files changed, 63 insertions(+), 72 deletions(-) (limited to 'src') diff --git a/src/main/Analyzor.java b/src/main/Analyzor.java index 050d5c3..e893529 100644 --- a/src/main/Analyzor.java +++ b/src/main/Analyzor.java @@ -9,6 +9,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; @@ -27,13 +28,13 @@ public class Analyzor { //maps for the lexicons HashMap unimap = new HashMap(); // Map for uni HashMap bimap = new HashMap(); // Map for bi - + //the resultset of the query or the import ResultSet data; Connection connection; - + //reads the lexicons - void readLexicon() throws FileNotFoundException{ + void readLexicon() throws FileNotFoundException { File uniFile = new File("unigrams-pmilexicon.txt"); // get uni File biFile = new File("bigrams-pmilexicon.txt"); // get bi @@ -57,88 +58,75 @@ public class Analyzor { } } } - + //query the database - //fills the ResultSet - void Query(String query){ - + //fills the ResultSet data + void Query(String query) throws SQLException { + PreparedStatement statement; - - try { - connection = Main.cb.create(); - statement = connection.prepareStatement(query); - data = statement.executeQuery(); - - - } - catch(SQLException ex){ - System.err.println("could not make a connection with the database"+ex); - } + //make a connection to the database and execute the query + connection = Main.cb.create(); + statement = connection.prepareStatement(query); + data = statement.executeQuery(); } - //sets the pointer before the first element of the dataset - void goToFirstRow(){ - try{ - data.beforeFirst(); - } - catch(SQLException ex){ - System.err.print("something went wrong with the dataset: SQLException"); - return; - } - catch(NullPointerException ex){ - System.err.print("dataset is null, try querying first"); - return; - } - } //analyzes the tweet on their positivity //this is just a base version - void sentimentAnalysis() { + void sentimentAnalysis() throws SQLException, IOException { //read the lexicons - try{ - readLexicon(); - } - catch(FileNotFoundException ex){ - System.out.println("could not find the lexicons, please try again"); + readLexicon(); + + //go to the start of te dataset + if (data == null) { + System.err.println("data is empty, try querying first"); return; } - //go to the start of te dataset - goToFirstRow(); - + data.beforeFirst(); + 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){ - positiverate += unimap.get(word); - } + + //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) { + 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 + 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; } + + data.beforeFirst(); } } diff --git a/src/main/FarmShell.java b/src/main/FarmShell.java index daab973..55585c2 100644 --- a/src/main/FarmShell.java +++ b/src/main/FarmShell.java @@ -1,6 +1,7 @@ package main; import java.io.IOException; +import java.sql.SQLException; import java.util.Arrays; import java.util.NoSuchElementException; import java.util.Scanner; @@ -81,6 +82,8 @@ public class FarmShell { } catch (NoSuchElementException ex) { // thrown by the "exit" command to signal exit return false; + } catch (SQLException ex){ + System.err.println("such SQLException"); } // another satisfied customer, next! return true; @@ -131,7 +134,7 @@ public class FarmShell { "Available commands:" }; - private void execute(Command command, String[] params) throws IOException { + private void execute(Command command, String[] params) throws SQLException, IOException { if (params.length < command.getParamCount()) { throw new IllegalArgumentException("Expected " + command.getParamCount() + " parameters, got only " -- cgit v1.2.1