You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@lucene.apache.org by us...@apache.org on 2012/01/07 16:38:38 UTC

svn commit: r1228636 - in /lucene/dev/branches/lucene3305/modules/analysis/kuromoji/src: java/org/apache/lucene/analysis/kuromoji/ java/org/apache/lucene/analysis/kuromoji/dict/ test/org/apache/lucene/analysis/kuromoji/dict/

Author: uschindler
Date: Sat Jan  7 15:38:38 2012
New Revision: 1228636

URL: http://svn.apache.org/viewvc?rev=1228636&view=rev
Log:
Remove nocommit in UserDictionary, move file reading to ctor. Dont close Readers passed to methods. This should be done by caller!

Modified:
    lucene/dev/branches/lucene3305/modules/analysis/kuromoji/src/java/org/apache/lucene/analysis/kuromoji/DebugTokenizer.java
    lucene/dev/branches/lucene3305/modules/analysis/kuromoji/src/java/org/apache/lucene/analysis/kuromoji/Tokenizer.java
    lucene/dev/branches/lucene3305/modules/analysis/kuromoji/src/java/org/apache/lucene/analysis/kuromoji/dict/UserDictionary.java
    lucene/dev/branches/lucene3305/modules/analysis/kuromoji/src/test/org/apache/lucene/analysis/kuromoji/dict/UserDictionaryTest.java

Modified: lucene/dev/branches/lucene3305/modules/analysis/kuromoji/src/java/org/apache/lucene/analysis/kuromoji/DebugTokenizer.java
URL: http://svn.apache.org/viewvc/lucene/dev/branches/lucene3305/modules/analysis/kuromoji/src/java/org/apache/lucene/analysis/kuromoji/DebugTokenizer.java?rev=1228636&r1=1228635&r2=1228636&view=diff
==============================================================================
--- lucene/dev/branches/lucene3305/modules/analysis/kuromoji/src/java/org/apache/lucene/analysis/kuromoji/DebugTokenizer.java (original)
+++ lucene/dev/branches/lucene3305/modules/analysis/kuromoji/src/java/org/apache/lucene/analysis/kuromoji/DebugTokenizer.java Sat Jan  7 15:38:38 2012
@@ -21,7 +21,7 @@ import java.io.BufferedInputStream;
 import java.io.FileInputStream;
 import java.io.FileNotFoundException;
 import java.io.IOException;
-import java.io.InputStream;
+import java.io.Reader;
 import java.util.List;
 
 import org.apache.lucene.analysis.kuromoji.Tokenizer.Mode;
@@ -58,6 +58,10 @@ public class DebugTokenizer {
     return new Builder();
   }
   
+  /**
+   * Builder class used to create DebugTokenizer instance.
+   * nocommit: why is everything synchronized here? Makes no sense for builders!
+   */
   public static class Builder {
     
     private Mode mode = Mode.NORMAL;
@@ -69,15 +73,26 @@ public class DebugTokenizer {
       return this;
     }
     
-    public synchronized Builder userDictionary(InputStream userDictionaryInputStream)
-        throws IOException {
-      this.userDictionary = UserDictionary.read(userDictionaryInputStream);
+    /**
+     * Set user dictionary input stream
+     * @param userDictionaryReader dictionary file as {@link Reader}
+     * @return Builder
+     * @throws IOException 
+     */
+    public synchronized Builder userDictionary(Reader userDictionaryReader) throws IOException {
+      this.userDictionary = new UserDictionary(userDictionaryReader);
       return this;
     }
     
-    public synchronized Builder userDictionary(String userDictionaryPath)
-        throws FileNotFoundException, IOException {
-      this.userDictionary(new BufferedInputStream(new FileInputStream(userDictionaryPath)));
+    /**
+     * Set user dictionary path
+     * @param userDictionaryPath path to dictionary file
+     * @return Builder
+     * @throws IOException 
+     * @throws FileNotFoundException 
+     */
+    public synchronized Builder userDictionary(String userDictionaryPath) throws FileNotFoundException, IOException {
+      this.userDictionary = new UserDictionary(userDictionaryPath);
       return this;
     }
     

Modified: lucene/dev/branches/lucene3305/modules/analysis/kuromoji/src/java/org/apache/lucene/analysis/kuromoji/Tokenizer.java
URL: http://svn.apache.org/viewvc/lucene/dev/branches/lucene3305/modules/analysis/kuromoji/src/java/org/apache/lucene/analysis/kuromoji/Tokenizer.java?rev=1228636&r1=1228635&r2=1228636&view=diff
==============================================================================
--- lucene/dev/branches/lucene3305/modules/analysis/kuromoji/src/java/org/apache/lucene/analysis/kuromoji/Tokenizer.java (original)
+++ lucene/dev/branches/lucene3305/modules/analysis/kuromoji/src/java/org/apache/lucene/analysis/kuromoji/Tokenizer.java Sat Jan  7 15:38:38 2012
@@ -21,7 +21,7 @@ import java.io.BufferedInputStream;
 import java.io.FileInputStream;
 import java.io.FileNotFoundException;
 import java.io.IOException;
-import java.io.InputStream;
+import java.io.Reader;
 import java.util.ArrayList;
 import java.util.EnumMap;
 import java.util.List;
@@ -167,6 +167,7 @@ public class Tokenizer {
   
   /**
    * Builder class used to create Tokenizer instance.
+   * nocommit: why is everything synchronized here? Makes no sense for builders!
    */
   public static class Builder {
     
@@ -204,12 +205,12 @@ public class Tokenizer {
     
     /**
      * Set user dictionary input stream
-     * @param userDictionaryInputStream dictionary file as input stream
+     * @param userDictionaryReader dictionary file as {@link Reader}
      * @return Builder
      * @throws IOException 
      */
-    public synchronized Builder userDictionary(InputStream userDictionaryInputStream) throws IOException {
-      this.userDictionary = UserDictionary.read(userDictionaryInputStream);
+    public synchronized Builder userDictionary(Reader userDictionaryReader) throws IOException {
+      this.userDictionary = new UserDictionary(userDictionaryReader);
       return this;
     }
     
@@ -221,9 +222,7 @@ public class Tokenizer {
      * @throws FileNotFoundException 
      */
     public synchronized Builder userDictionary(String userDictionaryPath) throws FileNotFoundException, IOException {
-      if (userDictionaryPath != null && ! userDictionaryPath.isEmpty()) {
-        this.userDictionary(new BufferedInputStream(new FileInputStream(userDictionaryPath)));
-      }
+      this.userDictionary = new UserDictionary(userDictionaryPath);
       return this;
     }
     

Modified: lucene/dev/branches/lucene3305/modules/analysis/kuromoji/src/java/org/apache/lucene/analysis/kuromoji/dict/UserDictionary.java
URL: http://svn.apache.org/viewvc/lucene/dev/branches/lucene3305/modules/analysis/kuromoji/src/java/org/apache/lucene/analysis/kuromoji/dict/UserDictionary.java?rev=1228636&r1=1228635&r2=1228636&view=diff
==============================================================================
--- lucene/dev/branches/lucene3305/modules/analysis/kuromoji/src/java/org/apache/lucene/analysis/kuromoji/dict/UserDictionary.java (original)
+++ lucene/dev/branches/lucene3305/modules/analysis/kuromoji/src/java/org/apache/lucene/analysis/kuromoji/dict/UserDictionary.java Sat Jan  7 15:38:38 2012
@@ -19,21 +19,23 @@ package org.apache.lucene.analysis.kurom
 
 import java.io.BufferedReader;
 import java.io.FileInputStream;
-import java.io.IOException;
 import java.io.InputStream;
+import java.io.IOException;
 import java.io.InputStreamReader;
+import java.io.Reader;
 import java.util.ArrayList;
 import java.util.HashMap;
 import java.util.Map;
 import java.util.TreeMap;
 
 import org.apache.lucene.analysis.kuromoji.util.CSVUtil;
+import org.apache.lucene.util.IOUtils;
 
-public class UserDictionary implements Dictionary {
+public final class UserDictionary implements Dictionary {
   
-  private TreeMap<String, int[]> entries = new TreeMap<String, int[]>();
+  private final TreeMap<String, int[]> entries = new TreeMap<String, int[]>();
   
-  private HashMap<Integer, String> featureEntries = new HashMap<Integer, String>();
+  private final HashMap<Integer, String> featureEntries = new HashMap<Integer, String>();
   
   private static final int CUSTOM_DICTIONARY_WORD_ID_OFFSET = 100000000;
   
@@ -43,7 +45,54 @@ public class UserDictionary implements D
   
   public static final int RIGHT_ID = 5;
   
-  private UserDictionary() {
+  public UserDictionary(String filename) throws IOException {
+    IOException priorE = null;
+    final InputStream is = new FileInputStream(filename);
+    try {
+      final Reader r = new InputStreamReader(is, "UTF-8");
+      read(r);
+    } catch (IOException ioe) {
+      priorE = ioe;
+    } finally {
+      IOUtils.closeWhileHandlingException(priorE, is);
+    }
+  }
+  
+  public UserDictionary(Reader reader) throws IOException {
+    read(reader);
+  }
+  
+  private void read(Reader reader) throws IOException {
+    BufferedReader br = new BufferedReader(reader);
+    String line = null;
+    int wordId = CUSTOM_DICTIONARY_WORD_ID_OFFSET;
+    while ((line = br.readLine()) != null) {
+      // Remove comments
+      line = line.replaceAll("#.*$", "");
+      
+      // Skip empty lines or comment lines
+      if (line.trim().length() == 0) {
+        continue;
+      }
+      String[] values = CSVUtil.parse(line);
+      String[] segmentation = values[1].replaceAll("  *", " ").split(" ");
+      String[] readings = values[2].replaceAll("  *", " ").split(" ");
+      String pos = values[3];
+      
+      if (segmentation.length != readings.length) {
+        // FIXME: Should probably deal with this differently.  Exception?
+        System.out.println("This entry is not properly formatted : " + line);
+      }
+      
+      int[] wordIdAndLength = new int[segmentation.length + 1]; // wordId offset, length, length....
+      wordIdAndLength[0] = wordId;
+      for (int i = 0; i < segmentation.length; i++) {
+        wordIdAndLength[i + 1] = segmentation[i].length();
+        featureEntries.put(wordId, readings[i] + INTERNAL_SEPARATOR + pos);
+        wordId++;
+      }
+      entries.put(values[0], wordIdAndLength);
+    }
   }
   
   /**
@@ -159,45 +208,4 @@ public class UserDictionary implements D
     return sb.deleteCharAt(sb.length() - 1).toString();
   }
   
-  public static UserDictionary read(String filename) throws IOException {
-    return read(new FileInputStream(filename));
-  }
-  
-  public static UserDictionary read(InputStream is) throws IOException {
-    UserDictionary dictionary = new UserDictionary();
-    // nocommit: require Readers not InputStreams (or InputStream/filename + charset)
-    BufferedReader reader = new BufferedReader(new InputStreamReader(is, "UTF-8"));
-    String line = null;
-    int wordId = CUSTOM_DICTIONARY_WORD_ID_OFFSET;
-    while ((line = reader.readLine()) != null) {
-      // Remove comments
-      line = line.replaceAll("#.*$", "");
-      
-      // Skip empty lines or comment lines
-      if (line.trim().length() == 0) {
-        continue;
-      }
-      String[] values = CSVUtil.parse(line);
-      String[] segmentation = values[1].replaceAll("  *", " ").split(" ");
-      String[] readings = values[2].replaceAll("  *", " ").split(" ");
-      String pos = values[3];
-      
-      if (segmentation.length != readings.length) {
-        // FIXME: Should probably deal with this differently.  Exception?
-        System.out.println("This entry is not properly formatted : " + line);
-      }
-      
-      int[] wordIdAndLength = new int[segmentation.length + 1]; // wordId offset, length, length....
-      wordIdAndLength[0] = wordId;
-      for (int i = 0; i < segmentation.length; i++) {
-        wordIdAndLength[i + 1] = segmentation[i].length();
-        dictionary.featureEntries.put(wordId, readings[i] + INTERNAL_SEPARATOR + pos);
-        wordId++;
-      }
-      dictionary.entries.put(values[0], wordIdAndLength);
-    }
-    reader.close();
-    return dictionary;
-  }
-  
 }

Modified: lucene/dev/branches/lucene3305/modules/analysis/kuromoji/src/test/org/apache/lucene/analysis/kuromoji/dict/UserDictionaryTest.java
URL: http://svn.apache.org/viewvc/lucene/dev/branches/lucene3305/modules/analysis/kuromoji/src/test/org/apache/lucene/analysis/kuromoji/dict/UserDictionaryTest.java?rev=1228636&r1=1228635&r2=1228636&view=diff
==============================================================================
--- lucene/dev/branches/lucene3305/modules/analysis/kuromoji/src/test/org/apache/lucene/analysis/kuromoji/dict/UserDictionaryTest.java (original)
+++ lucene/dev/branches/lucene3305/modules/analysis/kuromoji/src/test/org/apache/lucene/analysis/kuromoji/dict/UserDictionaryTest.java Sat Jan  7 15:38:38 2012
@@ -17,6 +17,10 @@ package org.apache.lucene.analysis.kurom
  * limitations under the License.
  */
 
+import java.io.FileNotFoundException;
+import java.io.InputStream;
+import java.io.InputStreamReader;
+import java.io.Reader;
 import java.io.IOException;
 
 import org.apache.lucene.analysis.kuromoji.TokenizerTest;
@@ -25,11 +29,22 @@ import org.apache.lucene.util.LuceneTest
 import org.junit.Test;
 
 public class UserDictionaryTest extends LuceneTestCase {
+
+  private UserDictionary readDict() throws IOException {
+    InputStream is = TokenizerTest.class.getResourceAsStream("userdict.txt");
+    if (is == null)
+      throw new FileNotFoundException("Cannot find userdict.txt in test classpath!");
+    try {
+      Reader reader = new InputStreamReader(is, "UTF-8");
+      return new UserDictionary(reader);
+    } finally {
+      is.close();
+    }
+  }
   
   @Test
   public void testLookup() throws IOException {
-    
-    UserDictionary dictionary = UserDictionary.read( TokenizerTest.class.getResourceAsStream("userdict.txt"));
+    UserDictionary dictionary = readDict();
     String s = "関西国際空港に行った";
     int[][] dictionaryEntryResult = dictionary.lookup(s.toCharArray(), 0, s.length());
     // Length should be three 関西, 国際, 空港
@@ -53,7 +68,7 @@ public class UserDictionaryTest extends 
   
   @Test
   public void testReadings() throws IOException {
-    UserDictionary dictionary = UserDictionary.read( TokenizerTest.class.getResourceAsStream("userdict.txt"));
+    UserDictionary dictionary = readDict();
     int wordIdNihon = 100000000; // wordId of 日本 in 日本経済新聞
     assertEquals("ニホン", dictionary.getReading(wordIdNihon));
     
@@ -66,14 +81,14 @@ public class UserDictionaryTest extends 
   
   @Test
   public void testPartOfSpeech() throws IOException {
-    UserDictionary dictionary = UserDictionary.read( TokenizerTest.class.getResourceAsStream("userdict.txt"));
+    UserDictionary dictionary = readDict();
     int wordIdKeizai = 100000001; // wordId of 経済 in 日本経済新聞
     assertEquals("カスタム名詞", dictionary.getPartOfSpeech(wordIdKeizai));
   }
   
   @Test
   public void testRead() throws IOException {
-    UserDictionary dictionary = UserDictionary.read( TokenizerTest.class.getResourceAsStream("userdict.txt"));
+    UserDictionary dictionary = readDict();
     assertNotNull(dictionary);		
   }
 }