From cfbb9801c48e0f50bdd0799f40a1a71de28b2873 Mon Sep 17 00:00:00 2001
From: Martin Goik <goik@hdm-stuttgart.de>
Date: Wed, 18 Jun 2014 18:05:38 +0200
Subject: [PATCH] Whitespace extension and single input filename

---
 .../mi/sd1/textstat/TextFileHandler.java        | 17 ++++++++---------
 .../mi/sd1/textstat/TextStatistics.java         | 16 +++-------------
 Sd1/P/WordFrequency1/Solution/testdata.txt      | 14 +++++++-------
 Sd1/swd1.xml                                    |  8 +++++++-
 4 files changed, 25 insertions(+), 30 deletions(-)

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 6ba0807fc..56c67cd79 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 19903f220..cd4c48b00 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 1fc141cef..2877d2fc8 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 f7f260da4..b75158eab 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>
-- 
GitLab