Advertisement
CheeseKeg

WordCount.java

Sep 29th, 2012
99
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.02 KB | None | 0 0
  1. package com.cheesekeg.wordcount;
  2.  
  3. import java.util.ArrayList;
  4.  
  5. import com.cheesekeg.wordcount.gui.WordCountWindow;
  6.  
  7. /**
  8.  * A Java Swing application that takes a given input from a TextArea and sorts space-separated words by popularity.
  9.  * @author Brandon DeRosier
  10.  * @since 0.1
  11.  * @version 0.1
  12.  */
  13. public class WordCount {
  14.    
  15.     WordCountWindow gui; // The jFrame I/O.
  16.    
  17.     /**
  18.      * Construct a new WordCount.
  19.      */
  20.     private WordCount() {
  21.         gui = new WordCountWindow();
  22.     }
  23.    
  24.     /**
  25.      * @param args
  26.      */
  27.     public static void main(String[] args) {
  28.         new WordCount();
  29.     }
  30.    
  31.     public static String CountWordString(String words) {
  32.         ArrayList<WordEntry> wordList = CountWords(words);
  33.         String result = "";
  34.        
  35.         for (int i = 0; i < wordList.size(); i++) {
  36.             result += wordList.get(i) + "\n";
  37.         }
  38.        
  39.         return result;
  40.     }
  41.    
  42.     public static ArrayList<WordEntry> CountWords(String words) {
  43.         ArrayList<WordEntry> wordList = new ArrayList<WordEntry>();
  44.        
  45.         String word = "";
  46.         char buffer;
  47.         for (int i = 0; i < words.length(); i++) {
  48.             buffer = words.charAt(i);
  49.            
  50.             if (buffer == ' ' || buffer == '.' || buffer == ',' || buffer == '\n') {
  51.                 if (word.length() > 0) {
  52.                     WordEntry test = new WordEntry("");
  53.                     for (int j = 0; j < wordList.size(); j++) {
  54.                        
  55.                         if (wordList.get(j).GetWord().toLowerCase().equals(word.toLowerCase())) {
  56.                             test = wordList.get(j);
  57.                             break;
  58.                         }
  59.                     }
  60.                    
  61.                     if (test.GetWord() == "") {
  62.                         test.SetWord(word);
  63.                         wordList.add(test);
  64.                     }
  65.                    
  66.                     test.IncrementCount();
  67.                    
  68.                     word = "";
  69.                 }
  70.             } else {
  71.                 word += buffer;
  72.             }
  73.         }
  74.        
  75.         // Simple descending sort.
  76.         ArrayList<WordEntry> result = new ArrayList<WordEntry>();
  77.        
  78.         while (wordList.size() > 0) {
  79.             WordEntry most = wordList.get(0);
  80.            
  81.             for (int j = 1; j < wordList.size(); j++) {
  82.                 if (wordList.get(j).GetCount() > most.GetCount()) {
  83.                     most = wordList.get(j);
  84.                 }
  85.             }
  86.            
  87.             wordList.remove(most);
  88.             result.add(most);
  89.         }
  90.        
  91.         return result;
  92.     }
  93.  
  94. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement