You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@lucene.apache.org by rm...@apache.org on 2013/10/22 05:36:17 UTC

svn commit: r1534472 - in /lucene/dev/branches/lucene4956/lucene/analysis/arirang/src: java/org/apache/lucene/analysis/ko/dic/ resources/org/apache/lucene/analysis/ko/dic/ tools/java/org/apache/lucene/analysis/ko/dic/

Author: rmuir
Date: Tue Oct 22 03:36:17 2013
New Revision: 1534472

URL: http://svn.apache.org/r1534472
Log:
LUCENE-4956: do this simpler/faster like kuromoji

Modified:
    lucene/dev/branches/lucene4956/lucene/analysis/arirang/src/java/org/apache/lucene/analysis/ko/dic/HangulDictionary.java
    lucene/dev/branches/lucene4956/lucene/analysis/arirang/src/resources/org/apache/lucene/analysis/ko/dic/words.dat
    lucene/dev/branches/lucene4956/lucene/analysis/arirang/src/tools/java/org/apache/lucene/analysis/ko/dic/DictionaryBuilder.java

Modified: lucene/dev/branches/lucene4956/lucene/analysis/arirang/src/java/org/apache/lucene/analysis/ko/dic/HangulDictionary.java
URL: http://svn.apache.org/viewvc/lucene/dev/branches/lucene4956/lucene/analysis/arirang/src/java/org/apache/lucene/analysis/ko/dic/HangulDictionary.java?rev=1534472&r1=1534471&r2=1534472&view=diff
==============================================================================
--- lucene/dev/branches/lucene4956/lucene/analysis/arirang/src/java/org/apache/lucene/analysis/ko/dic/HangulDictionary.java (original)
+++ lucene/dev/branches/lucene4956/lucene/analysis/arirang/src/java/org/apache/lucene/analysis/ko/dic/HangulDictionary.java Tue Oct 22 03:36:17 2013
@@ -25,20 +25,51 @@ import org.apache.lucene.util.fst.FST;
 import org.apache.lucene.util.fst.FST.BytesReader;
 
 class HangulDictionary {
-  final FST<Byte> fst;
-  final byte[] metadata;
+  private final FST<Byte> fst;
+  private final byte[] metadata;
+  private final FST.Arc<Byte> rootCache[]; // ~140kb
   
   static final int RECORD_SIZE = 15;
   
-  static final int SBASE = 0xAC00;
-  static final int HANGUL_B0 = 0xE0 | (SBASE >> 12);
-  static final int VCOUNT = 21;
-  static final int TCOUNT = 28;
-  static final int NCOUNT = VCOUNT * TCOUNT;
-  
   public HangulDictionary(FST<Byte> fst, byte[] metadata) {
     this.fst = fst;
     this.metadata = metadata;
+    try {
+      rootCache = cacheRootArcs();
+    } catch (IOException bogus) {
+      throw new RuntimeException(bogus);
+    }
+  }
+  
+  @SuppressWarnings({"rawtypes","unchecked"})
+  private FST.Arc<Byte>[] cacheRootArcs() throws IOException {
+    FST.Arc<Byte> rootCache[] = new FST.Arc[1+(0xD7AF-0xAC00)];
+    FST.Arc<Byte> firstArc = new FST.Arc<Byte>();
+    fst.getFirstArc(firstArc);
+    FST.Arc<Byte> arc = new FST.Arc<Byte>();
+    final FST.BytesReader fstReader = fst.getBytesReader();
+    // TODO: jump to AC00, readNextRealArc to ceiling? (just be careful we don't add bugs)
+    for (int i = 0; i < rootCache.length; i++) {
+      if (fst.findTargetArc(0xAC00 + i, firstArc, arc, fstReader) != null) {
+        rootCache[i] = new FST.Arc<Byte>().copyFrom(arc);
+      }
+    }
+    return rootCache;
+  }
+  
+  private FST.Arc<Byte> findTargetArc(int ch, FST.Arc<Byte> follow, FST.Arc<Byte> arc, boolean useCache, FST.BytesReader fstReader) throws IOException {
+    if (useCache && ch >= 0xAC00 && ch <= 0xD7AF) {
+      assert ch != FST.END_LABEL;
+      final FST.Arc<Byte> result = rootCache[ch - 0xAC00];
+      if (result == null) {
+        return null;
+      } else {
+        arc.copyFrom(result);
+        return arc;
+      }
+    } else {
+      return fst.findTargetArc(ch, follow, arc, fstReader);
+    }
   }
   
   /** looks up word class for a word (exact match) */
@@ -52,49 +83,20 @@ class HangulDictionary {
     final BytesReader fstReader = fst.getBytesReader();
 
     // Accumulate output as we go
-    Byte output = fst.outputs.getNoOutput();
+    byte output = 0;
     for (int i = 0; i < key.length(); i++) {
       try {
-        char ch = key.charAt(i);
-        if (ch < 0xFF) {
-          // latin-1: remap to hangul syllable
-          if (fst.findTargetArc(HANGUL_B0, arc, arc, fstReader) == null) {
-            return null;
-          }
-          output = fst.outputs.add(output, arc.output);
-          if (fst.findTargetArc(0x80 | ((ch >> 6) & 0x3F), arc, arc, fstReader) == null) {
-            return null;
-          }
-          output = fst.outputs.add(output, arc.output);
-          if (fst.findTargetArc(0x80 | (ch & 0x3F), arc, arc, fstReader) == null) {
-            return null;
-          }
-          output = fst.outputs.add(output, arc.output);
-        } else if (ch >= SBASE && ch <= 0xD7AF) {
-          // hangul syllable: decompose to jamo and remap to latin-1
-          ch -= SBASE;
-          if (fst.findTargetArc(ch / NCOUNT, arc, arc, fstReader) == null) {
-            return null;
-          }
-          output = fst.outputs.add(output, arc.output);
-          if (fst.findTargetArc((ch % NCOUNT) / TCOUNT, arc, arc, fstReader) == null) {
-            return null;
-          }
-          output = fst.outputs.add(output, arc.output);
-          if (fst.findTargetArc(ch % TCOUNT, arc, arc, fstReader) == null) {
-            return null;
-          }
-          output = fst.outputs.add(output, arc.output);
-        } else {
+        if (findTargetArc(key.charAt(i), arc, arc, i == 0, fstReader) == null) {
           return null;
         }
       } catch (IOException bogus) {
         throw new RuntimeException();
       }
+      output += arc.output;
     }
 
     if (arc.isFinal()) {
-      return fst.outputs.add(output, arc.nextFinalOutput);
+      return (byte) (output + arc.nextFinalOutput);
     } else {
       return null;
     }
@@ -153,31 +155,7 @@ class HangulDictionary {
 
     for (int i = 0; i < key.length(); i++) {
       try {
-        char ch = key.charAt(i);
-        if (ch < 0xFF) {
-          // latin-1: remap to hangul syllable
-          if (fst.findTargetArc(HANGUL_B0, arc, arc, fstReader) == null) {
-            return false;
-          }
-          if (fst.findTargetArc(0x80 | ((ch >> 6) & 0x3F), arc, arc, fstReader) == null) {
-            return false;
-          }
-          if (fst.findTargetArc(0x80 | (ch & 0x3F), arc, arc, fstReader) == null) {
-            return false;
-          }
-        } else if (ch >= SBASE && ch <= 0xD7AF) {
-          // hangul syllable: decompose to jamo and remap to latin-1
-          ch -= SBASE;
-          if (fst.findTargetArc(ch / NCOUNT, arc, arc, fstReader) == null) {
-            return false;
-          }
-          if (fst.findTargetArc((ch % NCOUNT) / TCOUNT, arc, arc, fstReader) == null) {
-            return false;
-          }
-          if (fst.findTargetArc(ch % TCOUNT, arc, arc, fstReader) == null) {
-            return false;
-          }
-        } else {
+        if (findTargetArc(key.charAt(i), arc, arc, i == 0, fstReader) == null) {
           return false;
         }
       } catch (IOException bogus) {

Modified: lucene/dev/branches/lucene4956/lucene/analysis/arirang/src/resources/org/apache/lucene/analysis/ko/dic/words.dat
URL: http://svn.apache.org/viewvc/lucene/dev/branches/lucene4956/lucene/analysis/arirang/src/resources/org/apache/lucene/analysis/ko/dic/words.dat?rev=1534472&r1=1534471&r2=1534472&view=diff
==============================================================================
Binary files - no diff available.

Modified: lucene/dev/branches/lucene4956/lucene/analysis/arirang/src/tools/java/org/apache/lucene/analysis/ko/dic/DictionaryBuilder.java
URL: http://svn.apache.org/viewvc/lucene/dev/branches/lucene4956/lucene/analysis/arirang/src/tools/java/org/apache/lucene/analysis/ko/dic/DictionaryBuilder.java?rev=1534472&r1=1534471&r2=1534472&view=diff
==============================================================================
--- lucene/dev/branches/lucene4956/lucene/analysis/arirang/src/tools/java/org/apache/lucene/analysis/ko/dic/DictionaryBuilder.java (original)
+++ lucene/dev/branches/lucene4956/lucene/analysis/arirang/src/tools/java/org/apache/lucene/analysis/ko/dic/DictionaryBuilder.java Tue Oct 22 03:36:17 2013
@@ -28,7 +28,6 @@ import java.io.InputStream;
 import java.io.OutputStream;
 import java.util.ArrayList;
 import java.util.Collections;
-import java.util.Comparator;
 import java.util.LinkedHashMap;
 import java.util.List;
 import java.util.Map;
@@ -226,42 +225,12 @@ public class DictionaryBuilder {
   
   
   /** 
-   * makes FST (byte1) mapping to "word class"
-   * syllables are decomposed to jamo, then swapped with latin1
-   * (this makes the FST both smaller and much faster)
+   * makes FST (currently byte2 syllables) mapping to "word class"
    * each word has features + compound data, but many of them share the
    * same set of features, and have simple compound splits in the same place.
    */
   static void buildHangulDict(File inputDir, File outputDir) throws Exception {
-    TreeMap<String,Integer> sorted = new TreeMap<String,Integer>(new Comparator<String>() {
-
-      @Override
-      public int compare(String a, String b) {
-        final int stop = Math.min(a.length(), b.length());
-        int upto = 0;
-        while(upto < stop) {
-          int aChar = remap(a.charAt(upto));
-          int bChar = remap(b.charAt(upto));
-
-          int diff = aChar - bChar;
-          if (diff != 0) {
-            return diff;
-          }
-          upto++;
-        }
-        // One is a prefix of the other, or, they are equal:
-        return a.length() - b.length();
-      }
-      
-      int remap(char ch) {
-        if (ch < 0xff) {
-          return ch + HangulDictionary.SBASE;
-        } else {
-          assert ch >= HangulDictionary.SBASE && ch <= 0xD7AF : ch;
-          return ch - HangulDictionary.SBASE;
-        }
-      }
-    });
+    TreeMap<String,Integer> sorted = new TreeMap<String,Integer>();
     Map<Output,Integer> classes = new LinkedHashMap<>();
     File input = new File(inputDir, "dictionary.dic");
     BufferedReader reader = new BufferedReader(IOUtils.getDecodingReader(input, IOUtils.CHARSET_UTF_8));
@@ -292,27 +261,14 @@ public class DictionaryBuilder {
     System.out.println("#classes: " + classes.size());
     Outputs<Byte> fstOutput = ByteOutputs.getSingleton();
     // why does packed=false give a smaller fst?!?!
-    Builder<Byte> builder = new Builder<Byte>(FST.INPUT_TYPE.BYTE1, 0, 0, true, true, Integer.MAX_VALUE, fstOutput, null, false, PackedInts.DEFAULT, true, 15);
+    Builder<Byte> builder = new Builder<Byte>(FST.INPUT_TYPE.BYTE2, 0, 0, true, true, Integer.MAX_VALUE, fstOutput, null, false, PackedInts.DEFAULT, true, 15);
     IntsRef scratch = new IntsRef();
     for (Map.Entry<String,Integer> e : sorted.entrySet()) {
       String token = e.getKey();
-      scratch.grow(token.length() * 3);
-      scratch.length = token.length() * 3;
+      scratch.grow(token.length());
+      scratch.length = token.length();
       for (int i = 0; i < token.length(); i++) {
-        char ch = token.charAt(i);
-        if (ch < 0xFF) {
-          scratch.ints[3*i] = HangulDictionary.HANGUL_B0;
-          scratch.ints[3*i+1] = (0x80 | ((ch >> 6) & 0x3F));
-          scratch.ints[3*i+2] = (0x80 | (ch & 0x3F));
-        } else if (ch >= HangulDictionary.SBASE && ch <= 0xD7AF) {
-          // hangul syllable: decompose to jamo and remap to latin-1
-          ch -= HangulDictionary.SBASE;
-          scratch.ints[3*i] = (ch / HangulDictionary.NCOUNT);
-          scratch.ints[3*i+1] = ((ch % HangulDictionary.NCOUNT) / HangulDictionary.TCOUNT);
-          scratch.ints[3*i+2] = (ch % HangulDictionary.TCOUNT);
-        } else {
-          assert false : ch;
-        }
+        scratch.ints[i] = (int) token.charAt(i);
       }
       int v = e.getValue();
       assert v >= 0 && v < 128;