/** * return value as the model suggested. The history state must be historified * or the history's level should be 0. when level == 0 but idx != 0, the * history is a psuedo unigram state used for this model to combine another * bigram cache language model */ double CThreadSlm::rawTransfer(TState history, unsigned int wid, TState& result) { unsigned int lvl = history.getLevel(); unsigned int pos = history.getIdx(); double cost = (m_UseLogPr)?0.0:1.0; // NON_Word id must be dealed with special, let it transfer to root // without any cost if (ID_NOT_WORD == wid) { result = 0; return cost; } while (true) { //for psuedo cache model unigram state TNode* pn = ((TNode *)m_Levels[lvl]) + ((lvl)?pos:0); unsigned int t = (pn+1)->ch(); if (lvl < m_N-1) { TNode* pBase =(TNode*)m_Levels[lvl+1]; unsigned int idx = find_id(pBase, pn->ch(), t, wid); if (idx != t) { result.setIdx(idx); result.setLevel(lvl+1); double pr = m_prTable[pBase[idx].pr()]; return (m_UseLogPr)?(cost+pr):(cost*pr); } } else { TLeaf* pBase =(TLeaf*)m_Levels[lvl+1]; unsigned int idx = find_id(pBase, pn->ch(), t, wid); if (idx != t) { result.setIdx(idx); result.setLevel(lvl+1); double pr = m_prTable[pBase[idx].pr()]; return (m_UseLogPr)?(cost+pr):(cost*pr); } } if (m_UseLogPr) cost += m_bowTable[pn->bow()]; else cost *= m_bowTable[pn->bow()]; if (lvl == 0) break; lvl = pn->bol(); pos = pn->bon(); } result.setLevel(0); result.setIdx(0); if (m_UseLogPr) return cost + m_prTable[((TNode *)m_Levels[0])->pr()]; else return cost * m_prTable[((TNode *)m_Levels[0])->pr()]; }
CThreadSlm::TState CThreadSlm::history_state_of(TState st) { if (st.getLevel() >= m_N) { TLeaf* pl = ((TLeaf *)m_Levels[m_N]) + st.getIdx(); return TState(pl->bol(), pl->bon()); } else { TNode* pn = ((TNode *)m_Levels[st.getLevel()]) + st.getIdx(); if (pn->ch() == (pn+1)->ch()) return TState(pn->bol(), pn->bon()); else return st; } }
CThreadSlm::TState& CThreadSlm::historify(TState& st) { if (st.getLevel() >= m_N) { TLeaf* pl = ((TLeaf *)m_Levels[m_N]) + st.getIdx(); st.setLevel(pl->bol()); st.setIdx(pl->bon()); } else { TNode* pn = ((TNode *)m_Levels[st.getLevel()]) + st.getIdx(); if (pn->ch() == (pn+1)->ch()) { st.setLevel(pn->bol()); st.setIdx(pn->bon()); } } return st; }