7 #include "pinyin_data.h"
14 CPinyinTrie::isValid(const TNode* pnode,
15 bool allowNonComplete,
18 if ((pnode != NULL) && (csLevel <= pnode->m_csLevel))
19 return(allowNonComplete || (pnode->m_bFullSyllableTransfer == 1));
24 CPinyinTrie::lengthAt(unsigned int idx) const
26 if (idx < getWordCount() - 1) {
27 return (m_words[idx + 1] - m_words[idx]) - 1;
28 } else if (idx == getWordCount() - 1) {
29 return (((TWCHAR*)(m_mem + m_Size)) - m_words[idx]) - 1;
35 CPinyinTrie::getSymbolId(const TWCHAR* wstr)
37 std::map<wstring, unsigned>::const_iterator it;
39 it = m_SymbolMap.find(wstring(wstr));
40 if (it != m_SymbolMap.end())
46 CPinyinTrie::getSymbolId(const wstring & wstr)
48 std::map<wstring, unsigned>::const_iterator it;
50 it = m_SymbolMap.find(wstr);
51 if (it != m_SymbolMap.end())
57 CPinyinTrie::free(void)
60 #ifdef HAVE_SYS_MMAN_H
61 munmap(m_mem, m_Size);
75 CPinyinTrie::load(const char *fname)
80 int fd = open(fname, O_RDONLY);
81 if (fd == -1) return false;
83 m_Size = lseek(fd, 0, SEEK_END);
84 lseek(fd, 0, SEEK_SET);
86 #ifdef HAVE_SYS_MMAN_H
89 (char*)mmap(NULL, m_Size, PROT_READ, MAP_SHARED, fd,
92 suc = (m_mem = new char [m_Size]) != NULL;
93 suc = suc && (read(fd, m_mem, m_Size) > 0);
97 suc = suc && ((m_words = new TWCHAR*[getWordCount()]) != NULL);
100 TWCHAR *p = (TWCHAR*)(m_mem + getStringOffset());
101 for (int i = 0, sz = getWordCount(); i < sz; ++i) {
106 for (unsigned i = 1; i < 100; ++i) {
107 if (*m_words[i] != WCH_NULL && *m_words[i] != WCH_LESSTHAN)
108 m_SymbolMap[wstring(m_words[i])] = i;
115 CPinyinTrie::print(FILE *fp) const
118 print(getRootNode(), prefix, fp);
122 CPinyinTrie::print(const TNode* pRoot, std::string& prefix, FILE *fp) const
124 static char buf[1024];
125 if (pRoot->m_nWordId > 0) {
126 fprintf(fp, "%s", prefix.c_str());
127 if (pRoot->m_csLevel)
128 fprintf(fp, "(GBK+)");
129 unsigned int sz = pRoot->m_nWordId;
130 const TWordIdInfo *pwids = pRoot->getWordIdPtr();
131 for (unsigned int i = 0; i < sz; ++i) {
132 unsigned int id = pwids[i].m_id;
133 const TWCHAR *pw = operator[](id);
134 int len = WCSLEN(pw);
135 if (len != lengthAt(id)) {
136 printf(" (lengthAt %d error) ", id);
138 WCSTOMBS(buf, pw, 1024);
139 fprintf(fp, " %s", buf);
140 if (pwids[i].m_bSeen == 0)
145 fprintf(fp, "(%d)", pwids[i].m_cost);
149 unsigned int sz = pRoot->m_nTransfer;
150 const TTransUnit* ptrans = pRoot->getTrans();
151 for (unsigned int i = 0; i < sz; ++i) {
152 unsigned s = ptrans[i].m_Syllable;
153 const TNode *pch = transfer(pRoot, s);
154 const char *str = CPinyinData::decodeSyllable(s);
156 prefix = prefix + str + '\'';
157 print(pch, prefix, fp);
158 prefix.resize(prefix.size() - strlen(str) - 1);