Advertisement
Guest User

Untitled

a guest
Feb 16th, 2019
255
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.48 KB | None | 0 0
  1.  
  2.  
  3.  
  4. public class WordUtil {
  5.     List<Word> words;
  6.  
  7.     /**
  8.      * Parse a sentence to obtain the unique words and the number of occurrences, and then pass each to the supplied
  9.      * WordCountConsumer.
  10.      *
  11.      * @param value A sentence.
  12.      * @return A list of Words with their count.
  13.      */
  14.     public void parse(String value, WordCountConsumer consumer) {
  15.         words = new ArrayList<>();
  16.         String[] values = value.split(" ");
  17.         for (int i = 0; i < values.length; i++) {
  18.             Word word = new Word(values[i], 1);
  19.             if (words.contains(word)) {
  20.                 Word tmpWord = words.get(words.indexOf(word));
  21.                 tmpWord.setCount(tmpWord.getCount() + 1);
  22.             } else {
  23.                 words.add(word);
  24.             }
  25.         }
  26.  
  27.         for (Word word : words) {
  28.             consumer.consume(word.getValue(), word.getCount());
  29.         }
  30.     }
  31.  
  32.     public static void main(String[] args) {
  33.         String test = "First half goals for Chelsea, Fulham and Leeds. Second half goals for Chelsea and Fulham.";
  34.         WordUtil wu = new WordUtil();
  35.         wu.parse(test, (word, count) -> System.out.println(word + " occurred " + count + " times"));
  36.     }
  37. }
  38.  
  39. public class Word {
  40.     String value;
  41.     int count;
  42.  
  43.     public Word(String value, int count) {
  44.         super();
  45.         this.value = value;
  46.         this.count = count;
  47.     }
  48.  
  49.     public String getValue() {
  50.         return value;
  51.     }
  52.  
  53.     public void setValue(String value) {
  54.         this.value = value;
  55.     }
  56.  
  57.     public int getCount() {
  58.         return count;
  59.     }
  60.  
  61.     @Override
  62.     public String toString() {
  63.         return "Word [value=" + value + ", count=" + count + "]";
  64.     }
  65.  
  66.     public void setCount(int count) {
  67.         this.count = count;
  68.     }
  69.  
  70.     @Override
  71.     public boolean equals(Object obj) {
  72.         if (this == obj) {
  73.             return true;
  74.         }
  75.         if (obj == null) {
  76.             return false;
  77.         }
  78.         if (getClass() != obj.getClass()) {
  79.             return false;
  80.         }
  81.         Word other = (Word) obj;
  82.         if (count != other.count) {
  83.             return false;
  84.         }
  85.         if (value == null) {
  86.             if (other.value != null) {
  87.                 return false;
  88.             }
  89.         } else if (!value.equals(other.value)) {
  90.             return false;
  91.         }
  92.         return true;
  93.     }
  94.  
  95. }
  96.  
  97. public interface WordCountConsumer {
  98.     void consume(String word, int occurences);
  99. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement