diff options
Diffstat (limited to 'src/de/fhswf/in/inf/java1/aufgabe09')
| -rw-r--r-- | src/de/fhswf/in/inf/java1/aufgabe09/WordCount.java | 133 | ||||
| -rw-r--r-- | src/de/fhswf/in/inf/java1/aufgabe09/WordCountTest.java | 43 |
2 files changed, 176 insertions, 0 deletions
diff --git a/src/de/fhswf/in/inf/java1/aufgabe09/WordCount.java b/src/de/fhswf/in/inf/java1/aufgabe09/WordCount.java new file mode 100644 index 0000000..ff806d1 --- /dev/null +++ b/src/de/fhswf/in/inf/java1/aufgabe09/WordCount.java @@ -0,0 +1,133 @@ +/** + * + */ +package de.fhswf.in.inf.java1.aufgabe09; + +import java.io.BufferedReader; +import java.io.File; +import java.io.FileReader; +import java.io.IOException; +import java.util.Collections; +import java.util.Map.Entry; +import java.util.Set; +import java.util.TreeMap; + +/** + * A class, that counts words in files. + * + * @author $Author: $ + * @version $Revision: $, $Date: $ UTC + */ +public class WordCount +{ + + private TreeMap<String, Integer> wordMap = new TreeMap<>(); + + /** + * Empty constructor. + * + */ + public WordCount() + { + + } + + /** + * Reads a file and counts the words. + * + * @param file + * The file to be read. + */ + public final void readFile(File file) + { + try (BufferedReader f = new BufferedReader(new FileReader(file))) + { + wordMap.clear(); + + String line = null; + + while ((line = f.readLine()) != null) + { + String[] lineArray = line + .split("[^\\p{IsAlphabetic}\\p{Digit}]+"); + for (String word : lineArray) + { + // Split creates empty String, if first char is a split char. + if (!word.isEmpty()) + { + word = word.toLowerCase(); + + Integer tmp = wordMap.get(word); + if (tmp != null) + { + wordMap.put(word, tmp + 1); + } + else + { + wordMap.put(word, new Integer(1)); + } + } + } + } + } + catch (IOException e) + { + e.printStackTrace(System.err); + } + } + + /* + * (non-Javadoc) + * + * @see java.lang.Object#toString() + */ + @Override + public final String toString() + { + StringBuilder ret = new StringBuilder(); + + for (Entry<String, Integer> mapping : wordMap.entrySet()) + { + ret.append(mapping.getKey() + " = " + mapping.getValue() + "\n"); + } + + return ret.toString(); + } + + /** + * Returns all words from the file. + * + * @return An unmodifiable Set of the words from the file. + */ + public final Set<String> getWords() + { + return Collections.unmodifiableSet(wordMap.keySet()); + } + + /** + * Returns the count of a word in a file. + * + * @param word + * The file to be counted. + * @return Returns the count of findings in the file. + */ + public final int getCount(String word) + { + if (word == null) + { + throw new IllegalArgumentException("Word must be a valid referece."); + } + if (word.isEmpty()) + { + throw new IllegalArgumentException("The word can't be empty."); + } + + Integer tmp = wordMap.get(word); + if (tmp == null) + { + return 0; + } + return tmp; + } + +} diff --git a/src/de/fhswf/in/inf/java1/aufgabe09/WordCountTest.java b/src/de/fhswf/in/inf/java1/aufgabe09/WordCountTest.java new file mode 100644 index 0000000..c9d2e4d --- /dev/null +++ b/src/de/fhswf/in/inf/java1/aufgabe09/WordCountTest.java @@ -0,0 +1,43 @@ +/** + * + */ +package de.fhswf.in.inf.java1.aufgabe09; + +import java.io.File; + +/** + * Tests the WordCount class. + * + * @author $Author: $ + * @version $Revision: $, $Date: $ UTC + */ +public final class WordCountTest +{ + + /** + * Private constructor for utility class. + * + */ + private WordCountTest() + { + } + + /** + * Tests the WordCount class. + * + * @param args + * Command line arguments. + */ + public static void main(String[] args) + { + File file = new File( + "C:/eclipse/workspace/java1/misc/Blatt9TestFile2.txt"); + + WordCount tmp = new WordCount(); + tmp.readFile(file); + System.out.println(tmp); + System.out.println("fgh = " + tmp.getCount("fgh")); + + } + +} |
