Get n-gram probability in structure policy.

Bug: 14425059
Change-Id: Id955a2e07dbdfb90ae55720f0c92597e0594c0d9
This commit is contained in:
Keisuke Kuroyanagi 2014-07-29 12:01:18 +09:00
parent 96d47fe745
commit 1229879e7c
4 changed files with 39 additions and 27 deletions

View File

@ -88,13 +88,7 @@ void Dictionary::getPredictions(const PrevWordsInfo *const prevWordsInfo,
}
int Dictionary::getProbability(const int *word, int length) const {
TimeKeeper::setCurrentTime();
int pos = getDictionaryStructurePolicy()->getTerminalPtNodePositionOfWord(word, length,
false /* forceLowerCaseSearch */);
if (NOT_A_DICT_POS == pos) {
return NOT_A_PROBABILITY;
}
return getDictionaryStructurePolicy()->getProbabilityOfPtNode(nullptr /* prevWordsInfo */, pos);
return getNgramProbability(nullptr /* prevWordsInfo */, word, length);
}
int Dictionary::getMaxProbabilityOfExactMatches(const int *word, int length) const {
@ -109,18 +103,7 @@ int Dictionary::getNgramProbability(const PrevWordsInfo *const prevWordsInfo, co
int nextWordPos = mDictionaryStructureWithBufferPolicy->getTerminalPtNodePositionOfWord(word,
length, false /* forceLowerCaseSearch */);
if (NOT_A_DICT_POS == nextWordPos) return NOT_A_PROBABILITY;
BinaryDictionaryBigramsIterator bigramsIt = prevWordsInfo->getBigramsIteratorForPrediction(
mDictionaryStructureWithBufferPolicy.get());
while (bigramsIt.hasNext()) {
bigramsIt.next();
if (bigramsIt.getBigramPos() == nextWordPos
&& bigramsIt.getProbability() != NOT_A_PROBABILITY) {
return mDictionaryStructureWithBufferPolicy->getProbability(
mDictionaryStructureWithBufferPolicy->getProbabilityOfPtNode(
nullptr /* prevWordsInfo */, nextWordPos), bigramsIt.getProbability());
}
}
return NOT_A_PROBABILITY;
return getDictionaryStructurePolicy()->getProbabilityOfPtNode(prevWordsInfo, nextWordPos);
}
bool Dictionary::addUnigramEntry(const int *const word, const int length,

View File

@ -140,6 +140,18 @@ int Ver4PatriciaTriePolicy::getProbabilityOfPtNode(const PrevWordsInfo *const pr
if (ptNodeParams.isDeleted() || ptNodeParams.isBlacklisted() || ptNodeParams.isNotAWord()) {
return NOT_A_PROBABILITY;
}
if (prevWordsInfo) {
BinaryDictionaryBigramsIterator bigramsIt =
prevWordsInfo->getBigramsIteratorForPrediction(this /* dictStructurePolicy */);
while (bigramsIt.hasNext()) {
bigramsIt.next();
if (bigramsIt.getBigramPos() == ptNodePos
&& bigramsIt.getProbability() != NOT_A_PROBABILITY) {
return getProbability(ptNodeParams.getProbability(), bigramsIt.getProbability());
}
}
return NOT_A_PROBABILITY;
}
return getProbability(ptNodeParams.getProbability(), NOT_A_PROBABILITY);
}

View File

@ -21,6 +21,7 @@
#include "suggest/core/dicnode/dic_node.h"
#include "suggest/core/dicnode/dic_node_vector.h"
#include "suggest/core/dictionary/binary_dictionary_bigrams_iterator.h"
#include "suggest/core/session/prev_words_info.h"
#include "suggest/policyimpl/dictionary/structure/pt_common/dynamic_pt_reading_helper.h"
#include "suggest/policyimpl/dictionary/structure/pt_common/patricia_trie_reading_utils.h"
#include "suggest/policyimpl/dictionary/utils/probability_utils.h"
@ -284,10 +285,6 @@ int PatriciaTriePolicy::getProbability(const int unigramProbability,
int PatriciaTriePolicy::getProbabilityOfPtNode(const PrevWordsInfo *const prevWordsInfo,
const int ptNodePos) const {
if (prevWordsInfo) {
// TODO: Return probability using prevWordsInfo.
return NOT_A_PROBABILITY;
}
if (ptNodePos == NOT_A_DICT_POS) {
return NOT_A_PROBABILITY;
}
@ -299,6 +296,18 @@ int PatriciaTriePolicy::getProbabilityOfPtNode(const PrevWordsInfo *const prevWo
// for shortcuts).
return NOT_A_PROBABILITY;
}
if (prevWordsInfo) {
BinaryDictionaryBigramsIterator bigramsIt =
prevWordsInfo->getBigramsIteratorForPrediction(this /* dictStructurePolicy */);
while (bigramsIt.hasNext()) {
bigramsIt.next();
if (bigramsIt.getBigramPos() == ptNodePos
&& bigramsIt.getProbability() != NOT_A_PROBABILITY) {
return getProbability(ptNodeParams.getProbability(), bigramsIt.getProbability());
}
}
return NOT_A_PROBABILITY;
}
return getProbability(ptNodeParams.getProbability(), NOT_A_PROBABILITY);
}

View File

@ -123,10 +123,6 @@ int Ver4PatriciaTriePolicy::getProbability(const int unigramProbability,
int Ver4PatriciaTriePolicy::getProbabilityOfPtNode(const PrevWordsInfo *const prevWordsInfo,
const int ptNodePos) const {
if (prevWordsInfo) {
// TODO: Return probability using prevWordsInfo.
return NOT_A_PROBABILITY;
}
if (ptNodePos == NOT_A_DICT_POS) {
return NOT_A_PROBABILITY;
}
@ -134,6 +130,18 @@ int Ver4PatriciaTriePolicy::getProbabilityOfPtNode(const PrevWordsInfo *const pr
if (ptNodeParams.isDeleted() || ptNodeParams.isBlacklisted() || ptNodeParams.isNotAWord()) {
return NOT_A_PROBABILITY;
}
if (prevWordsInfo) {
BinaryDictionaryBigramsIterator bigramsIt =
prevWordsInfo->getBigramsIteratorForPrediction(this /* dictStructurePolicy */);
while (bigramsIt.hasNext()) {
bigramsIt.next();
if (bigramsIt.getBigramPos() == ptNodePos
&& bigramsIt.getProbability() != NOT_A_PROBABILITY) {
return getProbability(ptNodeParams.getProbability(), bigramsIt.getProbability());
}
}
return NOT_A_PROBABILITY;
}
return getProbability(ptNodeParams.getProbability(), NOT_A_PROBABILITY);
}