summaryrefslogtreecommitdiff
path: root/src/main/Analyzor.java
blob: 1d0e8aaafabe523a78c630f6845e36bf40f3c6e3 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package main;

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;
import java.sql.SQLException;
import java.util.HashMap;
import java.util.Scanner;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 *
 * @author s123188
 */
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 {

        File uniFile = new File("unigrams-pmilexicon.txt"); // get uni
        File biFile = new File("bigrams-pmilexicon.txt"); // get bi

        Scanner uniScanner = new Scanner(uniFile);
        Scanner biScanner = new Scanner(biFile);

        //Fill the map of unigrams
        while (uniScanner.hasNext()) {
            unimap.put(uniScanner.next(), Double.parseDouble(uniScanner.next()));
            if (uniScanner.hasNextLine()) {
                uniScanner.nextLine();
            }
        }

        //fill the map of bigrams
        while (biScanner.hasNext()) {
            bimap.put(biScanner.next() + " " + biScanner.next(), Double.parseDouble(biScanner.next()));
            if (biScanner.hasNextLine()) {
                biScanner.nextLine();
            }
        }
    }

    //query the database
    //fills the ResultSet data
    void Query(String query) throws SQLException {

        PreparedStatement statement;
        //make a connection to the database and execute the query
        connection = Main.cb.create();
        statement = connection.prepareStatement(query);
        data = statement.executeQuery();
    }

    //analyzes the tweet on their positivity
    //this is just a base version
    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;

        //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);
                }
            }
            //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;
        }
        
        //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;
    }
}