aboutsummaryrefslogtreecommitdiffstats
path: root/native/jni/src/binary_format.h
diff options
context:
space:
mode:
authorJean Chalard <jchalard@google.com>2012-05-21 11:52:23 -0700
committerAndroid Git Automerger <android-git-automerger@android.com>2012-05-21 11:52:23 -0700
commit239401745b6d032bddd3b7662cc22d7daf81bced (patch)
tree1177303128f4cd7a05a247ae000afc7047328326 /native/jni/src/binary_format.h
parent2164e19c4a5a627938140719196dc934566a2d20 (diff)
parent7557d3c6f3e7669fa213dbabda0f399e496b56b7 (diff)
downloadlatinime-239401745b6d032bddd3b7662cc22d7daf81bced.tar.gz
latinime-239401745b6d032bddd3b7662cc22d7daf81bced.tar.xz
latinime-239401745b6d032bddd3b7662cc22d7daf81bced.zip
am 7557d3c6: am bc77adef: Merge "Return the bigram frequency if available." into jb-dev
* commit '7557d3c6f3e7669fa213dbabda0f399e496b56b7': Return the bigram frequency if available.
Diffstat (limited to 'native/jni/src/binary_format.h')
-rw-r--r--native/jni/src/binary_format.h32
1 files changed, 23 insertions, 9 deletions
diff --git a/native/jni/src/binary_format.h b/native/jni/src/binary_format.h
index b87593ca9..40f197619 100644
--- a/native/jni/src/binary_format.h
+++ b/native/jni/src/binary_format.h
@@ -520,19 +520,33 @@ inline int BinaryFormat::getWordAtAddress(const uint8_t* const root, const int a
return 0;
}
-// This should probably return a probability in log space.
+static inline int backoff(const int unigramFreq) {
+ return unigramFreq;
+ // For some reason, applying the backoff weight gives bad results in tests. To apply the
+ // backoff weight, we divide the probability by 2, which in our storing format means
+ // decreasing the score by 8.
+ // TODO: figure out what's wrong with this.
+ // return unigramFreq > 8 ? unigramFreq - 8 : (0 == unigramFreq ? 0 : 8);
+}
+
+// This returns a probability in log space.
inline int BinaryFormat::getProbability(const int position, const std::map<int, int> *bigramMap,
const uint8_t *bigramFilter, const int unigramFreq) {
- if (!bigramMap || !bigramFilter) return unigramFreq;
- if (!isInFilter(bigramFilter, position)) return unigramFreq;
- const std::map<int, int>::const_iterator bigramFreq = bigramMap->find(position);
- if (bigramFreq != bigramMap->end()) {
- // TODO: return the frequency in bigramFreq->second
- return unigramFreq;
+ if (!bigramMap || !bigramFilter) return backoff(unigramFreq);
+ if (!isInFilter(bigramFilter, position)) return backoff(unigramFreq);
+ const std::map<int, int>::const_iterator bigramFreqIt = bigramMap->find(position);
+ if (bigramFreqIt != bigramMap->end()) {
+ const int bigramFreq = bigramFreqIt->second;
+ // We divide the range [unigramFreq..255] in 16.5 steps - in other words, we want the
+ // unigram frequency to be the median value of the 17th step from the top. A value of
+ // 0 for the bigram frequency represents the middle of the 16th step from the top,
+ // while a value of 15 represents the middle of the top step.
+ // See makedict.BinaryDictInputOutput for details.
+ const float stepSize = ((float)MAX_FREQ - unigramFreq) / (1.5f + MAX_BIGRAM_FREQ);
+ return (int)(unigramFreq + bigramFreq * stepSize);
} else {
- return unigramFreq;
+ return backoff(unigramFreq);
}
- // TODO: if the unigram frequency is used, compute the actual probability
}
} // namespace latinime