1 #ifndef __WORDNODE_H__ // -*- tab-width: 2 mode: c++ -*-
8 #ifndef __DICTIONARY_H__
9 #include "dictionary.h"
19 #ifndef BOOST_SHARED_PTR_HPP_INCLUDED
20 #include <boost/shared_ptr.hpp>
23 #ifndef __MYSTRING_H__
30 typedef boost::shared_ptr
<NNode
> NNodeRef
;
35 virtual bool is_leaf() const = 0;
39 class BranchNNode
:public NNode
42 typedef std::map
<strid
,NNodeRef
> node_map
;
43 typedef std::pair
<node_map::const_iterator
,node_map::const_iterator
> const_np_range
;
44 typedef std::pair
<node_map::iterator
,node_map::iterator
> np_range
;
48 virtual ~BranchNNode() {}
49 bool is_leaf() const { return false; }
50 LeafNNode
* get_leaf(strid leaf
) const;
51 void get_leaves(std::vector
<LeafNNode
*> &nodes
) const;
52 void get_branches(strid
,std::vector
<BranchNNode
*> &nodes
) const;
53 BranchNNode
* get_branch(strid
) const;
54 const node_map
& get_nodes() const { return nodes
; }
56 BranchNNode
* add_path(const std::vector
<strid
> &toks
);
57 void add(strid
,NNodeRef
);
60 class LeafNNode
:public NNode
64 std::vector
<strid
> syllables
;
66 static std::map
<strid
,LeafNNode
*> leaf_index
;
68 LeafNNode():bitmask(0) {}
69 virtual ~LeafNNode() { leaf_index
.erase(id
); }
70 bool is_leaf() const { return true; }
71 strid
get_id() const { return id
; }
72 void set_id(const std::vector
<strid
> &sy
);
73 static LeafNNode
* find_leaf(const std::vector
<strid
> &sy
);
75 uint
get_mask() const { return bitmask
; }
76 void set_mask(uint maskval
,bool mask
= true);
77 bool filter(uint mask
) const { return (bitmask
& mask
) == mask
; }
79 uint
get_syllable_count() const { return syllables
.size(); }
80 void get_syllables(std::vector
<strid
> &_syllables
) const { _syllables
= syllables
; }
82 friend std::ostream
& operator << (std::ostream
&os
,const LeafNNode
&node
);
83 friend std::istream
& operator >> (std::istream
&is
,LeafNNode
* &node
);
90 DNNode(T
* _node
= NULL
):node(_node
),distance(0) {}
91 int operator == (const DNNode
&dn1
) const {
92 return dn1
.node
== node
;
94 int operator < (const DNNode
&dn1
) const {
95 return (int)node
+distance
< (int)dn1
.node
+dn1
.distance
;
97 T
& operator* () const { return *node
; }
98 T
* operator-> () const { return node
; }
105 void add_entry(const char *);
106 void add_case_entry(const char *);
107 std::vector
<strid
> leaf_id
;
110 WordArchive():root(new BranchNNode
) {}
111 void init(); // called after LM is initialized
112 BranchNNode
* get_root() { return (BranchNNode
*)root
.get(); }
113 bool load(const char *filename
);
114 LeafNNode
* add_special_entry(strid
);
115 LeafNNode
* find_leaf(const std::vector
<strid
> &syllables
,uint bitmask
);
116 void register_leaf(strid leaf
);
117 const std::vector
<strid
>& get_leaf_id() const { return leaf_id
; }
119 LeafNNode
* get_special_node(int);
121 extern WordArchive warch
;
124 std::ostream
& operator << (std::ostream
&os
,const DNNode
<T
> &node
)
126 os
<< node
.distance
<< " " << *node
.node
;
131 std::istream
& operator >> (std::istream
&is
,DNNode
<T
> &node
)
133 is
>> node
.distance
>> node
.node
;