bool _calcDP(vector<SegmentChar>& SegmentChars)const
            {
                if(SegmentChars.empty())
                {
                    LogError("SegmentChars empty");
                    return false;
                }

                size_t nextPos;
                const DictUnit* p;
                double val;

                for(int i = SegmentChars.size() - 1; i >= 0; i--)
                {
                    SegmentChars[i].pInfo = NULL;
                    SegmentChars[i].weight = MIN_DOUBLE;
                    for(DagType::const_iterator it = SegmentChars[i].dag.begin(); it != SegmentChars[i].dag.end(); it++)
                    {
                        nextPos = it->first;
                        p = it->second;
                        val = 0.0;
                        if(nextPos + 1 < SegmentChars.size())
                        {
                            val += SegmentChars[nextPos + 1].weight;
                        }

                        if(p)
                        {
                            val += p->weight; 
                        }
                        else
                        {
                            val += _dictTrie.getMinWeight();
                        }
                        if(val > SegmentChars[i].weight)
                        {
                            SegmentChars[i].pInfo = p;
                            SegmentChars[i].weight = val;
                        }
                    }
                }
                return true;

            }
Exemple #2
0
TEST(DictTrieTest, Test1) {

    string s1, s2;
    DictTrie trie;
    trie.init(DICT_FILE);
    ASSERT_LT(trie.getMinWeight() + 15.6479, 0.001);
    string word("来到");
    Unicode uni;
    ASSERT_TRUE(TransCode::decode(word, uni));
    DictUnit nodeInfo;
    nodeInfo.word = uni;
    nodeInfo.tag = "v";
    nodeInfo.weight = -8.87033;
    s1 << nodeInfo;
    s2 << (*trie.find(uni.begin(), uni.end()));

    EXPECT_EQ("[\"26469\", \"21040\"] v -8.870", s2);
    word = "清华大学";
    LocalVector<pair<size_t, const DictUnit*> > res;
    //vector<pair<size_t, const DictUnit* > resMap;
    LocalVector<pair<size_t, const DictUnit*> > res2;
    const char * words[] = {"清", "清华", "清华大学"};
    for(size_t i = 0; i < sizeof(words)/sizeof(words[0]); i++) {
        ASSERT_TRUE(TransCode::decode(words[i], uni));
        res.push_back(make_pair(uni.size() - 1, trie.find(uni.begin(), uni.end())));
        //resMap[uni.size() - 1] = trie.find(uni.begin(), uni.end());
    }
    //DictUnit
    //res.push_back(make_pair(0, ))

    vector<pair<size_t, const DictUnit*> > vec;
    ASSERT_TRUE(TransCode::decode(word, uni));
    ASSERT_TRUE(trie.find(uni.begin(), uni.end(), res2, 0));
    s1 << res;
    s2 << res;
    ASSERT_EQ(s1, s2);
}