Commit 5cd41a2a authored by ElaFHNW's avatar ElaFHNW
Browse files

Maps and SentiShakespeare

parent 49a0b7ed
package Collections;
// https://www.youtube.com/watch?v=H62Jfv1DJlU
public class Maps {
public static void main(String[] args) {
// TODO Auto-generated method stub
}
}
package Collections;
import java.io.File;
import java.io.FileNotFoundException;
import java.util.HashMap;
import java.util.Map;
import java.util.Scanner;
import java.util.Set;
import java.util.TreeSet;
public class SentiShakespeare {
static Set <String> adjectivesSenti;
static Set <String> adjectivesShakespeare;
static Map <String, Double> positiveAdj;
static Map <String, Double> negativeAdj;
SentiShakespeare() {
adjectivesSenti = new TreeSet<>();
positiveAdj = new HashMap<>();
negativeAdj = new HashMap<>();
getSentiAdjectives();
adjectivesShakespeare = new TreeSet<>();
getShakespeareAdjectives();
}
public void getShakespeareAdjectives() {
File senti = new File("shakespeare.txt");
try {
Scanner scan = new Scanner(senti);
while (scan.hasNext()) {
String line = scan.nextLine();
String [] words = line.split("\\s+");
// System.out.println("pos " + words[2] + " neg " +words[3] + " w " + words[4]);
for (String w : words) {
w = w.trim();
w = w.toLowerCase();
w = w.replace(",", "");
w = w.replace("?", "");
w = w.replace(".", "");
w = w.replace(";", "");
w = w.replace("]", "");
w = w.replace("^'", "");
w = w.replace(";", "");
w = w.replace("!", "");
if (w.length() > 0) {
adjectivesShakespeare.add(w); // set
}
}
}
} catch (FileNotFoundException e) {
e.printStackTrace();
}
}
public void getSentiAdjectives() {
File senti = new File("SentiWordNet_3.0.0.txt");
try {
Scanner scan = new Scanner(senti);
while (scan.hasNext()) {
String line = scan.nextLine();
if (line.startsWith("a")) {
String [] words = line.split("\\s+|#");
// System.out.println("pos " + words[2] + " neg " +words[3] + " w " + words[4]);
if (!words[4].contains("_")) {
adjectivesSenti.add(words[4]); //set
double posVal = Double.parseDouble(words[2]); // positive score
double negVal = Double.parseDouble(words[3]);
if (! (posVal == 0) ) {
positiveAdj.put(words[4], posVal); // fill in the map
}
if (! (negVal == 0) ) {
negativeAdj.put(words[4], negVal);
}
}
}
}
} catch (FileNotFoundException e) {
e.printStackTrace();
}
}
public static void main(String[] args) {
SentiShakespeare analysis1 = new SentiShakespeare();
// for (String word : adjectivesSenti) {
// System.out.print(word + "\t");
// }
System.out.println("SENTI " + adjectivesSenti.size() + " adjectives");
// for (String word : adjectivesShakespeare) {
// System.out.print(word + "\t");
// }
System.out.println("SHAKESPEARE " + adjectivesShakespeare.size() + " words");
Set <String> intersection = new TreeSet<>();
intersection.addAll(adjectivesSenti); // contains Sentinet
intersection.removeAll(adjectivesShakespeare);
System.out.println("shared adjectives " + intersection.size());
double shakespeareSentiment = 0;
for (String shared : intersection) {
if (positiveAdj.containsKey(shared)) {
shakespeareSentiment = shakespeareSentiment + positiveAdj.get(shared);
}
if (negativeAdj.containsKey(shared)) {
shakespeareSentiment = shakespeareSentiment - negativeAdj.get(shared);
}
}
System.out.println("Shakespeare sentiment index is " + shakespeareSentiment);
}
}
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment