softcount: tolerate zero ngrams
[vspell.git] / libvspell / dag.h
blobd3218e97be60d47a2215c9fd01ff1733240bf97d
1 #ifndef __DAG_H__ // -*- tab-width: 2 mode: c++ -*-
2 #define __DAG_H__
4 #ifndef __VECTOR__
5 #include <vector>
6 #endif
8 #ifndef __SPELL_H__
9 #include "spell.h"
10 #endif
12 typedef std::vector<uint> Path;
14 class DAG {
15 public:
16 virtual VocabIndex node_id(uint id) const = 0;
17 virtual uint node_begin() const = 0;
18 virtual uint node_end() const = 0;
19 virtual uint node_count() const = 0;
20 virtual const void* node_info(uint node_id) const = 0;
21 virtual void get_next(uint node_id,std::vector<uint> &next_id) const = 0;
22 /// the return value is undefined if there is no edge between node_from and node_to
23 virtual float edge_value(uint node_from,uint node_to) const = 0;
24 virtual bool fill_vi(uint node_from,uint node_to,VocabIndex &v,VocabIndex *vi,int size) const = 0;
27 class WordDAG : public DAG {
28 protected:
29 Lattice *lattice;
31 public:
32 WordDAG(Lattice *l);
33 virtual uint node_begin() const {
34 return (*lattice->we).size();
36 virtual uint node_end() const {
37 return (*lattice->we).size()+1;
39 virtual uint node_count() const {
40 return (*lattice->we).size()+2;
42 virtual VocabIndex node_id(uint id) const;
43 virtual const void* node_info(uint node_id) const;
44 virtual void get_next(uint node_id,std::vector<uint> &next_id) const;
45 virtual float edge_value(uint node_from,uint node_to) const;
46 virtual bool fill_vi(uint node_from,uint node_to,VocabIndex &v,VocabIndex *vi,int size) const;
49 class WordDAG2 : public DAG {
50 protected:
51 WordDAG *dag;
52 struct NNode {
53 uint n1,n2; // refer to node_id in dag
54 uint id; // refer to nodes
55 bool operator < (const NNode &n) const {
56 return n1 != n.n1 ? n1 < n.n1 : n2 < n.n2;
58 static bool node_cmp (const NNode &n1,const NNode &n2) {
59 return n1.n1 < n2.n1;
63 typedef std::vector<NNode> NNodes;
64 NNodes nodes;
66 public:
67 WordDAG2(WordDAG *dag_);
68 WordDAG* get_dag() const { return dag; }
69 void node_dag_edge(uint node_id,uint &n1,uint &n2);
70 virtual uint node_begin() const { return nodes.size(); }
71 virtual uint node_end() const { return nodes.size()+1; }
72 virtual uint node_count() const { return nodes.size()+2; }
73 virtual void demangle(std::vector<uint> &next_id) const;
74 virtual VocabIndex node_id(uint id) const;
75 virtual const void* node_info(uint node_id) const;
76 virtual void get_next(uint node_id,std::vector<uint> &next_id) const;
77 virtual float edge_value(uint node_from,uint node_to) const;
78 virtual bool fill_vi(uint node_from,uint node_to,VocabIndex &v,VocabIndex *vi,int size) const;
81 std::ostream& operator << (std::ostream& os, const DAG &dag);
83 #endif