-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathReview.java
169 lines (149 loc) · 4.44 KB
/
Review.java
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
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
import java.util.Scanner;
import java.io.File;
import java.util.HashMap;
import java.util.ArrayList;
import java.util.Random;
import java.io.*;
/**
* Class that contains helper methods for the Review Lab
**/
public class Review {
public static void main(String[] args){
System.out.print("that contains helper methods for the Review Lab");
}
private static HashMap<String, Double> sentiment = new HashMap<String, Double>();
private static ArrayList<String> posAdjectives = new ArrayList<String>();
private static ArrayList<String> negAdjectives = new ArrayList<String>();
private static final String SPACE = " ";
static{
try {
Scanner input = new Scanner(new File("cleanSentiment.csv"));
while(input.hasNextLine()){
String[] temp = input.nextLine().split(",");
sentiment.put(temp[0],Double.parseDouble(temp[1]));
//System.out.println("added "+ temp[0]+", "+temp[1]);
}
input.close();
}
catch(Exception e){
System.out.println("Error reading or parsing cleanSentiment.csv");
}
//read in the positive adjectives in postiveAdjectives.txt
try {
Scanner input = new Scanner(new File("positiveAdjectives.txt"));
while(input.hasNextLine()){
String temp = input.nextLine().trim();
System.out.println(temp);
posAdjectives.add(temp);
}
input.close();
}
catch(Exception e){
System.out.println("Error reading or parsing postitiveAdjectives.txt\n" + e);
}
//read in the negative adjectives in negativeAdjectives.txt
try {
Scanner input = new Scanner(new File("negativeAdjectives.txt"));
while(input.hasNextLine()){
negAdjectives.add(input.nextLine().trim());
}
input.close();
}
catch(Exception e){
System.out.println("Error reading or parsing negativeAdjectives.txt");
}
}
/**
* returns a string containing all of the text in fileName (including punctuation),
* with words separated by a single space
*/
public static String textToString( String fileName )
{
String temp = "";
try {
Scanner input = new Scanner(new File(fileName));
//add 'words' in the file to the string, separated by a single space
while(input.hasNext()){
temp = temp + input.next() + " ";
}
input.close();
}
catch(Exception e){
System.out.println("Unable to locate " + fileName);
}
//make sure to remove any additional space that may have been added at the end of the string.
return temp.trim();
}
/**
* @returns the sentiment value of word as a number between -1 (very negative) to 1 (very positive sentiment)
*/
public static double sentimentVal( String word )
{
try
{
return sentiment.get(word.toLowerCase());
}
catch(Exception e)
{
return 0;
}
}
/**
* Returns the ending punctuation of a string, or the empty string if there is none
*/
public static String getPunctuation( String word )
{
String punc = "";
for(int i=word.length()-1; i >= 0; i--){
if(!Character.isLetterOrDigit(word.charAt(i))){
punc = punc + word.charAt(i);
} else {
return punc;
}
}
return punc;
}
/**
* Returns the word after removing any beginning or ending punctuation
*/
public static String removePunctuation( String word )
{
while(word.length() > 0 && !Character.isAlphabetic(word.charAt(0)))
{
word = word.substring(1);
}
while(word.length() > 0 && !Character.isAlphabetic(word.charAt(word.length()-1)))
{
word = word.substring(0, word.length()-1);
}
return word;
}
/**
* Randomly picks a positive adjective from the positiveAdjectives.txt file and returns it.
*/
public static String randomPositiveAdj()
{
int index = (int)(Math.random() * posAdjectives.size());
return posAdjectives.get(index);
}
/**
* Randomly picks a negative adjective from the negativeAdjectives.txt file and returns it.
*/
public static String randomNegativeAdj()
{
int index = (int)(Math.random() * negAdjectives.size());
return negAdjectives.get(index);
}
/**
* Randomly picks a positive or negative adjective and returns it.
*/
public static String randomAdjective()
{
boolean positive = Math.random() < .5;
if(positive){
return randomPositiveAdj();
} else {
return randomNegativeAdj();
}
}
}