diff --git a/Sd1/P/WordFrequency1/Solution/src/main/java/de/hdm_stuttgart/mi/sd1/textstat/TextFileHandler.java b/Sd1/P/WordFrequency1/Solution/src/main/java/de/hdm_stuttgart/mi/sd1/textstat/TextFileHandler.java index 6ba0807fcf15e9c74d51479e75973af4c52e0355..56c67cd7934b7333aa16e6e628e8852f46f65fab 100644 --- a/Sd1/P/WordFrequency1/Solution/src/main/java/de/hdm_stuttgart/mi/sd1/textstat/TextFileHandler.java +++ b/Sd1/P/WordFrequency1/Solution/src/main/java/de/hdm_stuttgart/mi/sd1/textstat/TextFileHandler.java @@ -2,19 +2,19 @@ package de.hdm_stuttgart.mi.sd1.textstat; import java.io.BufferedReader; import java.io.FileNotFoundException; +import java.io.FileReader; import java.io.IOException; import java.util.ArrayList; import java.util.List; /** - * Count lines, words and characters for an individual - * file name. + * Count words and their respective frequencies of appearance. * */ public class TextFileHandler { /** - * The input file name or null if reading from {@link System#in}. + * The input file name to read from. */ public final String filename; @@ -24,20 +24,18 @@ public class TextFileHandler { final List<WordFrequency> wordFrequencies = new ArrayList<WordFrequency>(); /** - * @param reader Reader supplying content to be read and parsed. - * @param filename The filename input has been read from, empty string - * if reading from standard input, see {@link #filename} + * @param filename The filename input to read input, see {@link #filename} * * @throws IOException File fileName cannot be read * @throws FileNotFoundException File fileName does not exist */ - public TextFileHandler(final BufferedReader reader, final String filename) + public TextFileHandler(final String filename) throws FileNotFoundException, IOException { this.filename = filename; - + final BufferedReader reader = new BufferedReader(new FileReader(filename)); for (String line = reader.readLine(); line != null; line = reader.readLine()) { - final String[] words = line.split("[ \t]+"); + final String[] words = line.split("[ \t\"!.]+"); for (final String word: words) { final WordFrequency newWordCandidate = new WordFrequency(word); final int existingIndex = wordFrequencies.indexOf(newWordCandidate); @@ -48,6 +46,7 @@ public class TextFileHandler { } } } + reader.close(); } /** diff --git a/Sd1/P/WordFrequency1/Solution/src/main/java/de/hdm_stuttgart/mi/sd1/textstat/TextStatistics.java b/Sd1/P/WordFrequency1/Solution/src/main/java/de/hdm_stuttgart/mi/sd1/textstat/TextStatistics.java index 19903f220f7c48ae8c4f6e4867beca2605241d69..cd4c48b00182726c61dd0efbccaf7394dc70e909 100644 --- a/Sd1/P/WordFrequency1/Solution/src/main/java/de/hdm_stuttgart/mi/sd1/textstat/TextStatistics.java +++ b/Sd1/P/WordFrequency1/Solution/src/main/java/de/hdm_stuttgart/mi/sd1/textstat/TextStatistics.java @@ -1,9 +1,6 @@ package de.hdm_stuttgart.mi.sd1.textstat; -import java.io.BufferedReader; -import java.io.FileReader; import java.io.IOException; -import java.io.InputStreamReader; /** * Reading a text file and writing all content @@ -18,22 +15,15 @@ public class TextStatistics { TextFileHandler textFileHandler = null; - if (0 == fileNames.length) { // Try to read from standard input + if (1 == fileNames.length){ // Try to read from input file try { - textFileHandler = new TextFileHandler(new BufferedReader(new InputStreamReader(System.in)), ""); - } catch(IOException ex) { - System.err.println("unable to read from standard input"); - System.exit(1); - } - } else if (1 == fileNames.length){ // Try to read from input file - try { - textFileHandler = new TextFileHandler(new BufferedReader(new FileReader(fileNames[0])), fileNames[0]); + textFileHandler = new TextFileHandler(fileNames[0]); } catch (IOException e) { System.err.println("unable to read from file '" + fileNames[0] + "'"); System.exit(1); } } else { - System.err.println("Too many arguments"); + System.err.println("I need exactly one file name argument"); System.exit(1); } textFileHandler.printFrequencies(); diff --git a/Sd1/P/WordFrequency1/Solution/testdata.txt b/Sd1/P/WordFrequency1/Solution/testdata.txt index 1fc141cef458fc520ddab7df406166bd38a2e7e2..2877d2fc8f44cd1a8cd0c2861cebf55846ca5ec1 100644 --- a/Sd1/P/WordFrequency1/Solution/testdata.txt +++ b/Sd1/P/WordFrequency1/Solution/testdata.txt @@ -1,11 +1,11 @@ One day , Einstein , Newton , and Pascal meet up and decide to play a game of hide and seek. -Einstein volunteered to be " It . " As Einstein -counted , eyes closed , to 100 , Pascal ran away -and hid , but Newton stood right in front of +Einstein volunteered to be "It". As Einstein +counted, eyes closed, to 100, Pascal ran away +and hid, but Newton stood right in front of Einstein and drew a one meter by one meter -square on the floor around himself . When +square on the floor around himself. When Einstein opened his eyes, he immediately saw -Newton and said " I found you Newton , " but Newton -replied , " No , you found one Newton per square meter . -You found Pascal !" \ No newline at end of file +Newton and said "I found you Newton", but Newton +replied, "No, you found one Newton per square meter. +You found Pascal!" \ No newline at end of file diff --git a/Sd1/swd1.xml b/Sd1/swd1.xml index f7f260da4f17c129ece3b123c739046181f34ee4..b75158eabf2cf95758f00ecf9f6f47a271f7db78 100644 --- a/Sd1/swd1.xml +++ b/Sd1/swd1.xml @@ -7081,7 +7081,7 @@ final BufferedReader inputBufferedReader = new BufferedReader(fileReader); </section> </chapter> - <chapter xml:id="sd1Collection"> + <chapter xml:id="sd1CollectionI"> <title>Collections I (18.6.)</title> <section xml:id="sd1CollectionsPrep"> @@ -7484,5 +7484,11 @@ c12.equals("dummy"):false </section> </chapter> + <chapter xml:id="sd1CollectII"> + <title>Collections II (20.6.)</title> + + <para/> + </chapter> + <xi:include href="bibliography.xml" xpointer="element(/1)"/> </part>