5 void BranchNode::get_leaves(std::vector
<LeafNode
*> &_nodes
) const
8 range
= nodes
.equal_range(sarch
["<leaf>"]);
9 node_map::const_iterator iter
;
10 for (iter
= range
.first
;iter
!= range
.second
; ++iter
)
11 if (iter
->second
->is_leaf())
12 _nodes
.push_back((LeafNode
*)iter
->second
.get());
15 void BranchNode::get_branches(strid _id
,std::vector
<BranchNode
*> &_nodes
) const
18 range
= nodes
.equal_range(_id
);
19 node_map::const_iterator iter
;
20 for (iter
= range
.first
;iter
!= range
.second
; ++iter
)
21 if (!iter
->second
->is_leaf())
22 _nodes
.push_back((BranchNode
*)iter
->second
.get());
25 BranchNode
* BranchNode::get_branch(strid _id
) const
28 range
= nodes
.equal_range(_id
);
29 node_map::const_iterator iter
;
30 for (iter
= range
.first
;iter
!= range
.second
; ++iter
)
31 if (!iter
->second
->is_leaf())
32 return (BranchNode
*)iter
->second
.get();
36 void BranchNode::add(strid _id
,NodeRef _branch
)
38 nodes
.insert(make_pair(_id
,_branch
));
41 BranchNode
* BranchNode::add_path(std::vector
<strid
> toks
)
43 uint i
,n
= toks
.size();
44 BranchNode
*me
= this;
45 for (i
= 0;i
< n
;i
++) {
46 BranchNode
*next
= me
->get_branch(toks
[i
]);
48 NodeRef
branch(new BranchNode());
49 me
->add(toks
[i
],branch
);
50 next
= (BranchNode
*)branch
.get();
57 bool WordArchive::load(const char* filename
)
59 File
ifs(filename
,"rt");
66 int start
,len
,tmp_char
;
67 string::size_type pos
;
70 while ((line
= ifs
.getline())) {
75 str_pos
= strchr(line
+start
,'#');
82 toks
.push_back(line
+start
);
88 continue; // unrecoverable error
90 toks
.push_back("N"); // assume N
92 toks
.push_back("0"); // assume 0
102 void WordArchive::add_special_entry(strid tok
)
104 NodeRef
noderef(new LeafNode
);
105 LeafNode
*leaf
= (LeafNode
*)noderef
.get();
107 get_root()->add(sarch
["<leaf>"],noderef
);
110 void WordArchive::add_entry(vector
<string
> toks
)
112 unsigned start
,len
,pos
;
114 len
= toks
[0].size();
115 vector
<VocabIndex
> syllables
;
116 while (start
< len
) {
117 pos
= toks
[0].find(' ',start
);
118 if (pos
== string::npos
)
120 string s
= toks
[0].substr(start
,pos
-start
);
121 VocabIndex id
= sarch
[get_dic_syllable(s
)];
122 syllables
.push_back(id
);
126 vector
<strid
> path
= syllables
;
127 BranchNode
* branch
= get_root()->add_path(path
);
128 NodeRef
noderef(new LeafNode
);
129 LeafNode
*leaf
= (LeafNode
*)noderef
.get();
130 branch
->add("<leaf>",noderef
);
132 // reconstruct word id
134 int i
,nr_syllables
= syllables
.size();
135 for (i
= 0;i
< nr_syllables
;i
++) {
138 word
+= sarch
[syllables
[i
]];
140 leaf
->set_id(sarch
[word
]);