summaryrefslogtreecommitdiff
path: root/src/main/Analyzor.java
diff options
context:
space:
mode:
Diffstat (limited to 'src/main/Analyzor.java')
-rw-r--r--src/main/Analyzor.java41
1 files changed, 35 insertions, 6 deletions
diff --git a/src/main/Analyzor.java b/src/main/Analyzor.java
index c84f6cd..3eb93f9 100644
--- a/src/main/Analyzor.java
+++ b/src/main/Analyzor.java
@@ -84,7 +84,7 @@ public class Analyzor {
* @param query The query string to execute.
* @throws SQLException When database connection isn't available.
*/
- void Query(String query) throws SQLException {
+ public void query(String query) throws SQLException {
PreparedStatement statement;
//make a connection to the database and execute the query
@@ -99,7 +99,9 @@ public class Analyzor {
* @throws SQLException
* @throws IOException
*/
- public void sentimentAnalysis() throws SQLException, IOException {
+ public void sentimentAnalysis(String query) throws SQLException, IOException {
+ query(query);
+
//read the lexicons
readLexicon();
@@ -148,7 +150,9 @@ public class Analyzor {
}
//makes a wordcloud of the tweets in the ResultSet data
- private void makeWordCloud() throws SQLException {
+ void makeWordCloud(String query) throws SQLException {
+
+ query(query);
//go to the start of the ResultSet data
if (data == null) {
System.err.println("data is empty, try querying first");
@@ -157,17 +161,34 @@ public class Analyzor {
//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();
+
+ String text;
+ String[] words;
+ Integer value;
while(data.next()){
//get the text
+ text = data.getString("text");
+ //remove punctuation, convert to lowercase and split on words
+ text = removePunct(text);
+ text = text.toLowerCase();
+ words = text.split("\\s+");
+ //count the words
+ for(String word : words){
+ value = wordcloud.get(word);
+ if(value == null){
+ wordcloud.put(word, 1);
+ }
+ else{
+ wordcloud.put(word, value++);
+ }
+ }
}
}
//replaces punctuation so it will be splitted
+ //also removes urls
private String replacePunct(String text) {
text = text.replaceAll("https?://\\S*", "");
text = text.replaceAll("[!?):;\"']", " $0");
@@ -175,4 +196,12 @@ public class Analyzor {
text = text.replaceAll("\\s[(\"']", "$0 ");
return text;
}
+
+ //removes punctuation
+ //also removes urls
+ private String removePunct(String text){
+ text = text.replaceAll("https?://\\S*", "");
+ text = text.replaceAll("[.,!?()-:;\"']", " ");
+ return text;
+ }
}