This repository acts as a personal archive for my solutions to EdX course *Data Structures and Software Design* from PennX.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

177 lines
5.3 KiB

  1. import java.util.List;
  2. import java.util.LinkedList;
  3. import java.util.Map;
  4. import java.util.HashMap;
  5. import java.util.Scanner;
  6. import java.util.Set;
  7. import java.util.LinkedHashSet;
  8. import java.nio.file.Paths;
  9. import java.nio.file.Files;
  10. import java.io.IOException;
  11. import java.lang.NumberFormatException;
  12. /*
  13. * SD2x Homework #3
  14. * Implement the methods below according to the specification in the assignment description.
  15. * Please be sure not to change the method signatures!
  16. */
  17. public class Analyzer {
  18. /*
  19. * Implement this method in Part 1
  20. */
  21. public static Sentence parseLine(String line){
  22. int negative = 1;
  23. if(line.length() > 0){
  24. if(line.charAt(0) == '-'){
  25. negative = -1;
  26. line = line.substring(1);
  27. }
  28. }
  29. if(line.length() < 3){
  30. return null;
  31. }
  32. int rating;
  33. try{
  34. rating = Integer.parseInt(line.substring(0,1));
  35. }catch(NumberFormatException e){
  36. return null;
  37. }
  38. if(-2 > rating || 2 < rating || line.charAt(1) != ' '){
  39. return null;
  40. }
  41. return new Sentence(rating * negative, line.substring(2));
  42. }
  43. public static List<Sentence> readFile(String filename) {
  44. List<Sentence> sentences = new LinkedList<Sentence>();
  45. if( filename == null ){ return sentences; }
  46. List<String> lines;
  47. try{
  48. lines = Files.readAllLines(Paths.get(filename));
  49. }catch(IOException e){
  50. return sentences;
  51. }
  52. for(String line : lines){
  53. Sentence sentence = parseLine(line);
  54. if(sentence == null){
  55. continue;
  56. }
  57. sentences.add(sentence);
  58. }
  59. return sentences;
  60. }
  61. public static Set<Word> allWords(List<Sentence> sentences) {
  62. Set<Word> words = new LinkedHashSet<Word>();
  63. if(sentences == null){ return words; }
  64. if(sentences.size() == 0){ return words; }
  65. for(Sentence s : sentences){
  66. if(s == null){
  67. continue;
  68. }
  69. String[] elements = s.getText().toLowerCase().split(" ");
  70. for(int i = 0; i < elements.length; i++){
  71. if(elements[i] == null){
  72. continue;
  73. }
  74. if(!Character.isLetter(elements[i].charAt(0))){
  75. continue;
  76. }
  77. Word word = new Word(elements[i]);
  78. for(int j = i + 1; j < elements.length; j++){
  79. if(elements[j] == null){
  80. continue;
  81. }
  82. if(elements[i].equals(elements[j])){
  83. word.count++;
  84. elements[j] = null; // Set matching elements to null, should increase performance slightly
  85. }
  86. }
  87. words.add(word);
  88. }
  89. }
  90. return words;
  91. }
  92. /*
  93. * Implement this method in Part 3
  94. */
  95. public static Map<String, Double> calculateScores(Set<Word> words) {
  96. Map<String, Double> scores = new HashMap<String, Double>();
  97. if(words == null){
  98. return scores;
  99. }
  100. if(words.isEmpty()){
  101. return scores;
  102. }
  103. for(Word w : words){
  104. if( w == null ){
  105. continue;
  106. }
  107. scores.put(w.getText(),w.calculateScore());
  108. }
  109. return scores;
  110. }
  111. /*
  112. * Implement this method in Part 4
  113. */
  114. public static double calculateSentenceScore(Map<String, Double> wordScores, String sentence) {
  115. double total = 0;
  116. int invalidWords = 0;
  117. if(wordScores == null || sentence == null){
  118. return total;
  119. }
  120. if(wordScores.isEmpty() || sentence.isEmpty()){
  121. return total;
  122. }
  123. String[] elements = sentence.toLowerCase().split(" ");
  124. for(int i = 0; i < elements.length; i++){
  125. if(!Character.isLetter(elements[i].charAt(0))){
  126. invalidWords++;
  127. continue;
  128. }
  129. if(wordScores.containsKey(elements[i])){
  130. total += wordScores.get(elements[i]);
  131. }
  132. }
  133. if(elements.length - invalidWords == 0){ // In case the whole sentence is invalid
  134. return 0;
  135. }
  136. return total / (elements.length - invalidWords); // this line is here only so this code will compile if you don't modify it
  137. }
  138. /*
  139. * This method is here to help you run your program. Y
  140. * You may modify it as needed.
  141. */
  142. public static void main(String[] args) {
  143. if (args.length == 0) {
  144. System.out.println("Please specify the name of the input file");
  145. System.exit(0);
  146. }
  147. String filename = args[0];
  148. System.out.print("Please enter a sentence: ");
  149. Scanner in = new Scanner(System.in);
  150. String sentence = in.nextLine();
  151. in.close();
  152. List<Sentence> sentences = Analyzer.readFile(filename);
  153. Set<Word> words = Analyzer.allWords(sentences);
  154. Map<String, Double> wordScores = Analyzer.calculateScores(words);
  155. double score = Analyzer.calculateSentenceScore(wordScores, sentence);
  156. System.out.println("The sentiment score is " + score);
  157. }
  158. }