Advertisement
Guest User

code123

a guest
Nov 17th, 2019
139
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 4.99 KB | None | 0 0
  1. package cecs274;
  2.  
  3. import java.util.*;
  4. import java.util.Scanner;
  5. import java.io.File;
  6. import java.io.FileNotFoundException;
  7. import java.util.ArrayList;
  8. import java.lang.String;
  9.  
  10. /**
  11.  * This class finds the top 40 most used words said by a particular speaker in a
  12.  * political debate. This list of words depends on the speaker and the debate
  13.  * given.
  14.  *
  15.  * @author Matthew Chung matthew.chung01@student.csulb.edu
  16.  * @author Leo Rauthause leo.rauthause@student.csulb.edu
  17.  */
  18. public class DebateWordCloud {
  19.  
  20.    private String candidate;
  21.    private static ArrayList<String> stopWords = new ArrayList<>();
  22.    private static ArrayList<String> debateWords = new ArrayList<>();
  23.    private static ArrayList<WordFrequency> words = new ArrayList<>();
  24.    private static ArrayList<WordFrequency> topDebateWords = new ArrayList<>();
  25.    private static int maxFrequency;
  26.    private static int minFrequency;
  27.  
  28.    public DebateWordCloud(String candidate) {
  29.       this.candidate = candidate;
  30.    }
  31.  
  32.    public static void main(String args[]) throws FileNotFoundException {
  33.       System.out.println("Enter the following in the terminal as shown: (Debate File) (Candidate) (Stop Words File)");
  34.       try {
  35.          File stopWordsFile = new File(args[2]);
  36.          Scanner stopWordsScanner = new Scanner(stopWordsFile);
  37.          while (stopWordsScanner.hasNextLine()) {
  38.             String line = stopWordsScanner.nextLine();
  39.             stopWords.add(line.toLowerCase());
  40.          }
  41.          stopWordsScanner.close();
  42.  
  43.          File debateFile = new File(args[0]);
  44.          Scanner debateFileScanner1 = new Scanner(debateFile);
  45.          while (debateFileScanner1.hasNext()) {
  46.             String word = debateFileScanner1.next();
  47.             word.replaceAll("[^a-zA-Z0-9_-]", "");
  48.             debateWords.add(word);
  49.          }
  50.          debateFileScanner1.close();
  51.  
  52.          getCandidateWords(args[1]);
  53.          removeStopWords();
  54.          Collections.sort(words);
  55.          topWords();
  56.          System.out.print(topDebateWords);
  57.       } catch (FileNotFoundException fnfe) {
  58.          System.out.println("File not Found");
  59.       }
  60.    }
  61.  
  62.    /**
  63.     * Removes the stop words from a given stop words txt file from the words
  64.     * ArrayList Source:
  65.     * https://stackoverflow.com/questions/27685839/removing-stopwords-from-a-string-in-java
  66.     */
  67.    public static void removeStopWords() {
  68.       for (int x = 0; x < words.size(); x++) {
  69.          if (stopWords.contains(words.get(x).getWord().toLowerCase())) {
  70.             words.remove(x);
  71.             x--;
  72.          }
  73.       }
  74.  
  75.    }
  76.  
  77.    /**
  78.     * Retrieves the words from a specific candidate and adds them to the debate
  79.     * ArrayList Received help from CS Tutor
  80.     *
  81.     * @param speaker the candidate that's words we are retreiving
  82.     */
  83.    public static void getCandidateWords(String speaker) {
  84.       String candidate = speaker.toUpperCase() + ":";
  85.       Boolean isCandidateWords = false;
  86.       for (int x = 0; x < debateWords.size(); x++) {
  87.          String word = debateWords.get(x);
  88.          if (word.equals(candidate)) {
  89.             isCandidateWords = true;
  90.          } else if (!(word.equals(candidate)) && word.contains(":")) {
  91.             isCandidateWords = false;
  92.          }
  93.          getCandidateWordsExtended(word, candidate, isCandidateWords);
  94.          // word spoken by speaker
  95.  
  96.       }
  97.    }
  98.  
  99.    /**
  100.     * Gets words spoken by specific candidate and adds them to words ArrayList
  101.     * Received help from CS Tutor
  102.     *
  103.     * @param word             Word being checked if it was spoken by candidate
  104.     * @param candidate        candidate who's words are being assembled
  105.     * @param isCandidateWords is true if the words belong to the candidate. False
  106.     *                         otherwise.
  107.     */
  108.    public static void getCandidateWordsExtended(String word, String candidate, Boolean isCandidateWords) {
  109.       Boolean wordFound = false;
  110.       if (isCandidateWords && !(word.contains(":")) && !(word.equals(candidate))) {
  111.          for (int x = 0; x < words.size(); x++) {
  112.             if (words.get(x).getWord().equals(word.toLowerCase())) {
  113.                wordFound = true;
  114.             }
  115.          }
  116.          if (!wordFound) {
  117.             WordFrequency candidateWord = new WordFrequency(word.toLowerCase());
  118.             words.add(candidateWord);
  119.          } else {
  120.             for (int y = 0; y < words.size(); y++) {
  121.                if (words.get(y).getWord().equals(word.toLowerCase())) {
  122.                   words.get(y).incrementFrequency();
  123.                }
  124.             }
  125.          }
  126.       }
  127.    }
  128.  
  129.    /**
  130.     * Finds the top 40 words in the words ArrayList
  131.     */
  132.    public static void topWords() {
  133.       Collections.sort(words);
  134.       for (int x = words.size() - 1; x > words.size() - 41; x--) {
  135.          topDebateWords.add(words.get(x));
  136.       }
  137.       maxFrequency = topDebateWords.get(0).getFrequency();
  138.       minFrequency = topDebateWords.get(topDebateWords.size() - 1).getFrequency();
  139.       Collections.shuffle(topDebateWords);
  140.    }
  141. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement