Revision: 6356
          
http://languagetool.svn.sourceforge.net/languagetool/?rev=6356&view=rev
Author:   dnaber
Date:     2012-01-26 17:03:57 +0000 (Thu, 26 Jan 2012)
Log Message:
-----------
some code simplification

Modified Paths:
--------------
    
trunk/JLanguageTool/src/java/org/languagetool/synthesis/ManualSynthesizer.java
    trunk/JLanguageTool/src/java/org/languagetool/tagging/ManualTagger.java
    
trunk/JLanguageTool/src/test/org/languagetool/synthesis/ManualSynthesizerTest.java

Modified: 
trunk/JLanguageTool/src/java/org/languagetool/synthesis/ManualSynthesizer.java
===================================================================
--- 
trunk/JLanguageTool/src/java/org/languagetool/synthesis/ManualSynthesizer.java  
    2012-01-25 21:20:41 UTC (rev 6355)
+++ 
trunk/JLanguageTool/src/java/org/languagetool/synthesis/ManualSynthesizer.java  
    2012-01-26 17:03:57 UTC (rev 6356)
@@ -18,21 +18,13 @@
  */
 package org.languagetool.synthesis;
 
-import java.io.BufferedReader;
-import java.io.IOException;
-import java.io.InputStream;
-import java.io.InputStreamReader;
-import java.util.ArrayList;
-import java.util.Collections;
-import java.util.HashMap;
-import java.util.HashSet;
-import java.util.List;
-import java.util.Map;
-import java.util.Set;
-
 import org.languagetool.tagging.ManualTagger;
 import org.languagetool.tools.StringTools;
 
+import java.io.IOException;
+import java.io.InputStream;
+import java.util.*;
+
 /**
  * A synthesizer that reads the inflected form and POS information from a 
plain (UTF-8) text file. <br/>
  * This makes it possible for the user to edit the text file to let the system 
know
@@ -50,8 +42,8 @@
   private final Map<String, List<String>> mapping;
   private Set<String> possibleTags = new HashSet<String>();
 
-  public ManualSynthesizer(final InputStream file) throws IOException {
-    mapping = loadMapping(file, "utf8");
+  public ManualSynthesizer(final InputStream inputStream) throws IOException {
+    mapping = loadMapping(inputStream, "utf8");
     possibleTags = Collections.unmodifiableSet(possibleTags); // lock
   }
 
@@ -59,7 +51,7 @@
    * Retrieve all the possible POS values.
    */
   public Set<String> getPossibleTags() {
-       return possibleTags;
+    return possibleTags;
   }
   
   /**
@@ -73,23 +65,18 @@
     return mapping.get(lemma + "|" + posTag);
   }
 
-  private Map<String, List<String>> loadMapping(final InputStream file,
-      final String encoding) throws IOException {
-    // TODO consider refactoring: this is almost the same as 
BaseSynthesizer#loadMappings()
+  private Map<String, List<String>> loadMapping(final InputStream inputStream, 
final String encoding) throws IOException {
     final Map<String, List<String>> map = new HashMap<String, List<String>>();
-    InputStreamReader isr = null;
-    BufferedReader br = null;
+    final Scanner scanner = new Scanner(inputStream, encoding);
     try {
-      isr = new InputStreamReader(file, encoding);
-      br = new BufferedReader(isr);
-      String line;
-      while ((line = br.readLine()) != null) {
-        if (StringTools.isEmpty(line) || line.charAt(0)=='#') {
+      while (scanner.hasNextLine()) {
+        final String line = scanner.nextLine();
+        if (StringTools.isEmpty(line) || line.charAt(0) == '#') {
           continue;
         }
         final String[] parts = line.split("\t");
         if (parts.length != 3) {
-          throw new IOException("Unknown format in " + file + ": " + line);
+          throw new IOException("Unknown line format when loading manual 
synthesizer dictionary: " + line);
         }
         final String key = parts[1] + "|" + parts[2];
         if (!map.containsKey(key)) {
@@ -99,12 +86,7 @@
         possibleTags.add(parts[2]); // POS 
       }
     } finally {
-      if (br != null) {
-        br.close();
-      }
-      if (isr != null) {
-        isr.close();
-      }
+      scanner.close();
     }
     return map;
   }

Modified: 
trunk/JLanguageTool/src/java/org/languagetool/tagging/ManualTagger.java
===================================================================
--- trunk/JLanguageTool/src/java/org/languagetool/tagging/ManualTagger.java     
2012-01-25 21:20:41 UTC (rev 6355)
+++ trunk/JLanguageTool/src/java/org/languagetool/tagging/ManualTagger.java     
2012-01-26 17:03:57 UTC (rev 6356)
@@ -18,38 +18,29 @@
  */
 package org.languagetool.tagging;
 
-import java.io.BufferedReader;
-import java.io.IOException;
-import java.io.InputStream;
-import java.io.InputStreamReader;
-import java.util.ArrayList;
-import java.util.HashMap;
-import java.util.List;
-import java.util.Map;
-
 import org.languagetool.synthesis.ManualSynthesizer;
 import org.languagetool.tools.StringTools;
 
+import java.io.IOException;
+import java.io.InputStream;
+import java.util.*;
 
-
 /**
  * A tagger that reads the POS information from a plain (UTF-8) text file. This
  * makes it possible for the user to edit the text file to let the system know
  * about new words or missing readings in the *.dict file.
  * 
- * <p>
- * File Format: <tt>fullform baseform postags</tt> (tab separated)
+ * <p>File Format: <tt>fullform baseform postags</tt> (tab separated)
  * 
  * @author Daniel Naber
- * 
  * @see ManualSynthesizer
  */
 public class ManualTagger {
 
   private final Map<String, List<LookedUpTerm>> mapping;
 
-  public ManualTagger(final InputStream file) throws IOException {
-    mapping = loadMapping(file, "utf8");
+  public ManualTagger(final InputStream inputStream) throws IOException {
+    mapping = loadMapping(inputStream, "utf8");
   }
 
   /**
@@ -77,41 +68,28 @@
     return plainResult.toArray(new String[]{});
   }
 
-  private Map<String, List<LookedUpTerm>> loadMapping(final InputStream file,
-      final String encoding) throws IOException {
-    // TODO consider refactoring: this is almost the same as 
ManualSynthesizer#loadMappings()
+  private Map<String, List<LookedUpTerm>> loadMapping(final InputStream 
inputStream, final String encoding) throws IOException {
     final Map<String, List<LookedUpTerm>> map = new HashMap<String, 
List<LookedUpTerm>>();
-    InputStreamReader isr = null;
-    BufferedReader br = null;
+    final Scanner scanner = new Scanner(inputStream, encoding);
     try {
-      isr = new InputStreamReader(file, encoding);
-      br = new BufferedReader(isr);
-      String line;
-      while ((line = br.readLine()) != null) {
-        if (StringTools.isEmpty(line) || line.charAt(0)=='#') {
+      while (scanner.hasNextLine()) {
+        final String line = scanner.nextLine();
+        if (StringTools.isEmpty(line) || line.charAt(0) == '#') {
           continue;
         }
         final String[] parts = line.split("\t");
         if (parts.length != 3) {
-          throw new IOException("Unknown format in " + file + ": " + line);
+          throw new IOException("Unknown line format when loading manual 
tagger dictionary: " + line);
         }
-        if (map.containsKey(parts[0])) {
-          final List<LookedUpTerm> l = map.get(parts[0]);
-          l.add(new LookedUpTerm(parts[1], parts[2]));
-          map.put(parts[0], l);
-        } else {
-          final List<LookedUpTerm> l = new ArrayList<LookedUpTerm>();
-          l.add(new LookedUpTerm(parts[1], parts[2]));
-          map.put(parts[0], l);
+        List<LookedUpTerm> terms = map.get(parts[0]);
+        if (terms == null) {
+          terms = new ArrayList<LookedUpTerm>();
         }
+        terms.add(new LookedUpTerm(parts[1], parts[2]));
+        map.put(parts[0], terms);
       }
     } finally {
-      if (br != null) {
-        br.close();
-      }
-      if (isr != null) {
-        isr.close();
-      }
+      scanner.close();
     }
     return map;
   }

Modified: 
trunk/JLanguageTool/src/test/org/languagetool/synthesis/ManualSynthesizerTest.java
===================================================================
--- 
trunk/JLanguageTool/src/test/org/languagetool/synthesis/ManualSynthesizerTest.java
  2012-01-25 21:20:41 UTC (rev 6355)
+++ 
trunk/JLanguageTool/src/test/org/languagetool/synthesis/ManualSynthesizerTest.java
  2012-01-26 17:03:57 UTC (rev 6356)
@@ -40,8 +40,7 @@
       "InflectedForm11\tLemma1\tPOS1\n" +
       "InflectedForm121\tLemma1\tPOS2\n" +
       "InflectedForm122\tLemma1\tPOS2\n" +
-      "InflectedForm2\tLemma2\tPOS1\n"
-      ;
+      "InflectedForm2\tLemma2\tPOS1\n";
     synthesizer = new ManualSynthesizer(new 
ByteArrayInputStream(data.getBytes("UTF-8")));
   }
 

This was sent by the SourceForge.net collaborative development platform, the 
world's largest Open Source development site.


------------------------------------------------------------------------------
Keep Your Developer Skills Current with LearnDevNow!
The most comprehensive online learning library for Microsoft developers
is just $99.99! Visual Studio, SharePoint, SQL - plus HTML5, CSS3, MVC3,
Metro Style Apps, more. Free future releases when you subscribe now!
http://p.sf.net/sfu/learndevnow-d2d
_______________________________________________
Languagetool-cvs mailing list
Languagetool-cvs@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/languagetool-cvs

Reply via email to