2011-06-29 09:01:06 +00:00
|
|
|
/*
|
2013-08-15 10:11:09 +00:00
|
|
|
* Copyright (C) 2013 The Android Open Source Project
|
2011-06-29 09:01:06 +00:00
|
|
|
*
|
2013-01-21 12:52:57 +00:00
|
|
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
* you may not use this file except in compliance with the License.
|
|
|
|
* You may obtain a copy of the License at
|
2011-06-29 09:01:06 +00:00
|
|
|
*
|
2013-01-21 12:52:57 +00:00
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
2011-06-29 09:01:06 +00:00
|
|
|
*
|
|
|
|
* Unless required by applicable law or agreed to in writing, software
|
2013-01-21 12:52:57 +00:00
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
* See the License for the specific language governing permissions and
|
|
|
|
* limitations under the License.
|
2011-06-29 09:01:06 +00:00
|
|
|
*/
|
|
|
|
|
2012-03-19 08:23:26 +00:00
|
|
|
package com.android.inputmethod.latin.makedict;
|
2011-06-29 09:01:06 +00:00
|
|
|
|
2013-08-19 05:49:57 +00:00
|
|
|
import com.android.inputmethod.latin.makedict.BinaryDictDecoderUtils.CharEncoding;
|
2012-09-12 09:53:33 +00:00
|
|
|
import com.android.inputmethod.latin.makedict.FormatSpec.FormatOptions;
|
2013-08-22 02:07:52 +00:00
|
|
|
import com.android.inputmethod.latin.makedict.FusionDictionary.PtNode;
|
2012-04-06 06:30:42 +00:00
|
|
|
import com.android.inputmethod.latin.makedict.FusionDictionary.DictionaryOptions;
|
2013-08-16 05:51:37 +00:00
|
|
|
import com.android.inputmethod.latin.makedict.FusionDictionary.PtNodeArray;
|
2012-03-19 08:23:26 +00:00
|
|
|
import com.android.inputmethod.latin.makedict.FusionDictionary.WeightedString;
|
2011-06-29 09:01:06 +00:00
|
|
|
|
2012-04-18 21:15:34 +00:00
|
|
|
import java.io.ByteArrayOutputStream;
|
2011-06-29 09:01:06 +00:00
|
|
|
import java.io.IOException;
|
|
|
|
import java.io.OutputStream;
|
|
|
|
import java.util.ArrayList;
|
2011-12-26 10:17:39 +00:00
|
|
|
import java.util.Iterator;
|
2011-06-29 09:01:06 +00:00
|
|
|
|
|
|
|
/**
|
2013-08-15 10:11:09 +00:00
|
|
|
* Encodes binary files for a FusionDictionary.
|
2011-06-29 09:01:06 +00:00
|
|
|
*
|
|
|
|
* All the methods in this class are static.
|
2013-08-22 02:07:52 +00:00
|
|
|
*
|
|
|
|
* TODO: Rename this class to DictEncoderUtils.
|
2011-06-29 09:01:06 +00:00
|
|
|
*/
|
2013-08-20 12:15:52 +00:00
|
|
|
public class BinaryDictEncoderUtils {
|
2011-06-29 09:01:06 +00:00
|
|
|
|
2012-09-03 01:21:03 +00:00
|
|
|
private static final boolean DBG = MakedictLog.DBG;
|
2012-05-11 14:03:01 +00:00
|
|
|
|
2013-08-20 12:15:52 +00:00
|
|
|
private BinaryDictEncoderUtils() {
|
2013-08-15 10:11:09 +00:00
|
|
|
// This utility class is not publicly instantiable.
|
2013-08-12 03:44:04 +00:00
|
|
|
}
|
|
|
|
|
Fix a bug where a node size would be seen as increasing.
The core reason for this is quite shrewd. When a word is a bigram
of itself, the corresponding chargroup will have a bigram referring
to itself. When computing bigram offsets, we use cached addresses of
chargroups, but we compute the size of the node as we go. Hence, a
discrepancy may happen between the base offset as seen by the bigram
(which uses the recomputed value) and the target offset (which uses
the cached value).
When this happens, the cached node address is too large. The relative
offset is negative, which is expected, since it points to this very
charnode whose start is a few bytes earlier. But since the cached
address is too large, the offset is computed as smaller than it should
be.
On the next pass, the cache has been refreshed with the newly computed
size and the seen offset is now correct (or at least, much closer to
correct). The correct value is larger than the previously computed
offset, which was too small. If it happens that it crosses the -255 or
-65335 boundary, the address will be seen as needing 1 more byte than
previously computed. If this is the only change in size of this node,
the node will be seen as having a larger size than previously, which
is unexpected. Debug code was catching this and crashing the program.
So this case is very rare, but in an even rarer occurence, it may
happen that in the same node, another chargroup happens to decrease
it size by the same amount. In this case, the node may be seen as
having not been modified. This is probably extremely rare. If on
top of this, it happens that no other node has been modified, then
the file may be seen as complete, and the discrepancy left as is
in the file, leading to a broken file. The probability that this
happens is abyssally low, but the bug exists, and the current debug
code would not have caught this.
To further catch similar bugs, this change also modifies the test
that decides if the node has changed. On grounds that all components
of a node may only decrease in size with each successive pass, it's
theoritically safe to assume that the same size means the node
contents have not changed, but in case of a bug like the bug above
where a component wrongly grows while another shrinks and both cancel
each other out, the new code will catch this. Also, this change adds
a check against the number of passses, to avoid infinite loops in
case of a bug in the computation code.
This change fixes this bug by updating the cached address of each
chargroup as we go. This eliminates the discrepancy and fixes the
bug.
Bug: 6383103
Change-Id: Ia3f450e22c87c4c193cea8ddb157aebd5f224f01
2012-04-24 03:13:22 +00:00
|
|
|
// Arbitrary limit to how much passes we consider address size compression should
|
|
|
|
// terminate in. At the time of this writing, our largest dictionary completes
|
|
|
|
// compression in five passes.
|
|
|
|
// If the number of passes exceeds this number, makedict bails with an exception on
|
|
|
|
// suspicion that a bug might be causing an infinite loop.
|
|
|
|
private static final int MAX_PASSES = 24;
|
2011-06-29 09:01:06 +00:00
|
|
|
|
2012-10-03 08:58:22 +00:00
|
|
|
/**
|
|
|
|
* Compute the binary size of the character array.
|
|
|
|
*
|
|
|
|
* If only one character, this is the size of this character. If many, it's the sum of their
|
|
|
|
* sizes + 1 byte for the terminator.
|
|
|
|
*
|
|
|
|
* @param characters the character array
|
|
|
|
* @return the size of the char array, including the terminator if any
|
|
|
|
*/
|
2013-08-22 02:07:52 +00:00
|
|
|
static int getPtNodeCharactersSize(final int[] characters) {
|
2012-10-03 08:58:22 +00:00
|
|
|
int size = CharEncoding.getCharArraySize(characters);
|
2013-08-22 02:07:52 +00:00
|
|
|
if (characters.length > 1) size += FormatSpec.PTNODE_TERMINATOR_SIZE;
|
2012-10-03 08:58:22 +00:00
|
|
|
return size;
|
|
|
|
}
|
|
|
|
|
2011-06-29 09:01:06 +00:00
|
|
|
/**
|
2013-08-22 02:07:52 +00:00
|
|
|
* Compute the binary size of the character array in a PtNode
|
2011-06-29 09:01:06 +00:00
|
|
|
*
|
|
|
|
* If only one character, this is the size of this character. If many, it's the sum of their
|
|
|
|
* sizes + 1 byte for the terminator.
|
|
|
|
*
|
2013-08-22 02:07:52 +00:00
|
|
|
* @param ptNode the PtNode
|
2011-06-29 09:01:06 +00:00
|
|
|
* @return the size of the char array, including the terminator if any
|
|
|
|
*/
|
2013-08-22 02:07:52 +00:00
|
|
|
private static int getPtNodeCharactersSize(final PtNode ptNode) {
|
|
|
|
return getPtNodeCharactersSize(ptNode.mChars);
|
2011-06-29 09:01:06 +00:00
|
|
|
}
|
|
|
|
|
2012-01-16 08:37:24 +00:00
|
|
|
/**
|
2013-08-22 02:07:52 +00:00
|
|
|
* Compute the binary size of the PtNode count for a node array.
|
2013-08-16 05:51:37 +00:00
|
|
|
* @param nodeArray the nodeArray
|
2013-08-22 02:07:52 +00:00
|
|
|
* @return the size of the PtNode count, either 1 or 2 bytes.
|
2012-01-16 08:37:24 +00:00
|
|
|
*/
|
2013-08-22 02:07:52 +00:00
|
|
|
private static int getPtNodeCountSize(final PtNodeArray nodeArray) {
|
|
|
|
return BinaryDictIOUtils.getPtNodeCountSize(nodeArray.mData.size());
|
2012-01-16 08:37:24 +00:00
|
|
|
}
|
|
|
|
|
2012-03-27 03:36:19 +00:00
|
|
|
/**
|
|
|
|
* Compute the size of a shortcut in bytes.
|
|
|
|
*/
|
|
|
|
private static int getShortcutSize(final WeightedString shortcut) {
|
2013-08-22 02:07:52 +00:00
|
|
|
int size = FormatSpec.PTNODE_ATTRIBUTE_FLAGS_SIZE;
|
2012-03-27 03:36:19 +00:00
|
|
|
final String word = shortcut.mWord;
|
|
|
|
final int length = word.length();
|
|
|
|
for (int i = 0; i < length; i = word.offsetByCodePoints(i, 1)) {
|
|
|
|
final int codePoint = word.codePointAt(i);
|
|
|
|
size += CharEncoding.getCharSize(codePoint);
|
|
|
|
}
|
2013-08-22 02:07:52 +00:00
|
|
|
size += FormatSpec.PTNODE_TERMINATOR_SIZE;
|
2012-03-27 03:36:19 +00:00
|
|
|
return size;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Compute the size of a shortcut list in bytes.
|
|
|
|
*
|
|
|
|
* This is known in advance and does not change according to position in the file
|
|
|
|
* like address lists do.
|
|
|
|
*/
|
2012-09-28 08:03:23 +00:00
|
|
|
static int getShortcutListSize(final ArrayList<WeightedString> shortcutList) {
|
2013-08-22 13:01:19 +00:00
|
|
|
if (null == shortcutList || shortcutList.isEmpty()) return 0;
|
2013-08-22 02:07:52 +00:00
|
|
|
int size = FormatSpec.PTNODE_SHORTCUT_LIST_SIZE_SIZE;
|
2012-03-27 03:36:19 +00:00
|
|
|
for (final WeightedString shortcut : shortcutList) {
|
|
|
|
size += getShortcutSize(shortcut);
|
|
|
|
}
|
|
|
|
return size;
|
|
|
|
}
|
|
|
|
|
2011-06-29 09:01:06 +00:00
|
|
|
/**
|
2013-08-22 02:07:52 +00:00
|
|
|
* Compute the maximum size of a PtNode, assuming 3-byte addresses for everything.
|
2011-06-29 09:01:06 +00:00
|
|
|
*
|
2013-08-22 02:07:52 +00:00
|
|
|
* @param ptNode the PtNode to compute the size of.
|
2012-09-03 01:21:03 +00:00
|
|
|
* @param options file format options.
|
2013-08-22 02:07:52 +00:00
|
|
|
* @return the maximum size of the PtNode.
|
2011-06-29 09:01:06 +00:00
|
|
|
*/
|
2013-08-22 02:07:52 +00:00
|
|
|
private static int getPtNodeMaximumSize(final PtNode ptNode, final FormatOptions options) {
|
|
|
|
int size = getNodeHeaderSize(ptNode, options);
|
2011-06-29 09:01:06 +00:00
|
|
|
// If terminal, one byte for the frequency
|
2013-08-22 02:07:52 +00:00
|
|
|
if (ptNode.isTerminal()) size += FormatSpec.PTNODE_FREQUENCY_SIZE;
|
|
|
|
size += FormatSpec.PTNODE_MAX_ADDRESS_SIZE; // For children address
|
|
|
|
size += getShortcutListSize(ptNode.mShortcutTargets);
|
|
|
|
if (null != ptNode.mBigrams) {
|
|
|
|
size += (FormatSpec.PTNODE_ATTRIBUTE_FLAGS_SIZE
|
|
|
|
+ FormatSpec.PTNODE_ATTRIBUTE_MAX_ADDRESS_SIZE)
|
|
|
|
* ptNode.mBigrams.size();
|
2011-06-29 09:01:06 +00:00
|
|
|
}
|
|
|
|
return size;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2013-08-22 02:07:52 +00:00
|
|
|
* Compute the maximum size of each PtNode of a PtNode array, assuming 3-byte addresses for
|
2013-08-16 05:51:37 +00:00
|
|
|
* everything, and caches it in the `mCachedSize' member of the nodes; deduce the size of
|
|
|
|
* the containing node array, and cache it it its 'mCachedSize' member.
|
2011-06-29 09:01:06 +00:00
|
|
|
*
|
2013-08-22 02:07:52 +00:00
|
|
|
* @param ptNodeArray the node array to compute the maximum size of.
|
2012-09-03 01:21:03 +00:00
|
|
|
* @param options file format options.
|
2011-06-29 09:01:06 +00:00
|
|
|
*/
|
2013-08-22 02:07:52 +00:00
|
|
|
private static void calculatePtNodeArrayMaximumSize(final PtNodeArray ptNodeArray,
|
2013-08-16 05:51:37 +00:00
|
|
|
final FormatOptions options) {
|
2013-08-22 02:07:52 +00:00
|
|
|
int size = getPtNodeCountSize(ptNodeArray);
|
|
|
|
for (PtNode node : ptNodeArray.mData) {
|
|
|
|
final int nodeSize = getPtNodeMaximumSize(node, options);
|
|
|
|
node.mCachedSize = nodeSize;
|
|
|
|
size += nodeSize;
|
2011-06-29 09:01:06 +00:00
|
|
|
}
|
2012-09-21 12:21:58 +00:00
|
|
|
if (options.mSupportsDynamicUpdate) {
|
2012-09-18 06:28:05 +00:00
|
|
|
size += FormatSpec.FORWARD_LINK_ADDRESS_SIZE;
|
|
|
|
}
|
2013-08-22 02:07:52 +00:00
|
|
|
ptNodeArray.mCachedSize = size;
|
2011-06-29 09:01:06 +00:00
|
|
|
}
|
|
|
|
|
2013-08-15 08:54:29 +00:00
|
|
|
/**
|
2013-08-22 02:07:52 +00:00
|
|
|
* Compute the size of the header (flag + [parent address] + characters size) of a PtNode.
|
2013-08-15 08:54:29 +00:00
|
|
|
*
|
2013-08-22 02:07:52 +00:00
|
|
|
* @param ptNode the PtNode of which to compute the size of the header
|
2013-08-15 08:54:29 +00:00
|
|
|
* @param options file format options.
|
|
|
|
*/
|
2013-08-22 02:07:52 +00:00
|
|
|
private static int getNodeHeaderSize(final PtNode ptNode, final FormatOptions options) {
|
2013-08-15 08:54:29 +00:00
|
|
|
if (BinaryDictIOUtils.supportsDynamicUpdate(options)) {
|
2013-08-22 02:07:52 +00:00
|
|
|
return FormatSpec.PTNODE_FLAGS_SIZE + FormatSpec.PARENT_ADDRESS_SIZE
|
|
|
|
+ getPtNodeCharactersSize(ptNode);
|
2013-08-15 08:54:29 +00:00
|
|
|
} else {
|
2013-08-22 02:07:52 +00:00
|
|
|
return FormatSpec.PTNODE_FLAGS_SIZE + getPtNodeCharactersSize(ptNode);
|
2013-08-15 08:54:29 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-06-29 09:01:06 +00:00
|
|
|
/**
|
|
|
|
* Compute the size, in bytes, that an address will occupy.
|
|
|
|
*
|
|
|
|
* This can be used either for children addresses (which are always positive) or for
|
|
|
|
* attribute, which may be positive or negative but
|
|
|
|
* store their sign bit separately.
|
|
|
|
*
|
|
|
|
* @param address the address
|
|
|
|
* @return the byte size.
|
|
|
|
*/
|
2012-09-28 08:03:23 +00:00
|
|
|
static int getByteSize(final int address) {
|
2013-08-14 09:39:14 +00:00
|
|
|
assert(address <= FormatSpec.UINT24_MAX);
|
2013-08-14 10:18:28 +00:00
|
|
|
if (!BinaryDictIOUtils.hasChildrenAddress(address)) {
|
2011-06-29 09:01:06 +00:00
|
|
|
return 0;
|
2013-08-14 09:39:14 +00:00
|
|
|
} else if (Math.abs(address) <= FormatSpec.UINT8_MAX) {
|
2011-06-29 09:01:06 +00:00
|
|
|
return 1;
|
2013-08-14 09:39:14 +00:00
|
|
|
} else if (Math.abs(address) <= FormatSpec.UINT16_MAX) {
|
2011-06-29 09:01:06 +00:00
|
|
|
return 2;
|
|
|
|
} else {
|
|
|
|
return 3;
|
|
|
|
}
|
|
|
|
}
|
2012-09-20 07:21:40 +00:00
|
|
|
|
2013-08-15 10:11:09 +00:00
|
|
|
// End utility methods
|
2011-06-29 09:01:06 +00:00
|
|
|
|
|
|
|
// This method is responsible for finding a nice ordering of the nodes that favors run-time
|
|
|
|
// cache performance and dictionary size.
|
2013-08-16 05:51:37 +00:00
|
|
|
/* package for tests */ static ArrayList<PtNodeArray> flattenTree(
|
|
|
|
final PtNodeArray rootNodeArray) {
|
2013-08-22 02:07:52 +00:00
|
|
|
final int treeSize = FusionDictionary.countPtNodes(rootNodeArray);
|
2011-06-29 09:01:06 +00:00
|
|
|
MakedictLog.i("Counted nodes : " + treeSize);
|
2013-08-16 05:51:37 +00:00
|
|
|
final ArrayList<PtNodeArray> flatTree = new ArrayList<PtNodeArray>(treeSize);
|
|
|
|
return flattenTreeInner(flatTree, rootNodeArray);
|
2011-06-29 09:01:06 +00:00
|
|
|
}
|
|
|
|
|
2013-08-16 05:51:37 +00:00
|
|
|
private static ArrayList<PtNodeArray> flattenTreeInner(final ArrayList<PtNodeArray> list,
|
2013-08-22 02:07:52 +00:00
|
|
|
final PtNodeArray ptNodeArray) {
|
2011-06-29 09:01:06 +00:00
|
|
|
// Removing the node is necessary if the tails are merged, because we would then
|
|
|
|
// add the same node several times when we only want it once. A number of places in
|
|
|
|
// the code also depends on any node being only once in the list.
|
|
|
|
// Merging tails can only be done if there are no attributes. Searching for attributes
|
|
|
|
// in LatinIME code depends on a total breadth-first ordering, which merging tails
|
|
|
|
// breaks. If there are no attributes, it should be fine (and reduce the file size)
|
2012-05-11 13:56:50 +00:00
|
|
|
// to merge tails, and removing the node from the list would be necessary. However,
|
|
|
|
// we don't merge tails because breaking the breadth-first ordering would result in
|
|
|
|
// extreme overhead at bigram lookup time (it would make the search function O(n) instead
|
|
|
|
// of the current O(log(n)), where n=number of nodes in the dictionary which is pretty
|
|
|
|
// high).
|
|
|
|
// If no nodes are ever merged, we can't have the same node twice in the list, hence
|
|
|
|
// searching for duplicates in unnecessary. It is also very performance consuming,
|
|
|
|
// since `list' is an ArrayList so it's an O(n) operation that runs on all nodes, making
|
|
|
|
// this simple list.remove operation O(n*n) overall. On Android this overhead is very
|
|
|
|
// high.
|
|
|
|
// For future reference, the code to remove duplicate is a simple : list.remove(node);
|
2013-08-22 02:07:52 +00:00
|
|
|
list.add(ptNodeArray);
|
|
|
|
final ArrayList<PtNode> branches = ptNodeArray.mData;
|
2011-06-29 09:01:06 +00:00
|
|
|
final int nodeSize = branches.size();
|
2013-08-22 02:07:52 +00:00
|
|
|
for (PtNode ptNode : branches) {
|
|
|
|
if (null != ptNode.mChildren) flattenTreeInner(list, ptNode.mChildren);
|
2011-06-29 09:01:06 +00:00
|
|
|
}
|
|
|
|
return list;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2013-08-16 05:51:37 +00:00
|
|
|
* Get the offset from a position inside a current node array to a target node array, during
|
|
|
|
* update.
|
|
|
|
*
|
|
|
|
* If the current node array is before the target node array, the target node array has not
|
|
|
|
* been updated yet, so we should return the offset from the old position of the current node
|
|
|
|
* array to the old position of the target node array. If on the other hand the target is
|
|
|
|
* before the current node array, it already has been updated, so we should return the offset
|
|
|
|
* from the new position in the current node array to the new position in the target node
|
|
|
|
* array.
|
|
|
|
*
|
2013-08-22 02:07:52 +00:00
|
|
|
* @param currentNodeArray node array containing the PtNode where the offset will be written
|
2013-08-16 05:51:37 +00:00
|
|
|
* @param offsetFromStartOfCurrentNodeArray offset, in bytes, from the start of currentNodeArray
|
|
|
|
* @param targetNodeArray the target node array to get the offset to
|
|
|
|
* @return the offset to the target node array
|
2011-06-29 09:01:06 +00:00
|
|
|
*/
|
2013-08-16 05:51:37 +00:00
|
|
|
private static int getOffsetToTargetNodeArrayDuringUpdate(final PtNodeArray currentNodeArray,
|
|
|
|
final int offsetFromStartOfCurrentNodeArray, final PtNodeArray targetNodeArray) {
|
|
|
|
final boolean isTargetBeforeCurrent = (targetNodeArray.mCachedAddressBeforeUpdate
|
|
|
|
< currentNodeArray.mCachedAddressBeforeUpdate);
|
2013-07-10 11:16:13 +00:00
|
|
|
if (isTargetBeforeCurrent) {
|
2013-08-16 05:51:37 +00:00
|
|
|
return targetNodeArray.mCachedAddressAfterUpdate
|
|
|
|
- (currentNodeArray.mCachedAddressAfterUpdate
|
|
|
|
+ offsetFromStartOfCurrentNodeArray);
|
2013-07-10 11:16:13 +00:00
|
|
|
} else {
|
2013-08-16 05:51:37 +00:00
|
|
|
return targetNodeArray.mCachedAddressBeforeUpdate
|
|
|
|
- (currentNodeArray.mCachedAddressBeforeUpdate
|
|
|
|
+ offsetFromStartOfCurrentNodeArray);
|
2013-07-10 11:16:13 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2013-08-22 02:07:52 +00:00
|
|
|
* Get the offset from a position inside a current node array to a target PtNode, during
|
2013-08-16 05:51:37 +00:00
|
|
|
* update.
|
|
|
|
*
|
2013-08-22 02:07:52 +00:00
|
|
|
* @param currentNodeArray node array containing the PtNode where the offset will be written
|
2013-08-16 05:51:37 +00:00
|
|
|
* @param offsetFromStartOfCurrentNodeArray offset, in bytes, from the start of currentNodeArray
|
2013-08-22 02:07:52 +00:00
|
|
|
* @param targetPtNode the target PtNode to get the offset to
|
|
|
|
* @return the offset to the target PtNode
|
2013-07-10 11:16:13 +00:00
|
|
|
*/
|
|
|
|
// TODO: is there any way to factorize this method with the one above?
|
2013-08-22 02:07:52 +00:00
|
|
|
private static int getOffsetToTargetPtNodeDuringUpdate(final PtNodeArray currentNodeArray,
|
|
|
|
final int offsetFromStartOfCurrentNodeArray, final PtNode targetPtNode) {
|
2013-08-16 05:51:37 +00:00
|
|
|
final int oldOffsetBasePoint = currentNodeArray.mCachedAddressBeforeUpdate
|
|
|
|
+ offsetFromStartOfCurrentNodeArray;
|
2013-08-22 02:07:52 +00:00
|
|
|
final boolean isTargetBeforeCurrent = (targetPtNode.mCachedAddressBeforeUpdate
|
2013-07-10 11:16:13 +00:00
|
|
|
< oldOffsetBasePoint);
|
2013-08-16 05:51:37 +00:00
|
|
|
// If the target is before the current node array, then its address has already been
|
|
|
|
// updated. We can use the AfterUpdate member, and compare it to our own member after
|
|
|
|
// update. Otherwise, the AfterUpdate member is not updated yet, so we need to use the
|
|
|
|
// BeforeUpdate member, and of course we have to compare this to our own address before
|
|
|
|
// update.
|
2013-07-10 11:16:13 +00:00
|
|
|
if (isTargetBeforeCurrent) {
|
2013-08-16 05:51:37 +00:00
|
|
|
final int newOffsetBasePoint = currentNodeArray.mCachedAddressAfterUpdate
|
|
|
|
+ offsetFromStartOfCurrentNodeArray;
|
2013-08-22 02:07:52 +00:00
|
|
|
return targetPtNode.mCachedAddressAfterUpdate - newOffsetBasePoint;
|
2013-07-10 11:16:13 +00:00
|
|
|
} else {
|
2013-08-22 02:07:52 +00:00
|
|
|
return targetPtNode.mCachedAddressBeforeUpdate - oldOffsetBasePoint;
|
2013-07-10 11:16:13 +00:00
|
|
|
}
|
2011-06-29 09:01:06 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2013-08-16 05:51:37 +00:00
|
|
|
* Computes the actual node array size, based on the cached addresses of the children nodes.
|
2011-06-29 09:01:06 +00:00
|
|
|
*
|
2013-08-16 05:51:37 +00:00
|
|
|
* Each node array stores its tentative address. During dictionary address computing, these
|
|
|
|
* are not final, but they can be used to compute the node array size (the node array size
|
|
|
|
* depends on the address of the children because the number of bytes necessary to store an
|
|
|
|
* address depends on its numeric value. The return value indicates whether the node array
|
Fix a bug where a node size would be seen as increasing.
The core reason for this is quite shrewd. When a word is a bigram
of itself, the corresponding chargroup will have a bigram referring
to itself. When computing bigram offsets, we use cached addresses of
chargroups, but we compute the size of the node as we go. Hence, a
discrepancy may happen between the base offset as seen by the bigram
(which uses the recomputed value) and the target offset (which uses
the cached value).
When this happens, the cached node address is too large. The relative
offset is negative, which is expected, since it points to this very
charnode whose start is a few bytes earlier. But since the cached
address is too large, the offset is computed as smaller than it should
be.
On the next pass, the cache has been refreshed with the newly computed
size and the seen offset is now correct (or at least, much closer to
correct). The correct value is larger than the previously computed
offset, which was too small. If it happens that it crosses the -255 or
-65335 boundary, the address will be seen as needing 1 more byte than
previously computed. If this is the only change in size of this node,
the node will be seen as having a larger size than previously, which
is unexpected. Debug code was catching this and crashing the program.
So this case is very rare, but in an even rarer occurence, it may
happen that in the same node, another chargroup happens to decrease
it size by the same amount. In this case, the node may be seen as
having not been modified. This is probably extremely rare. If on
top of this, it happens that no other node has been modified, then
the file may be seen as complete, and the discrepancy left as is
in the file, leading to a broken file. The probability that this
happens is abyssally low, but the bug exists, and the current debug
code would not have caught this.
To further catch similar bugs, this change also modifies the test
that decides if the node has changed. On grounds that all components
of a node may only decrease in size with each successive pass, it's
theoritically safe to assume that the same size means the node
contents have not changed, but in case of a bug like the bug above
where a component wrongly grows while another shrinks and both cancel
each other out, the new code will catch this. Also, this change adds
a check against the number of passses, to avoid infinite loops in
case of a bug in the computation code.
This change fixes this bug by updating the cached address of each
chargroup as we go. This eliminates the discrepancy and fixes the
bug.
Bug: 6383103
Change-Id: Ia3f450e22c87c4c193cea8ddb157aebd5f224f01
2012-04-24 03:13:22 +00:00
|
|
|
* contents (as in, any of the addresses stored in the cache fields) have changed with
|
|
|
|
* respect to their previous value.
|
2011-06-29 09:01:06 +00:00
|
|
|
*
|
2013-08-22 02:07:52 +00:00
|
|
|
* @param ptNodeArray the node array to compute the size of.
|
2011-06-29 09:01:06 +00:00
|
|
|
* @param dict the dictionary in which the word/attributes are to be found.
|
2012-09-03 01:21:03 +00:00
|
|
|
* @param formatOptions file format options.
|
2013-08-16 05:51:37 +00:00
|
|
|
* @return false if none of the cached addresses inside the node array changed, true otherwise.
|
2011-06-29 09:01:06 +00:00
|
|
|
*/
|
2013-08-22 02:07:52 +00:00
|
|
|
private static boolean computeActualPtNodeArraySize(final PtNodeArray ptNodeArray,
|
2013-08-16 05:51:37 +00:00
|
|
|
final FusionDictionary dict, final FormatOptions formatOptions) {
|
Fix a bug where a node size would be seen as increasing.
The core reason for this is quite shrewd. When a word is a bigram
of itself, the corresponding chargroup will have a bigram referring
to itself. When computing bigram offsets, we use cached addresses of
chargroups, but we compute the size of the node as we go. Hence, a
discrepancy may happen between the base offset as seen by the bigram
(which uses the recomputed value) and the target offset (which uses
the cached value).
When this happens, the cached node address is too large. The relative
offset is negative, which is expected, since it points to this very
charnode whose start is a few bytes earlier. But since the cached
address is too large, the offset is computed as smaller than it should
be.
On the next pass, the cache has been refreshed with the newly computed
size and the seen offset is now correct (or at least, much closer to
correct). The correct value is larger than the previously computed
offset, which was too small. If it happens that it crosses the -255 or
-65335 boundary, the address will be seen as needing 1 more byte than
previously computed. If this is the only change in size of this node,
the node will be seen as having a larger size than previously, which
is unexpected. Debug code was catching this and crashing the program.
So this case is very rare, but in an even rarer occurence, it may
happen that in the same node, another chargroup happens to decrease
it size by the same amount. In this case, the node may be seen as
having not been modified. This is probably extremely rare. If on
top of this, it happens that no other node has been modified, then
the file may be seen as complete, and the discrepancy left as is
in the file, leading to a broken file. The probability that this
happens is abyssally low, but the bug exists, and the current debug
code would not have caught this.
To further catch similar bugs, this change also modifies the test
that decides if the node has changed. On grounds that all components
of a node may only decrease in size with each successive pass, it's
theoritically safe to assume that the same size means the node
contents have not changed, but in case of a bug like the bug above
where a component wrongly grows while another shrinks and both cancel
each other out, the new code will catch this. Also, this change adds
a check against the number of passses, to avoid infinite loops in
case of a bug in the computation code.
This change fixes this bug by updating the cached address of each
chargroup as we go. This eliminates the discrepancy and fixes the
bug.
Bug: 6383103
Change-Id: Ia3f450e22c87c4c193cea8ddb157aebd5f224f01
2012-04-24 03:13:22 +00:00
|
|
|
boolean changed = false;
|
2013-08-22 02:07:52 +00:00
|
|
|
int size = getPtNodeCountSize(ptNodeArray);
|
|
|
|
for (PtNode ptNode : ptNodeArray.mData) {
|
|
|
|
ptNode.mCachedAddressAfterUpdate = ptNodeArray.mCachedAddressAfterUpdate + size;
|
|
|
|
if (ptNode.mCachedAddressAfterUpdate != ptNode.mCachedAddressBeforeUpdate) {
|
Fix a bug where a node size would be seen as increasing.
The core reason for this is quite shrewd. When a word is a bigram
of itself, the corresponding chargroup will have a bigram referring
to itself. When computing bigram offsets, we use cached addresses of
chargroups, but we compute the size of the node as we go. Hence, a
discrepancy may happen between the base offset as seen by the bigram
(which uses the recomputed value) and the target offset (which uses
the cached value).
When this happens, the cached node address is too large. The relative
offset is negative, which is expected, since it points to this very
charnode whose start is a few bytes earlier. But since the cached
address is too large, the offset is computed as smaller than it should
be.
On the next pass, the cache has been refreshed with the newly computed
size and the seen offset is now correct (or at least, much closer to
correct). The correct value is larger than the previously computed
offset, which was too small. If it happens that it crosses the -255 or
-65335 boundary, the address will be seen as needing 1 more byte than
previously computed. If this is the only change in size of this node,
the node will be seen as having a larger size than previously, which
is unexpected. Debug code was catching this and crashing the program.
So this case is very rare, but in an even rarer occurence, it may
happen that in the same node, another chargroup happens to decrease
it size by the same amount. In this case, the node may be seen as
having not been modified. This is probably extremely rare. If on
top of this, it happens that no other node has been modified, then
the file may be seen as complete, and the discrepancy left as is
in the file, leading to a broken file. The probability that this
happens is abyssally low, but the bug exists, and the current debug
code would not have caught this.
To further catch similar bugs, this change also modifies the test
that decides if the node has changed. On grounds that all components
of a node may only decrease in size with each successive pass, it's
theoritically safe to assume that the same size means the node
contents have not changed, but in case of a bug like the bug above
where a component wrongly grows while another shrinks and both cancel
each other out, the new code will catch this. Also, this change adds
a check against the number of passses, to avoid infinite loops in
case of a bug in the computation code.
This change fixes this bug by updating the cached address of each
chargroup as we go. This eliminates the discrepancy and fixes the
bug.
Bug: 6383103
Change-Id: Ia3f450e22c87c4c193cea8ddb157aebd5f224f01
2012-04-24 03:13:22 +00:00
|
|
|
changed = true;
|
|
|
|
}
|
2013-08-22 02:07:52 +00:00
|
|
|
int nodeSize = getNodeHeaderSize(ptNode, formatOptions);
|
|
|
|
if (ptNode.isTerminal()) nodeSize += FormatSpec.PTNODE_FREQUENCY_SIZE;
|
|
|
|
if (null == ptNode.mChildren && formatOptions.mSupportsDynamicUpdate) {
|
|
|
|
nodeSize += FormatSpec.SIGNED_CHILDREN_ADDRESS_SIZE;
|
|
|
|
} else if (null != ptNode.mChildren) {
|
2012-09-20 07:21:40 +00:00
|
|
|
if (formatOptions.mSupportsDynamicUpdate) {
|
2013-08-22 02:07:52 +00:00
|
|
|
nodeSize += FormatSpec.SIGNED_CHILDREN_ADDRESS_SIZE;
|
2012-09-20 07:21:40 +00:00
|
|
|
} else {
|
2013-08-22 02:07:52 +00:00
|
|
|
nodeSize += getByteSize(getOffsetToTargetNodeArrayDuringUpdate(ptNodeArray,
|
|
|
|
nodeSize + size, ptNode.mChildren));
|
2012-09-20 07:21:40 +00:00
|
|
|
}
|
2011-06-29 09:01:06 +00:00
|
|
|
}
|
2013-08-22 02:07:52 +00:00
|
|
|
nodeSize += getShortcutListSize(ptNode.mShortcutTargets);
|
|
|
|
if (null != ptNode.mBigrams) {
|
|
|
|
for (WeightedString bigram : ptNode.mBigrams) {
|
|
|
|
final int offset = getOffsetToTargetPtNodeDuringUpdate(ptNodeArray,
|
|
|
|
nodeSize + size + FormatSpec.PTNODE_FLAGS_SIZE,
|
2013-08-16 05:51:37 +00:00
|
|
|
FusionDictionary.findWordInTree(dict.mRootNodeArray, bigram.mWord));
|
2013-08-22 02:07:52 +00:00
|
|
|
nodeSize += getByteSize(offset) + FormatSpec.PTNODE_FLAGS_SIZE;
|
2011-06-29 09:01:06 +00:00
|
|
|
}
|
|
|
|
}
|
2013-08-22 02:07:52 +00:00
|
|
|
ptNode.mCachedSize = nodeSize;
|
|
|
|
size += nodeSize;
|
2011-06-29 09:01:06 +00:00
|
|
|
}
|
2012-09-21 12:21:58 +00:00
|
|
|
if (formatOptions.mSupportsDynamicUpdate) {
|
2012-09-18 06:28:05 +00:00
|
|
|
size += FormatSpec.FORWARD_LINK_ADDRESS_SIZE;
|
|
|
|
}
|
2013-08-22 02:07:52 +00:00
|
|
|
if (ptNodeArray.mCachedSize != size) {
|
|
|
|
ptNodeArray.mCachedSize = size;
|
Fix a bug where a node size would be seen as increasing.
The core reason for this is quite shrewd. When a word is a bigram
of itself, the corresponding chargroup will have a bigram referring
to itself. When computing bigram offsets, we use cached addresses of
chargroups, but we compute the size of the node as we go. Hence, a
discrepancy may happen between the base offset as seen by the bigram
(which uses the recomputed value) and the target offset (which uses
the cached value).
When this happens, the cached node address is too large. The relative
offset is negative, which is expected, since it points to this very
charnode whose start is a few bytes earlier. But since the cached
address is too large, the offset is computed as smaller than it should
be.
On the next pass, the cache has been refreshed with the newly computed
size and the seen offset is now correct (or at least, much closer to
correct). The correct value is larger than the previously computed
offset, which was too small. If it happens that it crosses the -255 or
-65335 boundary, the address will be seen as needing 1 more byte than
previously computed. If this is the only change in size of this node,
the node will be seen as having a larger size than previously, which
is unexpected. Debug code was catching this and crashing the program.
So this case is very rare, but in an even rarer occurence, it may
happen that in the same node, another chargroup happens to decrease
it size by the same amount. In this case, the node may be seen as
having not been modified. This is probably extremely rare. If on
top of this, it happens that no other node has been modified, then
the file may be seen as complete, and the discrepancy left as is
in the file, leading to a broken file. The probability that this
happens is abyssally low, but the bug exists, and the current debug
code would not have caught this.
To further catch similar bugs, this change also modifies the test
that decides if the node has changed. On grounds that all components
of a node may only decrease in size with each successive pass, it's
theoritically safe to assume that the same size means the node
contents have not changed, but in case of a bug like the bug above
where a component wrongly grows while another shrinks and both cancel
each other out, the new code will catch this. Also, this change adds
a check against the number of passses, to avoid infinite loops in
case of a bug in the computation code.
This change fixes this bug by updating the cached address of each
chargroup as we go. This eliminates the discrepancy and fixes the
bug.
Bug: 6383103
Change-Id: Ia3f450e22c87c4c193cea8ddb157aebd5f224f01
2012-04-24 03:13:22 +00:00
|
|
|
changed = true;
|
|
|
|
}
|
|
|
|
return changed;
|
2011-06-29 09:01:06 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2013-08-16 05:51:37 +00:00
|
|
|
* Initializes the cached addresses of node arrays and their containing nodes from their size.
|
2011-06-29 09:01:06 +00:00
|
|
|
*
|
2013-08-16 05:51:37 +00:00
|
|
|
* @param flatNodes the list of node arrays.
|
2012-09-18 06:28:05 +00:00
|
|
|
* @param formatOptions file format options.
|
2011-06-29 09:01:06 +00:00
|
|
|
* @return the byte size of the entire stack.
|
|
|
|
*/
|
2013-08-22 02:07:52 +00:00
|
|
|
private static int initializePtNodeArraysCachedAddresses(final ArrayList<PtNodeArray> flatNodes,
|
2013-07-11 04:48:27 +00:00
|
|
|
final FormatOptions formatOptions) {
|
2013-08-16 05:51:37 +00:00
|
|
|
int nodeArrayOffset = 0;
|
|
|
|
for (final PtNodeArray nodeArray : flatNodes) {
|
|
|
|
nodeArray.mCachedAddressBeforeUpdate = nodeArrayOffset;
|
2013-08-22 02:07:52 +00:00
|
|
|
int nodeCountSize = getPtNodeCountSize(nodeArray);
|
|
|
|
int nodeffset = 0;
|
|
|
|
for (final PtNode ptNode : nodeArray.mData) {
|
|
|
|
ptNode.mCachedAddressBeforeUpdate = ptNode.mCachedAddressAfterUpdate =
|
|
|
|
nodeCountSize + nodeArrayOffset + nodeffset;
|
|
|
|
nodeffset += ptNode.mCachedSize;
|
2013-07-11 04:48:27 +00:00
|
|
|
}
|
2013-08-22 02:07:52 +00:00
|
|
|
final int nodeSize = nodeCountSize + nodeffset
|
2013-07-11 04:48:27 +00:00
|
|
|
+ (formatOptions.mSupportsDynamicUpdate
|
|
|
|
? FormatSpec.FORWARD_LINK_ADDRESS_SIZE : 0);
|
2013-08-16 05:51:37 +00:00
|
|
|
nodeArrayOffset += nodeArray.mCachedSize;
|
2013-07-11 04:48:27 +00:00
|
|
|
}
|
2013-08-16 05:51:37 +00:00
|
|
|
return nodeArrayOffset;
|
2013-07-11 04:48:27 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2013-08-16 05:51:37 +00:00
|
|
|
* Updates the cached addresses of node arrays after recomputing their new positions.
|
2013-07-11 04:48:27 +00:00
|
|
|
*
|
2013-08-16 05:51:37 +00:00
|
|
|
* @param flatNodes the list of node arrays.
|
2013-07-11 04:48:27 +00:00
|
|
|
*/
|
2013-08-22 02:07:52 +00:00
|
|
|
private static void updatePtNodeArraysCachedAddresses(final ArrayList<PtNodeArray> flatNodes) {
|
2013-08-16 05:51:37 +00:00
|
|
|
for (final PtNodeArray nodeArray : flatNodes) {
|
|
|
|
nodeArray.mCachedAddressBeforeUpdate = nodeArray.mCachedAddressAfterUpdate;
|
2013-08-22 02:07:52 +00:00
|
|
|
for (final PtNode ptNode : nodeArray.mData) {
|
|
|
|
ptNode.mCachedAddressBeforeUpdate = ptNode.mCachedAddressAfterUpdate;
|
2011-06-29 09:01:06 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-07-10 09:28:34 +00:00
|
|
|
/**
|
|
|
|
* Compute the cached parent addresses after all has been updated.
|
|
|
|
*
|
|
|
|
* The parent addresses are used by some binary formats at write-to-disk time. Not all formats
|
|
|
|
* need them. In particular, version 2 does not need them, and version 3 does.
|
|
|
|
*
|
2013-08-16 05:51:37 +00:00
|
|
|
* @param flatNodes the flat array of node arrays to fill in
|
2013-07-10 09:28:34 +00:00
|
|
|
*/
|
2013-08-16 05:51:37 +00:00
|
|
|
private static void computeParentAddresses(final ArrayList<PtNodeArray> flatNodes) {
|
|
|
|
for (final PtNodeArray nodeArray : flatNodes) {
|
2013-08-22 02:07:52 +00:00
|
|
|
for (final PtNode ptNode : nodeArray.mData) {
|
|
|
|
if (null != ptNode.mChildren) {
|
2013-07-10 10:23:03 +00:00
|
|
|
// Assign my address to children's parent address
|
|
|
|
// Here BeforeUpdate and AfterUpdate addresses have the same value, so it
|
|
|
|
// does not matter which we use.
|
2013-08-22 02:07:52 +00:00
|
|
|
ptNode.mChildren.mCachedParentAddress = ptNode.mCachedAddressAfterUpdate
|
|
|
|
- ptNode.mChildren.mCachedAddressAfterUpdate;
|
2013-07-10 09:28:34 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-06-29 09:01:06 +00:00
|
|
|
/**
|
2013-08-22 02:07:52 +00:00
|
|
|
* Compute the addresses and sizes of an ordered list of PtNode arrays.
|
2011-06-29 09:01:06 +00:00
|
|
|
*
|
2013-08-22 02:07:52 +00:00
|
|
|
* This method takes a list of PtNode arrays and will update their cached address and size
|
2013-08-16 05:51:37 +00:00
|
|
|
* values so that they can be written into a file. It determines the smallest size each of the
|
2013-08-22 02:07:52 +00:00
|
|
|
* PtNode arrays can be given the addresses of its children and attributes, and store that into
|
|
|
|
* each PtNode.
|
|
|
|
* The order of the PtNode is given by the order of the array. This method makes no effort
|
2011-06-29 09:01:06 +00:00
|
|
|
* to find a good order; it only mechanically computes the size this order results in.
|
|
|
|
*
|
|
|
|
* @param dict the dictionary
|
2013-08-22 02:07:52 +00:00
|
|
|
* @param flatNodes the ordered list of PtNode arrays
|
2012-09-03 01:21:03 +00:00
|
|
|
* @param formatOptions file format options.
|
2011-06-29 09:01:06 +00:00
|
|
|
* @return the same array it was passed. The nodes have been updated for address and size.
|
|
|
|
*/
|
2013-08-16 05:51:37 +00:00
|
|
|
private static ArrayList<PtNodeArray> computeAddresses(final FusionDictionary dict,
|
|
|
|
final ArrayList<PtNodeArray> flatNodes, final FormatOptions formatOptions) {
|
2013-07-10 10:23:03 +00:00
|
|
|
// First get the worst possible sizes and offsets
|
2013-08-22 02:07:52 +00:00
|
|
|
for (final PtNodeArray n : flatNodes) calculatePtNodeArrayMaximumSize(n, formatOptions);
|
|
|
|
final int offset = initializePtNodeArraysCachedAddresses(flatNodes, formatOptions);
|
2011-06-29 09:01:06 +00:00
|
|
|
|
|
|
|
MakedictLog.i("Compressing the array addresses. Original size : " + offset);
|
|
|
|
MakedictLog.i("(Recursively seen size : " + offset + ")");
|
|
|
|
|
|
|
|
int passes = 0;
|
|
|
|
boolean changesDone = false;
|
|
|
|
do {
|
|
|
|
changesDone = false;
|
2013-08-22 02:07:52 +00:00
|
|
|
int ptNodeArrayStartOffset = 0;
|
|
|
|
for (final PtNodeArray ptNodeArray : flatNodes) {
|
|
|
|
ptNodeArray.mCachedAddressAfterUpdate = ptNodeArrayStartOffset;
|
|
|
|
final int oldNodeArraySize = ptNodeArray.mCachedSize;
|
|
|
|
final boolean changed =
|
|
|
|
computeActualPtNodeArraySize(ptNodeArray, dict, formatOptions);
|
|
|
|
final int newNodeArraySize = ptNodeArray.mCachedSize;
|
2013-08-16 05:51:37 +00:00
|
|
|
if (oldNodeArraySize < newNodeArraySize) {
|
|
|
|
throw new RuntimeException("Increased size ?!");
|
|
|
|
}
|
2013-08-22 02:07:52 +00:00
|
|
|
ptNodeArrayStartOffset += newNodeArraySize;
|
Fix a bug where a node size would be seen as increasing.
The core reason for this is quite shrewd. When a word is a bigram
of itself, the corresponding chargroup will have a bigram referring
to itself. When computing bigram offsets, we use cached addresses of
chargroups, but we compute the size of the node as we go. Hence, a
discrepancy may happen between the base offset as seen by the bigram
(which uses the recomputed value) and the target offset (which uses
the cached value).
When this happens, the cached node address is too large. The relative
offset is negative, which is expected, since it points to this very
charnode whose start is a few bytes earlier. But since the cached
address is too large, the offset is computed as smaller than it should
be.
On the next pass, the cache has been refreshed with the newly computed
size and the seen offset is now correct (or at least, much closer to
correct). The correct value is larger than the previously computed
offset, which was too small. If it happens that it crosses the -255 or
-65335 boundary, the address will be seen as needing 1 more byte than
previously computed. If this is the only change in size of this node,
the node will be seen as having a larger size than previously, which
is unexpected. Debug code was catching this and crashing the program.
So this case is very rare, but in an even rarer occurence, it may
happen that in the same node, another chargroup happens to decrease
it size by the same amount. In this case, the node may be seen as
having not been modified. This is probably extremely rare. If on
top of this, it happens that no other node has been modified, then
the file may be seen as complete, and the discrepancy left as is
in the file, leading to a broken file. The probability that this
happens is abyssally low, but the bug exists, and the current debug
code would not have caught this.
To further catch similar bugs, this change also modifies the test
that decides if the node has changed. On grounds that all components
of a node may only decrease in size with each successive pass, it's
theoritically safe to assume that the same size means the node
contents have not changed, but in case of a bug like the bug above
where a component wrongly grows while another shrinks and both cancel
each other out, the new code will catch this. Also, this change adds
a check against the number of passses, to avoid infinite loops in
case of a bug in the computation code.
This change fixes this bug by updating the cached address of each
chargroup as we go. This eliminates the discrepancy and fixes the
bug.
Bug: 6383103
Change-Id: Ia3f450e22c87c4c193cea8ddb157aebd5f224f01
2012-04-24 03:13:22 +00:00
|
|
|
changesDone |= changed;
|
2011-06-29 09:01:06 +00:00
|
|
|
}
|
2013-08-22 02:07:52 +00:00
|
|
|
updatePtNodeArraysCachedAddresses(flatNodes);
|
2011-06-29 09:01:06 +00:00
|
|
|
++passes;
|
Fix a bug where a node size would be seen as increasing.
The core reason for this is quite shrewd. When a word is a bigram
of itself, the corresponding chargroup will have a bigram referring
to itself. When computing bigram offsets, we use cached addresses of
chargroups, but we compute the size of the node as we go. Hence, a
discrepancy may happen between the base offset as seen by the bigram
(which uses the recomputed value) and the target offset (which uses
the cached value).
When this happens, the cached node address is too large. The relative
offset is negative, which is expected, since it points to this very
charnode whose start is a few bytes earlier. But since the cached
address is too large, the offset is computed as smaller than it should
be.
On the next pass, the cache has been refreshed with the newly computed
size and the seen offset is now correct (or at least, much closer to
correct). The correct value is larger than the previously computed
offset, which was too small. If it happens that it crosses the -255 or
-65335 boundary, the address will be seen as needing 1 more byte than
previously computed. If this is the only change in size of this node,
the node will be seen as having a larger size than previously, which
is unexpected. Debug code was catching this and crashing the program.
So this case is very rare, but in an even rarer occurence, it may
happen that in the same node, another chargroup happens to decrease
it size by the same amount. In this case, the node may be seen as
having not been modified. This is probably extremely rare. If on
top of this, it happens that no other node has been modified, then
the file may be seen as complete, and the discrepancy left as is
in the file, leading to a broken file. The probability that this
happens is abyssally low, but the bug exists, and the current debug
code would not have caught this.
To further catch similar bugs, this change also modifies the test
that decides if the node has changed. On grounds that all components
of a node may only decrease in size with each successive pass, it's
theoritically safe to assume that the same size means the node
contents have not changed, but in case of a bug like the bug above
where a component wrongly grows while another shrinks and both cancel
each other out, the new code will catch this. Also, this change adds
a check against the number of passses, to avoid infinite loops in
case of a bug in the computation code.
This change fixes this bug by updating the cached address of each
chargroup as we go. This eliminates the discrepancy and fixes the
bug.
Bug: 6383103
Change-Id: Ia3f450e22c87c4c193cea8ddb157aebd5f224f01
2012-04-24 03:13:22 +00:00
|
|
|
if (passes > MAX_PASSES) throw new RuntimeException("Too many passes - probably a bug");
|
2011-06-29 09:01:06 +00:00
|
|
|
} while (changesDone);
|
|
|
|
|
2013-07-10 09:28:34 +00:00
|
|
|
if (formatOptions.mSupportsDynamicUpdate) {
|
|
|
|
computeParentAddresses(flatNodes);
|
|
|
|
}
|
2013-08-22 02:07:52 +00:00
|
|
|
final PtNodeArray lastPtNodeArray = flatNodes.get(flatNodes.size() - 1);
|
2011-06-29 09:01:06 +00:00
|
|
|
MakedictLog.i("Compression complete in " + passes + " passes.");
|
|
|
|
MakedictLog.i("After address compression : "
|
2013-08-22 02:07:52 +00:00
|
|
|
+ (lastPtNodeArray.mCachedAddressAfterUpdate + lastPtNodeArray.mCachedSize));
|
2011-06-29 09:01:06 +00:00
|
|
|
|
|
|
|
return flatNodes;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Sanity-checking method.
|
|
|
|
*
|
2013-08-22 02:07:52 +00:00
|
|
|
* This method checks a list of PtNode arrays for juxtaposition, that is, it will do
|
2013-08-16 05:51:37 +00:00
|
|
|
* nothing if each node array's cached address is actually the previous node array's address
|
2011-06-29 09:01:06 +00:00
|
|
|
* plus the previous node's size.
|
|
|
|
* If this is not the case, it will throw an exception.
|
|
|
|
*
|
2013-08-16 05:51:37 +00:00
|
|
|
* @param arrays the list of node arrays to check
|
2011-06-29 09:01:06 +00:00
|
|
|
*/
|
2013-08-22 02:07:52 +00:00
|
|
|
private static void checkFlatPtNodeArrayList(final ArrayList<PtNodeArray> arrays) {
|
2011-06-29 09:01:06 +00:00
|
|
|
int offset = 0;
|
|
|
|
int index = 0;
|
2013-08-22 02:07:52 +00:00
|
|
|
for (final PtNodeArray ptNodeArray : arrays) {
|
2013-07-10 10:23:03 +00:00
|
|
|
// BeforeUpdate and AfterUpdate addresses are the same here, so it does not matter
|
|
|
|
// which we use.
|
2013-08-22 02:07:52 +00:00
|
|
|
if (ptNodeArray.mCachedAddressAfterUpdate != offset) {
|
2011-06-29 09:01:06 +00:00
|
|
|
throw new RuntimeException("Wrong address for node " + index
|
2013-08-22 02:07:52 +00:00
|
|
|
+ " : expected " + offset + ", got " +
|
|
|
|
ptNodeArray.mCachedAddressAfterUpdate);
|
2011-06-29 09:01:06 +00:00
|
|
|
}
|
|
|
|
++index;
|
2013-08-22 02:07:52 +00:00
|
|
|
offset += ptNodeArray.mCachedSize;
|
2011-06-29 09:01:06 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Helper method to write a variable-size address to a file.
|
|
|
|
*
|
|
|
|
* @param buffer the buffer to write to.
|
|
|
|
* @param index the index in the buffer to write the address to.
|
|
|
|
* @param address the address to write.
|
|
|
|
* @return the size in bytes the address actually took.
|
|
|
|
*/
|
2012-03-27 03:36:19 +00:00
|
|
|
private static int writeVariableAddress(final byte[] buffer, int index, final int address) {
|
2011-06-29 09:01:06 +00:00
|
|
|
switch (getByteSize(address)) {
|
|
|
|
case 1:
|
|
|
|
buffer[index++] = (byte)address;
|
|
|
|
return 1;
|
|
|
|
case 2:
|
|
|
|
buffer[index++] = (byte)(0xFF & (address >> 8));
|
|
|
|
buffer[index++] = (byte)(0xFF & address);
|
|
|
|
return 2;
|
|
|
|
case 3:
|
|
|
|
buffer[index++] = (byte)(0xFF & (address >> 16));
|
|
|
|
buffer[index++] = (byte)(0xFF & (address >> 8));
|
|
|
|
buffer[index++] = (byte)(0xFF & address);
|
|
|
|
return 3;
|
|
|
|
case 0:
|
|
|
|
return 0;
|
|
|
|
default:
|
|
|
|
throw new RuntimeException("Address " + address + " has a strange size");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-09-20 07:21:40 +00:00
|
|
|
/**
|
|
|
|
* Helper method to write a variable-size signed address to a file.
|
|
|
|
*
|
|
|
|
* @param buffer the buffer to write to.
|
|
|
|
* @param index the index in the buffer to write the address to.
|
|
|
|
* @param address the address to write.
|
|
|
|
* @return the size in bytes the address actually took.
|
|
|
|
*/
|
|
|
|
private static int writeVariableSignedAddress(final byte[] buffer, int index,
|
|
|
|
final int address) {
|
2013-08-14 10:18:28 +00:00
|
|
|
if (!BinaryDictIOUtils.hasChildrenAddress(address)) {
|
2012-09-20 07:21:40 +00:00
|
|
|
buffer[index] = buffer[index + 1] = buffer[index + 2] = 0;
|
|
|
|
} else {
|
|
|
|
final int absAddress = Math.abs(address);
|
2013-08-14 09:39:14 +00:00
|
|
|
buffer[index++] =
|
|
|
|
(byte)((address < 0 ? FormatSpec.MSB8 : 0) | (0xFF & (absAddress >> 16)));
|
2012-09-20 07:21:40 +00:00
|
|
|
buffer[index++] = (byte)(0xFF & (absAddress >> 8));
|
|
|
|
buffer[index++] = (byte)(0xFF & absAddress);
|
|
|
|
}
|
|
|
|
return 3;
|
|
|
|
}
|
|
|
|
|
2012-10-03 03:34:52 +00:00
|
|
|
/**
|
2013-08-22 02:07:52 +00:00
|
|
|
* Makes the flag value for a PtNode.
|
2012-10-03 03:34:52 +00:00
|
|
|
*
|
2013-08-22 02:07:52 +00:00
|
|
|
* @param hasMultipleChars whether the PtNode has multiple chars.
|
|
|
|
* @param isTerminal whether the PtNode is terminal.
|
2012-10-03 03:34:52 +00:00
|
|
|
* @param childrenAddressSize the size of a children address.
|
2013-08-22 02:07:52 +00:00
|
|
|
* @param hasShortcuts whether the PtNode has shortcuts.
|
|
|
|
* @param hasBigrams whether the PtNode has bigrams.
|
|
|
|
* @param isNotAWord whether the PtNode is not a word.
|
|
|
|
* @param isBlackListEntry whether the PtNode is a blacklist entry.
|
2012-10-03 03:34:52 +00:00
|
|
|
* @param formatOptions file format options.
|
|
|
|
* @return the flags
|
|
|
|
*/
|
2013-08-22 02:07:52 +00:00
|
|
|
static int makePtNodeFlags(final boolean hasMultipleChars, final boolean isTerminal,
|
2012-10-03 03:34:52 +00:00
|
|
|
final int childrenAddressSize, final boolean hasShortcuts, final boolean hasBigrams,
|
|
|
|
final boolean isNotAWord, final boolean isBlackListEntry,
|
|
|
|
final FormatOptions formatOptions) {
|
2011-06-29 09:01:06 +00:00
|
|
|
byte flags = 0;
|
2012-10-03 03:34:52 +00:00
|
|
|
if (hasMultipleChars) flags |= FormatSpec.FLAG_HAS_MULTIPLE_CHARS;
|
|
|
|
if (isTerminal) flags |= FormatSpec.FLAG_IS_TERMINAL;
|
|
|
|
if (formatOptions.mSupportsDynamicUpdate) {
|
|
|
|
flags |= FormatSpec.FLAG_IS_NOT_MOVED;
|
|
|
|
} else if (true) {
|
|
|
|
switch (childrenAddressSize) {
|
|
|
|
case 1:
|
2013-08-22 02:07:52 +00:00
|
|
|
flags |= FormatSpec.FLAG_CHILDREN_ADDRESS_TYPE_ONEBYTE;
|
2012-10-03 03:34:52 +00:00
|
|
|
break;
|
|
|
|
case 2:
|
2013-08-22 02:07:52 +00:00
|
|
|
flags |= FormatSpec.FLAG_CHILDREN_ADDRESS_TYPE_TWOBYTES;
|
2012-10-03 03:34:52 +00:00
|
|
|
break;
|
|
|
|
case 3:
|
2013-08-22 02:07:52 +00:00
|
|
|
flags |= FormatSpec.FLAG_CHILDREN_ADDRESS_TYPE_THREEBYTES;
|
2012-10-03 03:34:52 +00:00
|
|
|
break;
|
|
|
|
case 0:
|
2013-08-22 02:07:52 +00:00
|
|
|
flags |= FormatSpec.FLAG_CHILDREN_ADDRESS_TYPE_NOADDRESS;
|
2012-10-03 03:34:52 +00:00
|
|
|
break;
|
|
|
|
default:
|
|
|
|
throw new RuntimeException("Node with a strange address");
|
2011-12-26 10:17:39 +00:00
|
|
|
}
|
2012-08-31 06:24:39 +00:00
|
|
|
}
|
2012-10-03 03:34:52 +00:00
|
|
|
if (hasShortcuts) flags |= FormatSpec.FLAG_HAS_SHORTCUT_TARGETS;
|
|
|
|
if (hasBigrams) flags |= FormatSpec.FLAG_HAS_BIGRAMS;
|
|
|
|
if (isNotAWord) flags |= FormatSpec.FLAG_IS_NOT_A_WORD;
|
|
|
|
if (isBlackListEntry) flags |= FormatSpec.FLAG_IS_BLACKLISTED;
|
2011-06-29 09:01:06 +00:00
|
|
|
return flags;
|
|
|
|
}
|
|
|
|
|
2013-08-22 02:07:52 +00:00
|
|
|
private static byte makePtNodeFlags(final PtNode node, final int ptNodeAddress,
|
2012-10-03 03:34:52 +00:00
|
|
|
final int childrenOffset, final FormatOptions formatOptions) {
|
2013-08-22 02:07:52 +00:00
|
|
|
return (byte) makePtNodeFlags(node.mChars.length > 1, node.mFrequency >= 0,
|
2013-08-22 13:01:19 +00:00
|
|
|
getByteSize(childrenOffset),
|
2013-08-22 02:07:52 +00:00
|
|
|
node.mShortcutTargets != null && !node.mShortcutTargets.isEmpty(),
|
|
|
|
node.mBigrams != null, node.mIsNotAWord, node.mIsBlacklistEntry, formatOptions);
|
2012-10-03 03:34:52 +00:00
|
|
|
}
|
|
|
|
|
2011-06-29 09:01:06 +00:00
|
|
|
/**
|
2012-05-11 10:28:05 +00:00
|
|
|
* Makes the flag value for a bigram.
|
2011-06-29 09:01:06 +00:00
|
|
|
*
|
2012-05-11 10:28:05 +00:00
|
|
|
* @param more whether there are more bigrams after this one.
|
|
|
|
* @param offset the offset of the bigram.
|
2012-05-11 10:58:21 +00:00
|
|
|
* @param bigramFrequency the frequency of the bigram, 0..255.
|
|
|
|
* @param unigramFrequency the unigram frequency of the same word, 0..255.
|
|
|
|
* @param word the second bigram, for debugging purposes
|
2011-06-29 09:01:06 +00:00
|
|
|
* @return the flags
|
|
|
|
*/
|
2012-05-11 10:28:05 +00:00
|
|
|
private static final int makeBigramFlags(final boolean more, final int offset,
|
2012-05-11 10:58:21 +00:00
|
|
|
int bigramFrequency, final int unigramFrequency, final String word) {
|
2013-08-22 02:07:52 +00:00
|
|
|
int bigramFlags = (more ? FormatSpec.FLAG_BIGRAM_SHORTCUT_ATTR_HAS_NEXT : 0)
|
|
|
|
+ (offset < 0 ? FormatSpec.FLAG_BIGRAM_ATTR_OFFSET_NEGATIVE : 0);
|
2011-06-29 09:01:06 +00:00
|
|
|
switch (getByteSize(offset)) {
|
|
|
|
case 1:
|
2013-08-22 02:07:52 +00:00
|
|
|
bigramFlags |= FormatSpec.FLAG_BIGRAM_ATTR_ADDRESS_TYPE_ONEBYTE;
|
2011-06-29 09:01:06 +00:00
|
|
|
break;
|
|
|
|
case 2:
|
2013-08-22 02:07:52 +00:00
|
|
|
bigramFlags |= FormatSpec.FLAG_BIGRAM_ATTR_ADDRESS_TYPE_TWOBYTES;
|
2011-06-29 09:01:06 +00:00
|
|
|
break;
|
|
|
|
case 3:
|
2013-08-22 02:07:52 +00:00
|
|
|
bigramFlags |= FormatSpec.FLAG_BIGRAM_ATTR_ADDRESS_TYPE_THREEBYTES;
|
2011-06-29 09:01:06 +00:00
|
|
|
break;
|
|
|
|
default:
|
|
|
|
throw new RuntimeException("Strange offset size");
|
|
|
|
}
|
2012-05-11 10:58:21 +00:00
|
|
|
if (unigramFrequency > bigramFrequency) {
|
|
|
|
MakedictLog.e("Unigram freq is superior to bigram freq for \"" + word
|
|
|
|
+ "\". Bigram freq is " + bigramFrequency + ", unigram freq for "
|
|
|
|
+ word + " is " + unigramFrequency);
|
|
|
|
bigramFrequency = unigramFrequency;
|
|
|
|
}
|
|
|
|
// We compute the difference between 255 (which means probability = 1) and the
|
2012-05-11 12:49:55 +00:00
|
|
|
// unigram score. We split this into a number of discrete steps.
|
|
|
|
// Now, the steps are numbered 0~15; 0 represents an increase of 1 step while 15
|
|
|
|
// represents an increase of 16 steps: a value of 15 will be interpreted as the median
|
|
|
|
// value of the 16th step. In all justice, if the bigram frequency is low enough to be
|
|
|
|
// rounded below the first step (which means it is less than half a step higher than the
|
|
|
|
// unigram frequency) then the unigram frequency itself is the best approximation of the
|
|
|
|
// bigram freq that we could possibly supply, hence we should *not* include this bigram
|
|
|
|
// in the file at all.
|
|
|
|
// until this is done, we'll write 0 and slightly overestimate this case.
|
|
|
|
// In other words, 0 means "between 0.5 step and 1.5 step", 1 means "between 1.5 step
|
|
|
|
// and 2.5 steps", and 15 means "between 15.5 steps and 16.5 steps". So we want to
|
|
|
|
// divide our range [unigramFreq..MAX_TERMINAL_FREQUENCY] in 16.5 steps to get the
|
|
|
|
// step size. Then we compute the start of the first step (the one where value 0 starts)
|
|
|
|
// by adding half-a-step to the unigramFrequency. From there, we compute the integer
|
|
|
|
// number of steps to the bigramFrequency. One last thing: we want our steps to include
|
|
|
|
// their lower bound and exclude their higher bound so we need to have the first step
|
|
|
|
// start at exactly 1 unit higher than floor(unigramFreq + half a step).
|
|
|
|
// Note : to reconstruct the score, the dictionary reader will need to divide
|
2012-08-17 08:22:28 +00:00
|
|
|
// MAX_TERMINAL_FREQUENCY - unigramFreq by 16.5 likewise to get the value of the step,
|
|
|
|
// and add (discretizedFrequency + 0.5 + 0.5) times this value to get the best
|
|
|
|
// approximation. (0.5 to get the first step start, and 0.5 to get the middle of the
|
|
|
|
// step pointed by the discretized frequency.
|
2012-06-08 08:05:28 +00:00
|
|
|
final float stepSize =
|
2012-09-11 01:28:15 +00:00
|
|
|
(FormatSpec.MAX_TERMINAL_FREQUENCY - unigramFrequency)
|
|
|
|
/ (1.5f + FormatSpec.MAX_BIGRAM_FREQUENCY);
|
2012-06-08 08:05:28 +00:00
|
|
|
final float firstStepStart = 1 + unigramFrequency + (stepSize / 2.0f);
|
2012-05-11 12:49:55 +00:00
|
|
|
final int discretizedFrequency = (int)((bigramFrequency - firstStepStart) / stepSize);
|
|
|
|
// If the bigram freq is less than half-a-step higher than the unigram freq, we get -1
|
|
|
|
// here. The best approximation would be the unigram freq itself, so we should not
|
|
|
|
// include this bigram in the dictionary. For now, register as 0, and live with the
|
|
|
|
// small over-estimation that we get in this case. TODO: actually remove this bigram
|
|
|
|
// if discretizedFrequency < 0.
|
|
|
|
final int finalBigramFrequency = discretizedFrequency > 0 ? discretizedFrequency : 0;
|
2013-08-22 02:07:52 +00:00
|
|
|
bigramFlags += finalBigramFrequency & FormatSpec.FLAG_BIGRAM_SHORTCUT_ATTR_FREQUENCY;
|
2011-06-29 09:01:06 +00:00
|
|
|
return bigramFlags;
|
|
|
|
}
|
|
|
|
|
2012-04-06 06:30:42 +00:00
|
|
|
/**
|
|
|
|
* Makes the 2-byte value for options flags.
|
|
|
|
*/
|
2012-09-03 01:21:03 +00:00
|
|
|
private static final int makeOptionsValue(final FusionDictionary dictionary,
|
|
|
|
final FormatOptions formatOptions) {
|
2012-04-25 09:49:31 +00:00
|
|
|
final DictionaryOptions options = dictionary.mOptions;
|
|
|
|
final boolean hasBigrams = dictionary.hasBigrams();
|
2012-09-11 01:28:15 +00:00
|
|
|
return (options.mFrenchLigatureProcessing ? FormatSpec.FRENCH_LIGATURE_PROCESSING_FLAG : 0)
|
|
|
|
+ (options.mGermanUmlautProcessing ? FormatSpec.GERMAN_UMLAUT_PROCESSING_FLAG : 0)
|
|
|
|
+ (hasBigrams ? FormatSpec.CONTAINS_BIGRAMS_FLAG : 0)
|
2012-09-21 12:21:58 +00:00
|
|
|
+ (formatOptions.mSupportsDynamicUpdate ? FormatSpec.SUPPORTS_DYNAMIC_UPDATE : 0);
|
2012-04-06 06:30:42 +00:00
|
|
|
}
|
|
|
|
|
2012-03-27 03:36:19 +00:00
|
|
|
/**
|
|
|
|
* Makes the flag value for a shortcut.
|
|
|
|
*
|
|
|
|
* @param more whether there are more attributes after this one.
|
|
|
|
* @param frequency the frequency of the attribute, 0..15
|
|
|
|
* @return the flags
|
|
|
|
*/
|
2012-09-28 08:03:23 +00:00
|
|
|
static final int makeShortcutFlags(final boolean more, final int frequency) {
|
2013-08-22 02:07:52 +00:00
|
|
|
return (more ? FormatSpec.FLAG_BIGRAM_SHORTCUT_ATTR_HAS_NEXT : 0)
|
|
|
|
+ (frequency & FormatSpec.FLAG_BIGRAM_SHORTCUT_ATTR_FREQUENCY);
|
2012-03-27 03:36:19 +00:00
|
|
|
}
|
|
|
|
|
2012-09-20 07:21:40 +00:00
|
|
|
private static final int writeParentAddress(final byte[] buffer, final int index,
|
|
|
|
final int address, final FormatOptions formatOptions) {
|
2013-08-14 10:18:28 +00:00
|
|
|
if (BinaryDictIOUtils.supportsDynamicUpdate(formatOptions)) {
|
2012-09-20 07:21:40 +00:00
|
|
|
if (address == FormatSpec.NO_PARENT_ADDRESS) {
|
|
|
|
buffer[index] = buffer[index + 1] = buffer[index + 2] = 0;
|
|
|
|
} else {
|
|
|
|
final int absAddress = Math.abs(address);
|
2013-08-14 09:39:14 +00:00
|
|
|
assert(absAddress <= FormatSpec.SINT24_MAX);
|
|
|
|
buffer[index] = (byte)((address < 0 ? FormatSpec.MSB8 : 0)
|
2012-09-20 07:21:40 +00:00
|
|
|
| ((absAddress >> 16) & 0xFF));
|
|
|
|
buffer[index + 1] = (byte)((absAddress >> 8) & 0xFF);
|
|
|
|
buffer[index + 2] = (byte)(absAddress & 0xFF);
|
|
|
|
}
|
|
|
|
return index + 3;
|
|
|
|
} else {
|
|
|
|
return index;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-06-29 09:01:06 +00:00
|
|
|
/**
|
2013-08-22 02:07:52 +00:00
|
|
|
* Write a PtNodeArray to memory. The PtNodeArray is expected to have its final position cached.
|
2011-06-29 09:01:06 +00:00
|
|
|
*
|
2013-08-16 05:51:37 +00:00
|
|
|
* @param dict the dictionary the node array is a part of (for relative offsets).
|
2011-06-29 09:01:06 +00:00
|
|
|
* @param buffer the memory buffer to write to.
|
2013-08-22 02:07:52 +00:00
|
|
|
* @param ptNodeArray the node array to write.
|
2012-09-03 01:21:03 +00:00
|
|
|
* @param formatOptions file format options.
|
2011-06-29 09:01:06 +00:00
|
|
|
* @return the address of the END of the node.
|
|
|
|
*/
|
2012-12-06 11:47:19 +00:00
|
|
|
@SuppressWarnings("unused")
|
2012-09-03 01:21:03 +00:00
|
|
|
private static int writePlacedNode(final FusionDictionary dict, byte[] buffer,
|
2013-08-22 02:07:52 +00:00
|
|
|
final PtNodeArray ptNodeArray, final FormatOptions formatOptions) {
|
|
|
|
// TODO: Make the code in common with BinaryDictIOUtils#writePtNode
|
|
|
|
int index = ptNodeArray.mCachedAddressAfterUpdate;
|
2011-06-29 09:01:06 +00:00
|
|
|
|
2013-08-22 02:07:52 +00:00
|
|
|
final int ptNodeCount = ptNodeArray.mData.size();
|
|
|
|
final int countSize = getPtNodeCountSize(ptNodeArray);
|
|
|
|
final int parentAddress = ptNodeArray.mCachedParentAddress;
|
2012-01-16 06:17:06 +00:00
|
|
|
if (1 == countSize) {
|
2013-08-22 02:07:52 +00:00
|
|
|
buffer[index++] = (byte)ptNodeCount;
|
2012-01-16 06:17:06 +00:00
|
|
|
} else if (2 == countSize) {
|
|
|
|
// We need to signal 2-byte size by setting the top bit of the MSB to 1, so
|
|
|
|
// we | 0x80 to do this.
|
2013-08-22 02:07:52 +00:00
|
|
|
buffer[index++] = (byte)((ptNodeCount >> 8) | 0x80);
|
|
|
|
buffer[index++] = (byte)(ptNodeCount & 0xFF);
|
2012-01-16 06:17:06 +00:00
|
|
|
} else {
|
|
|
|
throw new RuntimeException("Strange size from getGroupCountSize : " + countSize);
|
|
|
|
}
|
2013-08-22 02:07:52 +00:00
|
|
|
int ptNodeAddress = index;
|
|
|
|
for (int i = 0; i < ptNodeCount; ++i) {
|
|
|
|
final PtNode ptNode = ptNodeArray.mData.get(i);
|
|
|
|
if (index != ptNode.mCachedAddressAfterUpdate) {
|
2013-07-10 11:16:13 +00:00
|
|
|
throw new RuntimeException("Bug: write index is not the same as the cached address "
|
2013-08-22 02:07:52 +00:00
|
|
|
+ "of the node : " + index + " <> " + ptNode.mCachedAddressAfterUpdate);
|
2013-07-10 11:16:13 +00:00
|
|
|
}
|
2013-08-22 02:07:52 +00:00
|
|
|
ptNodeAddress += getNodeHeaderSize(ptNode, formatOptions);
|
2011-06-29 09:01:06 +00:00
|
|
|
// Sanity checks.
|
2013-08-22 02:07:52 +00:00
|
|
|
if (DBG && ptNode.mFrequency > FormatSpec.MAX_TERMINAL_FREQUENCY) {
|
2012-09-11 01:28:15 +00:00
|
|
|
throw new RuntimeException("A node has a frequency > "
|
|
|
|
+ FormatSpec.MAX_TERMINAL_FREQUENCY
|
2013-08-22 02:07:52 +00:00
|
|
|
+ " : " + ptNode.mFrequency);
|
2011-06-29 09:01:06 +00:00
|
|
|
}
|
2013-08-22 02:07:52 +00:00
|
|
|
if (ptNode.mFrequency >= 0) ptNodeAddress += FormatSpec.PTNODE_FREQUENCY_SIZE;
|
|
|
|
final int childrenOffset = null == ptNode.mChildren
|
2012-09-11 01:28:15 +00:00
|
|
|
? FormatSpec.NO_CHILDREN_ADDRESS
|
2013-08-22 02:07:52 +00:00
|
|
|
: ptNode.mChildren.mCachedAddressAfterUpdate - ptNodeAddress;
|
2013-07-10 11:16:13 +00:00
|
|
|
buffer[index++] =
|
2013-08-22 02:07:52 +00:00
|
|
|
makePtNodeFlags(ptNode, ptNodeAddress, childrenOffset, formatOptions);
|
2012-09-03 01:21:03 +00:00
|
|
|
|
2012-09-20 07:21:40 +00:00
|
|
|
if (parentAddress == FormatSpec.NO_PARENT_ADDRESS) {
|
|
|
|
index = writeParentAddress(buffer, index, parentAddress, formatOptions);
|
|
|
|
} else {
|
2013-07-10 11:16:13 +00:00
|
|
|
index = writeParentAddress(buffer, index, parentAddress
|
2013-08-22 02:07:52 +00:00
|
|
|
+ (ptNodeArray.mCachedAddressAfterUpdate
|
|
|
|
- ptNode.mCachedAddressAfterUpdate),
|
2012-09-20 07:21:40 +00:00
|
|
|
formatOptions);
|
2012-09-03 01:21:03 +00:00
|
|
|
}
|
|
|
|
|
2013-08-22 02:07:52 +00:00
|
|
|
index = CharEncoding.writeCharArray(ptNode.mChars, buffer, index);
|
|
|
|
if (ptNode.hasSeveralChars()) {
|
|
|
|
buffer[index++] = FormatSpec.PTNODE_CHARACTERS_TERMINATOR;
|
2011-06-29 09:01:06 +00:00
|
|
|
}
|
2013-08-22 02:07:52 +00:00
|
|
|
if (ptNode.mFrequency >= 0) {
|
|
|
|
buffer[index++] = (byte) ptNode.mFrequency;
|
2011-06-29 09:01:06 +00:00
|
|
|
}
|
2012-09-20 07:21:40 +00:00
|
|
|
|
|
|
|
final int shift;
|
|
|
|
if (formatOptions.mSupportsDynamicUpdate) {
|
|
|
|
shift = writeVariableSignedAddress(buffer, index, childrenOffset);
|
|
|
|
} else {
|
|
|
|
shift = writeVariableAddress(buffer, index, childrenOffset);
|
|
|
|
}
|
2011-06-29 09:01:06 +00:00
|
|
|
index += shift;
|
2013-08-22 02:07:52 +00:00
|
|
|
ptNodeAddress += shift;
|
2011-06-29 09:01:06 +00:00
|
|
|
|
2011-12-26 10:17:39 +00:00
|
|
|
// Write shortcuts
|
2013-08-22 02:07:52 +00:00
|
|
|
if (null != ptNode.mShortcutTargets && !ptNode.mShortcutTargets.isEmpty()) {
|
2012-03-27 03:36:19 +00:00
|
|
|
final int indexOfShortcutByteSize = index;
|
2013-08-22 02:07:52 +00:00
|
|
|
index += FormatSpec.PTNODE_SHORTCUT_LIST_SIZE_SIZE;
|
|
|
|
ptNodeAddress += FormatSpec.PTNODE_SHORTCUT_LIST_SIZE_SIZE;
|
|
|
|
final Iterator<WeightedString> shortcutIterator =
|
|
|
|
ptNode.mShortcutTargets.iterator();
|
2011-12-26 10:17:39 +00:00
|
|
|
while (shortcutIterator.hasNext()) {
|
2012-05-25 10:04:54 +00:00
|
|
|
final WeightedString target = shortcutIterator.next();
|
2013-08-22 02:07:52 +00:00
|
|
|
++ptNodeAddress;
|
2012-03-27 03:36:19 +00:00
|
|
|
int shortcutFlags = makeShortcutFlags(shortcutIterator.hasNext(),
|
2011-12-26 10:17:39 +00:00
|
|
|
target.mFrequency);
|
|
|
|
buffer[index++] = (byte)shortcutFlags;
|
2012-03-27 03:36:19 +00:00
|
|
|
final int shortcutShift = CharEncoding.writeString(buffer, index, target.mWord);
|
2011-12-26 10:17:39 +00:00
|
|
|
index += shortcutShift;
|
2013-08-22 02:07:52 +00:00
|
|
|
ptNodeAddress += shortcutShift;
|
2011-12-26 10:17:39 +00:00
|
|
|
}
|
2012-03-27 03:36:19 +00:00
|
|
|
final int shortcutByteSize = index - indexOfShortcutByteSize;
|
|
|
|
if (shortcutByteSize > 0xFFFF) {
|
|
|
|
throw new RuntimeException("Shortcut list too large");
|
|
|
|
}
|
|
|
|
buffer[indexOfShortcutByteSize] = (byte)(shortcutByteSize >> 8);
|
|
|
|
buffer[indexOfShortcutByteSize + 1] = (byte)(shortcutByteSize & 0xFF);
|
2011-12-26 10:17:39 +00:00
|
|
|
}
|
2011-06-29 09:01:06 +00:00
|
|
|
// Write bigrams
|
2013-08-22 02:07:52 +00:00
|
|
|
if (null != ptNode.mBigrams) {
|
|
|
|
final Iterator<WeightedString> bigramIterator = ptNode.mBigrams.iterator();
|
2011-12-26 10:17:39 +00:00
|
|
|
while (bigramIterator.hasNext()) {
|
2012-05-25 10:04:54 +00:00
|
|
|
final WeightedString bigram = bigramIterator.next();
|
2013-08-22 02:07:52 +00:00
|
|
|
final PtNode target =
|
2013-08-16 05:51:37 +00:00
|
|
|
FusionDictionary.findWordInTree(dict.mRootNodeArray, bigram.mWord);
|
2013-07-10 11:16:13 +00:00
|
|
|
final int addressOfBigram = target.mCachedAddressAfterUpdate;
|
2012-05-11 10:28:05 +00:00
|
|
|
final int unigramFrequencyForThisWord = target.mFrequency;
|
2013-08-22 02:07:52 +00:00
|
|
|
++ptNodeAddress;
|
|
|
|
final int offset = addressOfBigram - ptNodeAddress;
|
2012-05-11 10:28:05 +00:00
|
|
|
int bigramFlags = makeBigramFlags(bigramIterator.hasNext(), offset,
|
2012-05-11 10:58:21 +00:00
|
|
|
bigram.mFrequency, unigramFrequencyForThisWord, bigram.mWord);
|
2011-06-29 09:01:06 +00:00
|
|
|
buffer[index++] = (byte)bigramFlags;
|
|
|
|
final int bigramShift = writeVariableAddress(buffer, index, Math.abs(offset));
|
|
|
|
index += bigramShift;
|
2013-08-22 02:07:52 +00:00
|
|
|
ptNodeAddress += bigramShift;
|
2011-06-29 09:01:06 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
2012-09-21 12:21:58 +00:00
|
|
|
if (formatOptions.mSupportsDynamicUpdate) {
|
2012-09-18 06:28:05 +00:00
|
|
|
buffer[index] = buffer[index + 1] = buffer[index + 2]
|
|
|
|
= FormatSpec.NO_FORWARD_LINK_ADDRESS;
|
|
|
|
index += FormatSpec.FORWARD_LINK_ADDRESS_SIZE;
|
|
|
|
}
|
2013-08-22 02:07:52 +00:00
|
|
|
if (index != ptNodeArray.mCachedAddressAfterUpdate + ptNodeArray.mCachedSize) {
|
2013-08-16 05:51:37 +00:00
|
|
|
throw new RuntimeException(
|
2013-08-22 02:07:52 +00:00
|
|
|
"Not the same size : written " + (index - ptNodeArray.mCachedAddressAfterUpdate)
|
|
|
|
+ " bytes from a node that should have " + ptNodeArray.mCachedSize + " bytes");
|
2013-08-16 05:51:37 +00:00
|
|
|
}
|
2011-06-29 09:01:06 +00:00
|
|
|
return index;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2013-08-22 02:07:52 +00:00
|
|
|
* Dumps a collection of useful statistics about a list of PtNode arrays.
|
2011-06-29 09:01:06 +00:00
|
|
|
*
|
|
|
|
* This prints purely informative stuff, like the total estimated file size, the
|
2013-08-22 02:07:52 +00:00
|
|
|
* number of PtNode arrays, of PtNodes, the repartition of each address size, etc
|
2011-06-29 09:01:06 +00:00
|
|
|
*
|
2013-08-22 02:07:52 +00:00
|
|
|
* @param ptNodeArrays the list of PtNode arrays.
|
2011-06-29 09:01:06 +00:00
|
|
|
*/
|
2013-08-22 02:07:52 +00:00
|
|
|
private static void showStatistics(ArrayList<PtNodeArray> ptNodeArrays) {
|
2011-06-29 09:01:06 +00:00
|
|
|
int firstTerminalAddress = Integer.MAX_VALUE;
|
|
|
|
int lastTerminalAddress = Integer.MIN_VALUE;
|
|
|
|
int size = 0;
|
2013-08-22 02:07:52 +00:00
|
|
|
int ptNodes = 0;
|
|
|
|
int maxNodes = 0;
|
2011-06-29 09:01:06 +00:00
|
|
|
int maxRuns = 0;
|
2013-08-22 02:07:52 +00:00
|
|
|
for (final PtNodeArray ptNodeArray : ptNodeArrays) {
|
|
|
|
if (maxNodes < ptNodeArray.mData.size()) maxNodes = ptNodeArray.mData.size();
|
|
|
|
for (final PtNode ptNode : ptNodeArray.mData) {
|
|
|
|
++ptNodes;
|
|
|
|
if (ptNode.mChars.length > maxRuns) maxRuns = ptNode.mChars.length;
|
|
|
|
if (ptNode.mFrequency >= 0) {
|
|
|
|
if (ptNodeArray.mCachedAddressAfterUpdate < firstTerminalAddress)
|
|
|
|
firstTerminalAddress = ptNodeArray.mCachedAddressAfterUpdate;
|
|
|
|
if (ptNodeArray.mCachedAddressAfterUpdate > lastTerminalAddress)
|
|
|
|
lastTerminalAddress = ptNodeArray.mCachedAddressAfterUpdate;
|
2011-06-29 09:01:06 +00:00
|
|
|
}
|
|
|
|
}
|
2013-08-22 02:07:52 +00:00
|
|
|
if (ptNodeArray.mCachedAddressAfterUpdate + ptNodeArray.mCachedSize > size) {
|
|
|
|
size = ptNodeArray.mCachedAddressAfterUpdate + ptNodeArray.mCachedSize;
|
2013-07-10 10:23:03 +00:00
|
|
|
}
|
2011-06-29 09:01:06 +00:00
|
|
|
}
|
2013-08-22 02:07:52 +00:00
|
|
|
final int[] ptNodeCounts = new int[maxNodes + 1];
|
2011-06-29 09:01:06 +00:00
|
|
|
final int[] runCounts = new int[maxRuns + 1];
|
2013-08-22 02:07:52 +00:00
|
|
|
for (final PtNodeArray ptNodeArray : ptNodeArrays) {
|
|
|
|
++ptNodeCounts[ptNodeArray.mData.size()];
|
|
|
|
for (final PtNode ptNode : ptNodeArray.mData) {
|
|
|
|
++runCounts[ptNode.mChars.length];
|
2011-06-29 09:01:06 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
MakedictLog.i("Statistics:\n"
|
|
|
|
+ " total file size " + size + "\n"
|
2013-08-22 02:07:52 +00:00
|
|
|
+ " " + ptNodeArrays.size() + " node arrays\n"
|
|
|
|
+ " " + ptNodes + " PtNodes (" + ((float)ptNodes / ptNodeArrays.size())
|
|
|
|
+ " PtNodes per node)\n"
|
2011-06-29 09:01:06 +00:00
|
|
|
+ " first terminal at " + firstTerminalAddress + "\n"
|
|
|
|
+ " last terminal at " + lastTerminalAddress + "\n"
|
2013-08-22 02:07:52 +00:00
|
|
|
+ " PtNode stats : max = " + maxNodes);
|
|
|
|
for (int i = 0; i < ptNodeCounts.length; ++i) {
|
|
|
|
MakedictLog.i(" " + i + " : " + ptNodeCounts[i]);
|
2011-06-29 09:01:06 +00:00
|
|
|
}
|
|
|
|
MakedictLog.i(" Character run stats : max = " + maxRuns);
|
|
|
|
for (int i = 0; i < runCounts.length; ++i) {
|
|
|
|
MakedictLog.i(" " + i + " : " + runCounts[i]);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Dumps a FusionDictionary to a file.
|
|
|
|
*
|
|
|
|
* @param destination the stream to write the binary data to.
|
|
|
|
* @param dict the dictionary to write.
|
2012-09-03 01:21:03 +00:00
|
|
|
* @param formatOptions file format options.
|
2011-06-29 09:01:06 +00:00
|
|
|
*/
|
2013-08-21 12:15:21 +00:00
|
|
|
/* package */ static void writeDictionaryBinary(final OutputStream destination,
|
2012-09-05 03:37:56 +00:00
|
|
|
final FusionDictionary dict, final FormatOptions formatOptions)
|
2012-03-06 07:15:42 +00:00
|
|
|
throws IOException, UnsupportedFormatException {
|
2011-06-29 09:01:06 +00:00
|
|
|
|
2013-08-16 05:51:37 +00:00
|
|
|
// Addresses are limited to 3 bytes, but since addresses can be relative to each node
|
|
|
|
// array, the structure itself is not limited to 16MB. However, if it is over 16MB deciding
|
2013-08-22 02:07:52 +00:00
|
|
|
// the order of the PtNode arrays becomes a quite complicated problem, because though the
|
2013-08-16 05:51:37 +00:00
|
|
|
// dictionary itself does not have a size limit, each node array must still be within 16MB
|
|
|
|
// of all its children and parents. As long as this is ensured, the dictionary file may
|
|
|
|
// grow to any size.
|
2011-06-29 09:01:06 +00:00
|
|
|
|
2012-09-05 03:37:56 +00:00
|
|
|
final int version = formatOptions.mVersion;
|
2012-09-11 01:28:15 +00:00
|
|
|
if (version < FormatSpec.MINIMUM_SUPPORTED_VERSION
|
|
|
|
|| version > FormatSpec.MAXIMUM_SUPPORTED_VERSION) {
|
2012-02-27 10:50:34 +00:00
|
|
|
throw new UnsupportedFormatException("Requested file format version " + version
|
|
|
|
+ ", but this implementation only supports versions "
|
2012-09-11 01:28:15 +00:00
|
|
|
+ FormatSpec.MINIMUM_SUPPORTED_VERSION + " through "
|
|
|
|
+ FormatSpec.MAXIMUM_SUPPORTED_VERSION);
|
2012-02-27 10:50:34 +00:00
|
|
|
}
|
|
|
|
|
2012-04-18 21:15:34 +00:00
|
|
|
ByteArrayOutputStream headerBuffer = new ByteArrayOutputStream(256);
|
|
|
|
|
2012-03-06 07:15:42 +00:00
|
|
|
// The magic number in big-endian order.
|
2013-08-08 05:16:50 +00:00
|
|
|
// Magic number for all versions.
|
|
|
|
headerBuffer.write((byte) (0xFF & (FormatSpec.MAGIC_NUMBER >> 24)));
|
|
|
|
headerBuffer.write((byte) (0xFF & (FormatSpec.MAGIC_NUMBER >> 16)));
|
|
|
|
headerBuffer.write((byte) (0xFF & (FormatSpec.MAGIC_NUMBER >> 8)));
|
|
|
|
headerBuffer.write((byte) (0xFF & FormatSpec.MAGIC_NUMBER));
|
|
|
|
// Dictionary version.
|
|
|
|
headerBuffer.write((byte) (0xFF & (version >> 8)));
|
|
|
|
headerBuffer.write((byte) (0xFF & version));
|
|
|
|
|
2011-06-29 09:01:06 +00:00
|
|
|
// Options flags
|
2012-09-03 01:21:03 +00:00
|
|
|
final int options = makeOptionsValue(dict, formatOptions);
|
2012-04-18 21:15:34 +00:00
|
|
|
headerBuffer.write((byte) (0xFF & (options >> 8)));
|
|
|
|
headerBuffer.write((byte) (0xFF & options));
|
2013-08-08 05:16:50 +00:00
|
|
|
final int headerSizeOffset = headerBuffer.size();
|
|
|
|
// Placeholder to be written later with header size.
|
|
|
|
for (int i = 0; i < 4; ++i) {
|
|
|
|
headerBuffer.write(0);
|
|
|
|
}
|
|
|
|
// Write out the options.
|
|
|
|
for (final String key : dict.mOptions.mAttributes.keySet()) {
|
|
|
|
final String value = dict.mOptions.mAttributes.get(key);
|
|
|
|
CharEncoding.writeString(headerBuffer, key);
|
|
|
|
CharEncoding.writeString(headerBuffer, value);
|
|
|
|
}
|
|
|
|
final int size = headerBuffer.size();
|
|
|
|
final byte[] bytes = headerBuffer.toByteArray();
|
|
|
|
// Write out the header size.
|
|
|
|
bytes[headerSizeOffset] = (byte) (0xFF & (size >> 24));
|
|
|
|
bytes[headerSizeOffset + 1] = (byte) (0xFF & (size >> 16));
|
|
|
|
bytes[headerSizeOffset + 2] = (byte) (0xFF & (size >> 8));
|
|
|
|
bytes[headerSizeOffset + 3] = (byte) (0xFF & (size >> 0));
|
|
|
|
destination.write(bytes);
|
2011-06-29 09:01:06 +00:00
|
|
|
|
2012-04-18 21:15:34 +00:00
|
|
|
headerBuffer.close();
|
2011-06-29 09:01:06 +00:00
|
|
|
|
|
|
|
// Leave the choice of the optimal node order to the flattenTree function.
|
|
|
|
MakedictLog.i("Flattening the tree...");
|
2013-08-16 05:51:37 +00:00
|
|
|
ArrayList<PtNodeArray> flatNodes = flattenTree(dict.mRootNodeArray);
|
2011-06-29 09:01:06 +00:00
|
|
|
|
|
|
|
MakedictLog.i("Computing addresses...");
|
2012-09-03 01:21:03 +00:00
|
|
|
computeAddresses(dict, flatNodes, formatOptions);
|
2013-08-22 02:07:52 +00:00
|
|
|
MakedictLog.i("Checking PtNode array...");
|
|
|
|
if (DBG) checkFlatPtNodeArrayList(flatNodes);
|
2011-06-29 09:01:06 +00:00
|
|
|
|
2012-04-18 21:15:34 +00:00
|
|
|
// Create a buffer that matches the final dictionary size.
|
2013-08-16 05:51:37 +00:00
|
|
|
final PtNodeArray lastNodeArray = flatNodes.get(flatNodes.size() - 1);
|
|
|
|
final int bufferSize = lastNodeArray.mCachedAddressAfterUpdate + lastNodeArray.mCachedSize;
|
2012-04-18 21:15:34 +00:00
|
|
|
final byte[] buffer = new byte[bufferSize];
|
|
|
|
int index = 0;
|
|
|
|
|
2011-06-29 09:01:06 +00:00
|
|
|
MakedictLog.i("Writing file...");
|
|
|
|
int dataEndOffset = 0;
|
2013-08-16 05:51:37 +00:00
|
|
|
for (PtNodeArray nodeArray : flatNodes) {
|
|
|
|
dataEndOffset = writePlacedNode(dict, buffer, nodeArray, formatOptions);
|
2011-06-29 09:01:06 +00:00
|
|
|
}
|
|
|
|
|
2012-05-11 14:04:24 +00:00
|
|
|
if (DBG) showStatistics(flatNodes);
|
2011-06-29 09:01:06 +00:00
|
|
|
|
|
|
|
destination.write(buffer, 0, dataEndOffset);
|
|
|
|
|
|
|
|
destination.close();
|
|
|
|
MakedictLog.i("Done");
|
|
|
|
}
|
|
|
|
}
|