source: src/dgp/dgp0.cc

Last change on this file was e7de6cc, checked in by Tomasz Obrebski <to@…>, 13 years ago

new version of dgp
added dgc, tre and compdic components
compiledic renamed to compdic_utf8
./configure updated

  • Property mode set to 100644
File size: 5.6 KB
Line 
1#include "dgp0.hh"
2#include "global.hh"
3
4extern Grammar grammar;
5extern MGraph mgraph;
6extern SGraph sgraph;
7
8extern bool debug;
9
10list<int> nodelist;
11list<int>::iterator processed;
12
13
14void set_initial_constraints(int node)
15{
16  sgraph[node].prop.forbidden.reset();
17  sgraph[node].prop.required=grammar.is_obl(mgraph[sgraph[node].mnode].cat);
18}
19
20
21bool changing_constraints(int head, Role role)
22{
23  return grammar.is_sgl(role) || sgraph[head].prop.required[role];
24}
25
26void apply_constraints(int head, Role role)
27{
28  if(grammar.is_sgl(role)) sgraph[head].prop.forbidden.set(role);
29  sgraph[head].prop.required.reset(role);
30}
31
32NodeProp compute_prop_left(NodeProp headprop, Role role)
33{
34  NodeProp ret=headprop;
35  if(grammar.is_sgl(role)) ret.forbidden.set(role);
36  ret.required.reset(role);
37  return ret;
38}
39
40NodeProp compute_prop_right(NodeProp headprop, Role role)
41{
42  NodeProp ret=headprop;
43
44  if(grammar.is_sgl(role)) ret.forbidden.set(role);
45  ret.required.reset(role);
46  return ret;
47}
48
49int get_node(MNode& mnode, NodeProp p, bitset<MAXNODES>& newheadLH, bitset<MAXNODES>& newheadLV)
50{
51  for(vector<int>::iterator ps=mnode.snodes.begin(); ps!=mnode.snodes.end(); ++ps)
52    if(sgraph[*ps].prop==p && sgraph[*ps].LH==newheadLH && sgraph[*ps].LV==newheadLV)
53      return *ps;
54  return -1;
55}
56
57void connect_left(list<int>::iterator h, list<int>::iterator d, Role r)
58{
59  NodeProp &oldheadprop = sgraph[*h].prop;
60  NodeProp newheadprop  = compute_prop_left(oldheadprop,r);
61 
62  int newheadind;
63  if(oldheadprop==newheadprop)
64    newheadind = *h;
65  else
66  {
67    bitset<MAXNODES> newheadLH = sgraph[*h].LH;
68    bitset<MAXNODES> newheadLV = sgraph[*d].LV;
69    bitset<MAXNODES> newheadLD = sgraph[*h].LD;
70
71    newheadind = get_node(mgraph[sgraph[*h].mnode], newheadprop, newheadLH, newheadLV);
72    if( newheadind < 0 )
73    {
74      newheadind = sgraph.clone(*h,newheadprop);
75      list<int>::iterator nextit=h; ++nextit;
76      nodelist.insert(nextit,newheadind);
77      sgraph[newheadind].LH=newheadLH;
78      sgraph[newheadind].LD = newheadLD;
79      sgraph[newheadind].in_LH=true;
80      sgraph[newheadind].LV.reset();
81     
82      if(debug) sgraph.print_node_debug(stderr," C ",newheadind);
83    }
84    else
85      sgraph[newheadind].LD |= newheadLD; // TYLKO DLA LD
86  }
87
88  sgraph[newheadind].deps.push_back(Arc(*d,r,*h));
89 
90  if(sgraph[*d].saturated()) sgraph[newheadind].LV |= sgraph[*d].LV;
91
92  sgraph[newheadind].LD.set(*d);
93  if(sgraph[*d].saturated()) sgraph[newheadind].LD |= sgraph[*d].LD;
94 
95  if(debug) sgraph.print_arc(stderr,*d,newheadind,r,0), sgraph.print_node_debug(stderr," U ",newheadind);
96}
97
98void connect_right(list<int>::iterator h, list<int>::iterator d, Role r)
99{
100  NodeProp &oldheadprop = sgraph[*h].prop;
101  NodeProp newheadprop = compute_prop_right(oldheadprop,r);
102  int newheadind;
103 
104  if(oldheadprop==newheadprop)
105    newheadind = *h;
106  else
107  {
108    bitset<MAXNODES> newheadLH = sgraph[*h].LH;
109    bitset<MAXNODES> newheadLV = sgraph[*h].LV;
110    bitset<MAXNODES> newheadLD = sgraph[*h].LD;
111
112    newheadind = get_node(mgraph[sgraph[*h].mnode], newheadprop, newheadLH, newheadLV);
113    if( newheadind < 0 )
114    {
115      newheadind = sgraph.clone(*h,newheadprop);
116      list<int>::iterator nextit=h; ++nextit;
117      nodelist.insert(nextit,newheadind);
118      sgraph[newheadind].LH=newheadLH;
119      sgraph[newheadind].LD=newheadLD;
120      sgraph[newheadind].in_LH=false;
121      sgraph[newheadind].LV=newheadLV;
122     
123      if(debug) sgraph.print_node_debug(stderr," C ",newheadind);
124    }
125    else
126      sgraph[newheadind].LD |= newheadLD; // TYLKO DLA LD
127  }
128 
129  sgraph[*d].heads.push_back(Arc(newheadind,r,*h));
130
131  sgraph[*d].LH.set(newheadind);
132
133  if(sgraph[newheadind].saturated()) sgraph[*d].LH |= sgraph[newheadind].LH;
134
135  if(debug) sgraph.print_arc(stderr,newheadind,*d,r,1), sgraph.print_node_debug(stderr," U ",*d);
136 
137}
138
139
140void try_connect_dependents(list<int>::iterator j)
141{
142  for(list<int>::iterator i(j); i!=nodelist.begin(); --i)
143    if(sgraph.visible(*i,*j) && sgraph.saturated(*i))
144    {
145      Roles& ji_roles = grammar.connectable( mgraph[sgraph[*j].mnode].cat, mgraph[sgraph[*i].mnode].cat );
146      for(RolesIter r=ji_roles.begin(); r!=ji_roles.end();++r)
147        if(grammar.check_constr(sgraph[*j].prop,sgraph[*i].prop,0,*r))
148          connect_left(j,i,*r);
149    }
150}
151
152
153void try_connect_heads(list<int>::iterator j)
154{
155  for(list<int>::iterator i(j); i!=nodelist.begin(); --i)
156    if(sgraph.visible(*i,*j))
157    {
158      Roles& ij_roles = grammar.connectable( mgraph[sgraph[*i].mnode].cat, mgraph[sgraph[*j].mnode].cat );
159      for(RolesIter r=ij_roles.begin(); r!=ij_roles.end();++r)
160        if(grammar.check_constr(sgraph[*i].prop,sgraph[*j].prop,1,*r))
161          connect_right(i,j,*r);
162    }
163}
164
165
166void reverse_links()
167{
168  list<int>::iterator i = nodelist.begin();
169  for(++i; i!=nodelist.end(); ++i)
170    {
171      for(vector<Arc>::iterator da=sgraph[*i].deps.begin()--; da!=sgraph[*i].deps.end(); ++da)
172        sgraph[da->dst].heads.push_back(Arc(*i,da->role,da->anc));
173      for(vector<Arc>::iterator ha=sgraph[*i].heads.begin(); ha!=sgraph[*i].heads.end(); ++ha)
174        sgraph[ha->dst].deps.push_back(Arc(*i,ha->role,ha->anc));
175    }
176}
177
178
179void dgp0()
180{
181
182  nodelist.clear();
183  nodelist.push_back(0); // BOS
184  processed=nodelist.begin();
185
186  for(int m=0; m<mgraph.size() ; ++m)
187  {
188    int basenode = sgraph.add_base_snode(m); // ma zwracaæ SNode*
189
190    set_initial_constraints(basenode);
191    nodelist.push_back(basenode);
192
193    if(debug) {sgraph.print_node_debug(stderr,"B  ",basenode);} // STDOUT!!!
194
195    list<int>::iterator cursor=processed;
196    while(++cursor != nodelist.end())
197    {
198      try_connect_dependents(cursor);
199      try_connect_heads(cursor);
200      processed=cursor;
201    }
202
203  }
204  reverse_links();
205}
Note: See TracBrowser for help on using the repository browser.