From c27cf394d7fca31d020eca39e311896338338819 Mon Sep 17 00:00:00 2001 From: S129778 Date: Mon, 19 May 2014 14:25:44 +0200 Subject: double exception fixed --- src/main/Analyzor.java | 49 +++++++++++++++++++++++++++++++------------------ 1 file changed, 31 insertions(+), 18 deletions(-) diff --git a/src/main/Analyzor.java b/src/main/Analyzor.java index 678887f..b493a33 100644 --- a/src/main/Analyzor.java +++ b/src/main/Analyzor.java @@ -12,7 +12,9 @@ import java.sql.Connection; import java.sql.PreparedStatement; import java.sql.ResultSet; import java.sql.SQLException; +import java.util.ArrayList; import java.util.HashMap; +import java.util.List; import java.util.Scanner; /** @@ -35,14 +37,14 @@ public class Analyzor { * The results of a query, maybe return from query(). */ private ResultSet data; - + /** * The persistent connection to the database. */ private final Connection connection; /** - * @param connection An open connection to the database. + * @param connection An open connection to the database. */ public Analyzor(Connection connection) { this.connection = connection; @@ -50,8 +52,8 @@ public class Analyzor { /** * Read the unigram and bigram lexica. - * - * @throws FileNotFoundException + * + * @throws FileNotFoundException */ public void readLexicon() throws FileNotFoundException { if (!unimap.isEmpty()) { @@ -65,18 +67,23 @@ public class Analyzor { try (Scanner uniScanner = new Scanner(new File("unigrams-pmilexicon.txt")); Scanner biScanner = new Scanner(new File("bigrams-pmilexicon.txt"));) { //Fill the map of unigrams + int lineno = 1; while (uniScanner.hasNext()) { + String words = uniScanner.next(); - unimap.put(words.toLowerCase(), uniScanner.nextDouble()); + Double d = Double.valueOf(uniScanner.next()); + unimap.put(words.toLowerCase(), d); if (uniScanner.hasNextLine()) { uniScanner.nextLine(); } + lineno++; + } //fill the map of bigrams while (biScanner.hasNext()) { String words = biScanner.next() + " " + biScanner.next(); - bimap.put(words.toLowerCase(), biScanner.nextDouble()); + bimap.put(words.toLowerCase(), Double.valueOf(biScanner.next())); if (biScanner.hasNextLine()) { biScanner.nextLine(); } @@ -156,11 +163,11 @@ public class Analyzor { /** * Make a wordcloud of the results of some query. - * + * * @param query The sql text for a query. * @throws SQLException * @throws FileNotFoundException - * @throws UnsupportedEncodingException + * @throws UnsupportedEncodingException */ public void makeWordCloud(String query) throws SQLException, FileNotFoundException, UnsupportedEncodingException { @@ -175,7 +182,7 @@ public class Analyzor { String[] words; Integer value; String tweetid; - + PrintWriter writer = new PrintWriter("wordcloud.csv", "UTF-8"); //print the first row writer.println("tweetid, word"); @@ -189,15 +196,14 @@ public class Analyzor { words = text.split("\\s+"); //we use the tweetid as case id tweetid = Long.toString(data.getLong("tweetid")); - + for (String word : words) { writer.println(tweetid + ", " + word); } } //print it in a csv file to put in disco - + //print the first row - //print the values writer.close(); } @@ -221,7 +227,7 @@ public class Analyzor { writer.print(data.getObject(i).toString().replaceAll("[,\n]", " ") + ", "); } } - if(data.getObject(data.getMetaData().getColumnCount())==null){ + if (data.getObject(data.getMetaData().getColumnCount()) == null) { writer.println("0"); } else { writer.println(data.getObject(data.getMetaData().getColumnCount()).toString().replace(",", " ")); @@ -229,8 +235,8 @@ public class Analyzor { } writer.close(); } - - public void getBrands() throws SQLException{ + + public void getBrands() throws SQLException { PreparedStatement statement; //make a connection to the database and execute the query statement = connection.prepareStatement("delete from mentionsbrand"); @@ -238,11 +244,18 @@ public class Analyzor { BrandChecker checker = new BrandChecker("brandrules.txt"); query("select * from tweet"); NamedPreparedStatement m_insertBrand = new NamedPreparedStatement(connection, QueryUtils.insertBrand); - while(data.next()){ - for(String brand:checker.getBrands(data.getString("text"))){ - QueryUtils.setInsertBrandParams(m_insertBrand, data.getLong("tweetid"), brand); + while (data.next()) { + List brands = checker.getBrands(data.getString("text")); + if (brands.isEmpty()) { + QueryUtils.setInsertBrandParams(m_insertBrand, data.getLong("tweetid"), "no"); m_insertBrand.executeUpdate(); + } else { + for (String brand : brands) { + QueryUtils.setInsertBrandParams(m_insertBrand, data.getLong("tweetid"), brand); + m_insertBrand.executeUpdate(); + } } + } } -- cgit v1.2.1