chart decoding uses unordered set

This commit is contained in:
Hieu Hoang 2015-10-14 10:37:51 +01:00
parent 1a93fae9ac
commit 9b9992671b
3 changed files with 41 additions and 38 deletions

View File

@ -181,32 +181,6 @@ void ChartHypothesis::GetOutputPhrase(size_t leftRightMost, size_t numWords, Phr
}
}
/** check, if two hypothesis can be recombined.
this is actually a sorting function that allows us to
keep an ordered list of hypotheses. This makes recombination
much quicker. Returns one of 3 possible values:
-1 = this < compare
+1 = this > compare
0 = this ==compare
\param compare the other hypo to compare to
*/
int ChartHypothesis::RecombineCompare(const ChartHypothesis &compare) const
{
int comp = 0;
for (unsigned i = 0; i < m_ffStates.size(); ++i) {
if (m_ffStates[i] == NULL || compare.m_ffStates[i] == NULL)
comp = m_ffStates[i] - compare.m_ffStates[i];
else
comp = m_ffStates[i]->Compare(*compare.m_ffStates[i]);
if (comp != 0)
return comp;
}
return 0;
}
/** calculate total score */
void ChartHypothesis::EvaluateWhenApplied()
{
@ -325,6 +299,33 @@ void ChartHypothesis::SetWinningHypo(const ChartHypothesis *hypo)
m_winningHypo = hypo;
}
size_t ChartHypothesis::hash() const
{
size_t seed;
// states
for (size_t i = 0; i < m_ffStates.size(); ++i) {
const FFState *state = m_ffStates[i];
size_t hash = state->hash();
boost::hash_combine(seed, hash);
}
return seed;
}
bool ChartHypothesis::operator==(const ChartHypothesis& other) const
{
// states
for (size_t i = 0; i < m_ffStates.size(); ++i) {
const FFState &thisState = *m_ffStates[i];
const FFState &otherState = *other.m_ffStates[i];
if (thisState != otherState) {
return false;
}
}
return true;
}
TO_STRING_BODY(ChartHypothesis)
// friend

View File

@ -146,8 +146,6 @@ public:
// leftRightMost: 1=left, 2=right
void GetOutputPhrase(size_t leftRightMost, size_t numWords, Phrase &outPhrase) const;
int RecombineCompare(const ChartHypothesis &compare) const;
void EvaluateWhenApplied();
void AddArc(ChartHypothesis *loserHypo);
@ -214,6 +212,10 @@ public:
return m_winningHypo;
}
// for unordered_set in stack
size_t hash() const;
bool operator==(const ChartHypothesis& other) const;
TO_STRING();
}; // class ChartHypothesis

View File

@ -42,18 +42,17 @@ public:
/** functor to compare (chart) hypotheses by feature function states.
* If 2 hypos are equal, according to this functor, then they can be recombined.
*/
class ChartHypothesisRecombinationOrderer
class ChartHypothesisRecombinationUnordered
{
public:
bool operator()(const ChartHypothesis* hypoA, const ChartHypothesis* hypoB) const {
// assert in same cell
assert(hypoA->GetCurrSourceRange() == hypoB->GetCurrSourceRange());
// shouldn't be mixing hypos with different lhs
assert(hypoA->GetTargetLHS() == hypoB->GetTargetLHS());
return (hypoA->RecombineCompare(*hypoB) < 0);
size_t operator()(const ChartHypothesis* hypo) const {
return hypo->hash();
}
bool operator()(const ChartHypothesis* hypoA, const ChartHypothesis* hypoB) const {
return (*hypoA) == (*hypoB);
}
};
/** Contains a set of unique hypos that have the same HS non-term.
@ -64,7 +63,8 @@ class ChartHypothesisCollection
friend std::ostream& operator<<(std::ostream&, const ChartHypothesisCollection&);
protected:
typedef std::set<ChartHypothesis*, ChartHypothesisRecombinationOrderer> HCType;
//typedef std::set<ChartHypothesis*, ChartHypothesisRecombinationOrderer> HCType;
typedef boost::unordered_set< ChartHypothesis*, ChartHypothesisRecombinationUnordered, ChartHypothesisRecombinationUnordered > HCType;
HCType m_hypos;
HypoList m_hyposOrdered;