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/09 03:35:22 UTC
svn commit: r1229008 - in
/lucene/dev/branches/lucene3305/modules/analysis/kuromoji/src:
java/org/apache/lucene/analysis/kuromoji/dict/
java/org/apache/lucene/analysis/kuromoji/viterbi/
resources/org/apache/lucene/analysis/kuromoji/dict/ tools/java/org...
Author: uschindler
Date: Mon Jan 9 02:35:22 2012
New Revision: 1229008
URL: http://svn.apache.org/viewvc?rev=1229008&view=rev
Log:
LUCENE-3305: refactor targetMap to only use 2 parallel arrays instead of 300,000 int[] in a large int[][]
Modified:
lucene/dev/branches/lucene3305/modules/analysis/kuromoji/src/java/org/apache/lucene/analysis/kuromoji/dict/BinaryDictionary.java
lucene/dev/branches/lucene3305/modules/analysis/kuromoji/src/java/org/apache/lucene/analysis/kuromoji/viterbi/Viterbi.java
lucene/dev/branches/lucene3305/modules/analysis/kuromoji/src/resources/org/apache/lucene/analysis/kuromoji/dict/TokenInfoDictionary$targetMap.dat
lucene/dev/branches/lucene3305/modules/analysis/kuromoji/src/resources/org/apache/lucene/analysis/kuromoji/dict/UnknownDictionary$targetMap.dat
lucene/dev/branches/lucene3305/modules/analysis/kuromoji/src/tools/java/org/apache/lucene/analysis/kuromoji/util/BinaryDictionaryWriter.java
Modified: lucene/dev/branches/lucene3305/modules/analysis/kuromoji/src/java/org/apache/lucene/analysis/kuromoji/dict/BinaryDictionary.java
URL: http://svn.apache.org/viewvc/lucene/dev/branches/lucene3305/modules/analysis/kuromoji/src/java/org/apache/lucene/analysis/kuromoji/dict/BinaryDictionary.java?rev=1229008&r1=1229007&r2=1229008&view=diff
==============================================================================
--- lucene/dev/branches/lucene3305/modules/analysis/kuromoji/src/java/org/apache/lucene/analysis/kuromoji/dict/BinaryDictionary.java (original)
+++ lucene/dev/branches/lucene3305/modules/analysis/kuromoji/src/java/org/apache/lucene/analysis/kuromoji/dict/BinaryDictionary.java Mon Jan 9 02:35:22 2012
@@ -29,6 +29,7 @@ import java.nio.channels.ReadableByteCha
import org.apache.lucene.store.DataInput;
import org.apache.lucene.store.InputStreamDataInput;
import org.apache.lucene.util.CodecUtil;
+import org.apache.lucene.util.IntsRef;
import org.apache.lucene.util.IOUtils;
public abstract class BinaryDictionary implements Dictionary {
@@ -43,13 +44,13 @@ public abstract class BinaryDictionary i
public static final int VERSION = 1;
private final ByteBuffer buffer;
- private final int[][] targetMap;
+ private final int[] targetMapOffsets, targetMap;
private final String[] posDict;
protected BinaryDictionary() throws IOException {
InputStream mapIS = null, dictIS = null, posIS = null;
IOException priorE = null;
- int[][] targetMap = null;
+ int[] targetMapOffsets = null, targetMap = null;
String[] posDict = null;
ByteBuffer buffer = null;
try {
@@ -59,24 +60,21 @@ public abstract class BinaryDictionary i
mapIS = new BufferedInputStream(mapIS);
DataInput in = new InputStreamDataInput(mapIS);
CodecUtil.checkHeader(in, TARGETMAP_HEADER, VERSION, VERSION);
- targetMap = new int[in.readVInt()][];
- int accum = 0;
- for (int j = 0; j < targetMap.length; j++) {
- final int len = in.readVInt();
- final int a[];
- if ((len & 1) == 1) {
- a = new int[1];
- accum += len >>> 1;
- a[0] = accum;
- } else {
- a = new int[len >>> 1];
- for (int i = 0; i < a.length; i++) {
- accum += in.readVInt();
- a[i] = accum;
- }
+ targetMap = new int[in.readVInt()];
+ targetMapOffsets = new int[in.readVInt()];
+ int accum = 0, sourceId = 0;
+ for (int ofs = 0; ofs < targetMap.length; ofs++) {
+ final int val = in.readVInt();
+ if ((val & 0x01) != 0) {
+ targetMapOffsets[sourceId] = ofs;
+ sourceId++;
}
- targetMap[j] = a;
+ accum += val >>> 1;
+ targetMap[ofs] = accum;
}
+ if (sourceId + 1 != targetMapOffsets.length)
+ throw new IOException("targetMap file format broken");
+ targetMapOffsets[sourceId] = targetMap.length;
posIS = getClass().getResourceAsStream(getClass().getSimpleName() + POSDICT_FILENAME_SUFFIX);
if (posIS == null)
@@ -109,12 +107,16 @@ public abstract class BinaryDictionary i
}
this.targetMap = targetMap;
+ this.targetMapOffsets = targetMapOffsets;
this.posDict = posDict;
this.buffer = buffer;
}
- public int[] lookupWordIds(int sourceId) {
- return targetMap[sourceId];
+ public void lookupWordIds(int sourceId, IntsRef ref) {
+ ref.ints = targetMap;
+ ref.offset = targetMapOffsets[sourceId];
+ // targetMapOffsets always has one more entry pointing behind last:
+ ref.length = targetMapOffsets[sourceId + 1] - ref.offset;
}
@Override
Modified: lucene/dev/branches/lucene3305/modules/analysis/kuromoji/src/java/org/apache/lucene/analysis/kuromoji/viterbi/Viterbi.java
URL: http://svn.apache.org/viewvc/lucene/dev/branches/lucene3305/modules/analysis/kuromoji/src/java/org/apache/lucene/analysis/kuromoji/viterbi/Viterbi.java?rev=1229008&r1=1229007&r2=1229008&view=diff
==============================================================================
--- lucene/dev/branches/lucene3305/modules/analysis/kuromoji/src/java/org/apache/lucene/analysis/kuromoji/viterbi/Viterbi.java (original)
+++ lucene/dev/branches/lucene3305/modules/analysis/kuromoji/src/java/org/apache/lucene/analysis/kuromoji/viterbi/Viterbi.java Mon Jan 9 02:35:22 2012
@@ -29,6 +29,7 @@ import org.apache.lucene.analysis.kuromo
import org.apache.lucene.analysis.kuromoji.dict.UnknownDictionary;
import org.apache.lucene.analysis.kuromoji.dict.UserDictionary;
import org.apache.lucene.analysis.kuromoji.viterbi.ViterbiNode.Type;
+import org.apache.lucene.util.IntsRef;
import org.apache.lucene.util.fst.FST;
public class Viterbi {
@@ -217,6 +218,8 @@ public class Viterbi {
int unknownWordEndIndex = -1; // index of the last character of unknown word
+ final IntsRef wordIdRef = new IntsRef();
+
for (int startIndex = 0; startIndex < length; startIndex++) {
// If no token ends where current token starts, skip this index
if (endSizeArr[startIndex + 1] == 0) {
@@ -240,8 +243,9 @@ public class Viterbi {
if (arc.isFinal()) {
output += arc.nextFinalOutput.intValue();
found = true; // Don't produce unknown word starting from this index
-
- for (int wordId : dictionary.lookupWordIds(output)) {
+ dictionary.lookupWordIds(output, wordIdRef);
+ for (int ofs = 0; ofs < wordIdRef.length; ofs++) {
+ final int wordId = wordIdRef.ints[wordIdRef.offset + ofs];
ViterbiNode node = new ViterbiNode(wordId, text, suffixStart, endIndex, dictionary.getLeftId(wordId), dictionary.getRightId(wordId), dictionary.getWordCost(wordId), startIndex, Type.KNOWN);
addToArrays(node, startIndex + 1, startIndex + 1 + endIndex, startIndexArr, endIndexArr, startSizeArr, endSizeArr);
}
@@ -264,10 +268,10 @@ public class Viterbi {
}
if (unknownWordLength > 0) { // found unknown word
- int characterId = characterDefinition.getCharacterClass(firstCharacter);
- int[] wordIds = unkDictionary.lookupWordIds(characterId); // characters in input text are supposed to be the same
-
- for (int wordId : wordIds) {
+ final int characterId = characterDefinition.getCharacterClass(firstCharacter);
+ unkDictionary.lookupWordIds(characterId, wordIdRef); // characters in input text are supposed to be the same
+ for (int ofs = 0; ofs < wordIdRef.length; ofs++) {
+ final int wordId = wordIdRef.ints[wordIdRef.offset + ofs];
ViterbiNode node = new ViterbiNode(wordId, text, suffixStart, unknownWordLength, unkDictionary.getLeftId(wordId), unkDictionary.getRightId(wordId), unkDictionary.getWordCost(wordId), startIndex, Type.UNKNOWN);
addToArrays(node, startIndex + 1, startIndex + 1 + unknownWordLength, startIndexArr, endIndexArr, startSizeArr, endSizeArr);
}
Modified: lucene/dev/branches/lucene3305/modules/analysis/kuromoji/src/resources/org/apache/lucene/analysis/kuromoji/dict/TokenInfoDictionary$targetMap.dat
URL: http://svn.apache.org/viewvc/lucene/dev/branches/lucene3305/modules/analysis/kuromoji/src/resources/org/apache/lucene/analysis/kuromoji/dict/TokenInfoDictionary%24targetMap.dat?rev=1229008&r1=1229007&r2=1229008&view=diff
==============================================================================
Binary files - no diff available.
Modified: lucene/dev/branches/lucene3305/modules/analysis/kuromoji/src/resources/org/apache/lucene/analysis/kuromoji/dict/UnknownDictionary$targetMap.dat
URL: http://svn.apache.org/viewvc/lucene/dev/branches/lucene3305/modules/analysis/kuromoji/src/resources/org/apache/lucene/analysis/kuromoji/dict/UnknownDictionary%24targetMap.dat?rev=1229008&r1=1229007&r2=1229008&view=diff
==============================================================================
Binary files - no diff available.
Modified: lucene/dev/branches/lucene3305/modules/analysis/kuromoji/src/tools/java/org/apache/lucene/analysis/kuromoji/util/BinaryDictionaryWriter.java
URL: http://svn.apache.org/viewvc/lucene/dev/branches/lucene3305/modules/analysis/kuromoji/src/tools/java/org/apache/lucene/analysis/kuromoji/util/BinaryDictionaryWriter.java?rev=1229008&r1=1229007&r2=1229008&view=diff
==============================================================================
--- lucene/dev/branches/lucene3305/modules/analysis/kuromoji/src/tools/java/org/apache/lucene/analysis/kuromoji/util/BinaryDictionaryWriter.java (original)
+++ lucene/dev/branches/lucene3305/modules/analysis/kuromoji/src/tools/java/org/apache/lucene/analysis/kuromoji/util/BinaryDictionaryWriter.java Mon Jan 9 02:35:22 2012
@@ -44,9 +44,9 @@ import org.apache.lucene.analysis.kuromo
public abstract class BinaryDictionaryWriter {
protected final Class<? extends BinaryDictionary> implClazz;
protected ByteBuffer buffer;
- private int targetMapSize = 0;
- private int[][] targetMap = new int[8192][];
- private int[] targetMapComponentSizes = new int[8192];
+ private int targetMapEndOffset = 0, lastWordId = -1, lastSourceId = -1;
+ private int[] targetMap = new int[8192];
+ private int[] targetMapOffsets = new int[8192];
private final List<String> posDict = new ArrayList<String>();
private final Map<String,Integer> posDictLookup = new HashMap<String,Integer>();
@@ -158,40 +158,25 @@ public abstract class BinaryDictionaryWr
}
}
- // only for assert
- int lastWordId = -1;
- int lastSourceId = 0;
-
public void addMapping(int sourceId, int wordId) {
- assert sourceId >= lastSourceId;
assert wordId > lastWordId : "words out of order: " + wordId + " vs lastID: " + lastWordId;
- lastSourceId = sourceId;
- lastWordId = wordId;
- if(targetMap.length <= sourceId) {
- final int newSize = ArrayUtil.oversize(sourceId + 1, RamUsageEstimator.NUM_BYTES_OBJECT_REF);
- int[][] newArray = new int[newSize][];
- System.arraycopy(targetMap, 0, newArray, 0, targetMap.length);
- targetMap = newArray;
- int[] newSizeArray = new int[newSize];
- System.arraycopy(targetMapComponentSizes, 0, newSizeArray, 0, targetMapComponentSizes.length);
- targetMapComponentSizes = newSizeArray;
- }
-
- // Prepare array -- extend the length of array
- int[] current = targetMap[sourceId];
- if (current == null) {
- assert targetMapComponentSizes[sourceId] == 0;
- current = new int[1];
+ if (sourceId > lastSourceId) {
+ assert sourceId > lastSourceId : "source ids out of order: lastSourceId=" + lastSourceId + " vs sourceId=" + sourceId;
+ targetMapOffsets = ArrayUtil.grow(targetMapOffsets, sourceId + 1);
+ for (int i = lastSourceId + 1; i <= sourceId; i++) {
+ targetMapOffsets[i] = targetMapEndOffset;
+ }
} else {
- current = ArrayUtil.grow(current);
+ assert sourceId == lastSourceId;
}
- targetMap[sourceId] = current;
-
- int[] targets = targetMap[sourceId];
- targets[targetMapComponentSizes[sourceId]] = wordId;
- targetMapComponentSizes[sourceId]++;
- targetMapSize = Math.max(targetMapSize, sourceId + 1);
+
+ targetMap = ArrayUtil.grow(targetMap, targetMapEndOffset + 1);
+ targetMap[targetMapEndOffset] = wordId;
+ targetMapEndOffset++;
+
+ lastSourceId = sourceId;
+ lastWordId = wordId;
}
protected final String getBaseFileName(String baseDir) throws IOException {
@@ -219,27 +204,23 @@ public abstract class BinaryDictionaryWr
os = new BufferedOutputStream(os);
final DataOutput out = new OutputStreamDataOutput(os);
CodecUtil.writeHeader(out, BinaryDictionary.TARGETMAP_HEADER, BinaryDictionary.VERSION);
- out.writeVInt(targetMapSize);
- int prev = 0;
- for (int j = 0; j < targetMapSize; j++) {
- final int size = targetMapComponentSizes[j];
- assert size > 0;
- if (size == 1) {
- int delta = targetMap[j][0] - prev;
- assert delta >= 0;
- out.writeVInt(delta << 1 | 1);
- prev += delta;
+
+ final int numSourceIds = lastSourceId + 1;
+ out.writeVInt(targetMapEndOffset); // <-- size of main array
+ out.writeVInt(numSourceIds + 1); // <-- size of offset array (+ 1 more entry)
+ int prev = 0, sourceId = 0;
+ for (int ofs = 0; ofs < targetMapEndOffset; ofs++) {
+ final int val = targetMap[ofs], delta = val - prev;
+ assert delta >= 0;
+ if (ofs == targetMapOffsets[sourceId]) {
+ out.writeVInt((delta << 1) | 0x01);
+ sourceId++;
} else {
- out.writeVInt(size << 1);
- final int[] a = targetMap[j];
- for (int i = 0; i < size; i++) {
- int delta = a[i] - prev;
- assert delta >= 0;
- out.writeVInt(delta);
- prev += delta;
- }
+ out.writeVInt((delta << 1));
}
+ prev += delta;
}
+ assert sourceId == numSourceIds : "sourceId:"+sourceId+" != numSourceIds:"+numSourceIds;
} finally {
os.close();
}