am 332f4327: Merge "Fix a behavior change in dicttool"

* commit '332f43275ddbefa5f2376381f6f8d6115d69b3b4':
  Fix a behavior change in dicttool
main
Jean Chalard 2014-10-03 09:36:16 +00:00 committed by Android Git Automerger
commit ddaa176476
2 changed files with 30 additions and 13 deletions

View File

@ -54,6 +54,9 @@ public final class WordProperty implements Comparable<WordProperty> {
mWord = word; mWord = word;
mProbabilityInfo = probabilityInfo; mProbabilityInfo = probabilityInfo;
mShortcutTargets = shortcutTargets; mShortcutTargets = shortcutTargets;
if (null == bigrams) {
mNgrams = null;
} else {
mNgrams = new ArrayList<>(); mNgrams = new ArrayList<>();
final NgramContext ngramContext = new NgramContext(new WordInfo(mWord)); final NgramContext ngramContext = new NgramContext(new WordInfo(mWord));
if (bigrams != null) { if (bigrams != null) {
@ -61,6 +64,7 @@ public final class WordProperty implements Comparable<WordProperty> {
mNgrams.add(new NgramProperty(bigramTarget, ngramContext)); mNgrams.add(new NgramProperty(bigramTarget, ngramContext));
} }
} }
}
mIsBeginningOfSentence = false; mIsBeginningOfSentence = false;
mIsNotAWord = isNotAWord; mIsNotAWord = isNotAWord;
mIsBlacklistEntry = isBlacklistEntry; mIsBlacklistEntry = isBlacklistEntry;
@ -87,7 +91,7 @@ public final class WordProperty implements Comparable<WordProperty> {
mWord = StringUtils.getStringFromNullTerminatedCodePointArray(codePoints); mWord = StringUtils.getStringFromNullTerminatedCodePointArray(codePoints);
mProbabilityInfo = createProbabilityInfoFromArray(probabilityInfo); mProbabilityInfo = createProbabilityInfoFromArray(probabilityInfo);
mShortcutTargets = new ArrayList<>(); mShortcutTargets = new ArrayList<>();
mNgrams = new ArrayList<>(); final ArrayList<NgramProperty> ngrams = new ArrayList<>();
mIsBeginningOfSentence = isBeginningOfSentence; mIsBeginningOfSentence = isBeginningOfSentence;
mIsNotAWord = isNotAWord; mIsNotAWord = isNotAWord;
mIsBlacklistEntry = isBlacklisted; mIsBlacklistEntry = isBlacklisted;
@ -104,8 +108,9 @@ public final class WordProperty implements Comparable<WordProperty> {
final WeightedString ngramTarget = new WeightedString(ngramTargetString, final WeightedString ngramTarget = new WeightedString(ngramTargetString,
createProbabilityInfoFromArray(bigramProbabilityInfo.get(i))); createProbabilityInfoFromArray(bigramProbabilityInfo.get(i)));
// TODO: Support n-gram. // TODO: Support n-gram.
mNgrams.add(new NgramProperty(ngramTarget, ngramContext)); ngrams.add(new NgramProperty(ngramTarget, ngramContext));
} }
mNgrams = ngrams.isEmpty() ? null : ngrams;
final int shortcutTargetCount = shortcutTargets.size(); final int shortcutTargetCount = shortcutTargets.size();
for (int i = 0; i < shortcutTargetCount; i++) { for (int i = 0; i < shortcutTargetCount; i++) {
@ -118,6 +123,9 @@ public final class WordProperty implements Comparable<WordProperty> {
// TODO: Remove // TODO: Remove
public ArrayList<WeightedString> getBigrams() { public ArrayList<WeightedString> getBigrams() {
if (null == mNgrams) {
return null;
}
final ArrayList<WeightedString> bigrams = new ArrayList<>(); final ArrayList<WeightedString> bigrams = new ArrayList<>();
for (final NgramProperty ngram : mNgrams) { for (final NgramProperty ngram : mNgrams) {
if (ngram.mNgramContext.getPrevWordCount() == 1) { if (ngram.mNgramContext.getPrevWordCount() == 1) {
@ -167,11 +175,18 @@ public final class WordProperty implements Comparable<WordProperty> {
if (!(o instanceof WordProperty)) return false; if (!(o instanceof WordProperty)) return false;
WordProperty w = (WordProperty)o; WordProperty w = (WordProperty)o;
return mProbabilityInfo.equals(w.mProbabilityInfo) && mWord.equals(w.mWord) return mProbabilityInfo.equals(w.mProbabilityInfo) && mWord.equals(w.mWord)
&& mShortcutTargets.equals(w.mShortcutTargets) && mNgrams.equals(w.mNgrams) && mShortcutTargets.equals(w.mShortcutTargets) && equals(mNgrams, w.mNgrams)
&& mIsNotAWord == w.mIsNotAWord && mIsBlacklistEntry == w.mIsBlacklistEntry && mIsNotAWord == w.mIsNotAWord && mIsBlacklistEntry == w.mIsBlacklistEntry
&& mHasNgrams == w.mHasNgrams && mHasShortcuts && w.mHasNgrams; && mHasNgrams == w.mHasNgrams && mHasShortcuts && w.mHasNgrams;
} }
private <T> boolean equals(final ArrayList<T> a, final ArrayList<T> b) {
if (null == a) {
return null == b;
}
return a.equals(b);
}
@Override @Override
public int hashCode() { public int hashCode() {
if (mHashCode == 0) { if (mHashCode == 0) {

View File

@ -715,12 +715,14 @@ public class BinaryDictDecoderEncoderTests extends AndroidTestCase {
} }
assertTrue(shortcutList.isEmpty()); assertTrue(shortcutList.isEmpty());
} }
if (wordProperty.mHasNgrams) {
for (final WeightedString bigramTarget : wordProperty.getBigrams()) { for (final WeightedString bigramTarget : wordProperty.getBigrams()) {
final String word1 = bigramTarget.mWord; final String word1 = bigramTarget.mWord;
final Pair<String, String> bigram = new Pair<>(word0, word1); final Pair<String, String> bigram = new Pair<>(word0, word1);
assertTrue(bigramSet.contains(bigram)); assertTrue(bigramSet.contains(bigram));
bigramSet.remove(bigram); bigramSet.remove(bigram);
} }
}
token = result.mNextToken; token = result.mNextToken;
} while (token != 0); } while (token != 0);
assertTrue(wordSet.isEmpty()); assertTrue(wordSet.isEmpty());