Reworked WordArchive::load to use new wordlist format
[vspell.git] / libvspell / wordnode.h
blob95f415cc5720d3ea4cb9a41ea97b45e30aa6446e
1 #ifndef __WORDNODE_H__ // -*- tab-width: 2 mode: c++ -*-
2 #define __WORDNODE_H__
4 #ifndef __TYPES_H__
5 #include "types.h"
6 #endif
8 #ifndef __DICTIONARY_H__
9 #include "dictionary.h"
10 #endif
11 #ifndef __VECTOR__
12 #include <vector>
13 #endif
15 #ifndef __MAP__
16 #include <map>
17 #endif
19 #ifndef BOOST_SHARED_PTR_HPP_INCLUDED
20 #include <boost/shared_ptr.hpp>
21 #endif
23 #ifndef __MYSTRING_H__
24 #include "mystring.h"
25 #endif
27 class NNode;
28 class BranchNNode;
29 class LeafNNode;
30 typedef boost::shared_ptr<NNode> NNodeRef;
32 class NNode
34 public:
35 virtual bool is_leaf() const = 0;
36 virtual ~NNode() {}
39 class BranchNNode:public NNode
41 public:
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;
45 protected:
46 node_map nodes;
47 public:
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
62 protected:
63 strid id; // word id
64 std::vector<strid> syllables;
65 uint bitmask;
66 static std::map<strid,LeafNNode*> leaf_index;
67 public:
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);
86 template<class T>
87 struct DNNode {
88 T* node;
89 int distance;
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; }
101 class WordArchive
103 protected:
104 NNodeRef root;
105 void add_entry(const char *);
106 void add_case_entry(const char *);
107 std::vector<strid> leaf_id;
109 public:
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;
123 template<class T>
124 std::ostream& operator << (std::ostream &os,const DNNode<T> &node)
126 os << node.distance << " " << *node.node;
127 return os;
130 template<class T>
131 std::istream& operator >> (std::istream &is,DNNode<T> &node)
133 is >> node.distance >> node.node;
134 return is;
137 #endif