source: src/dgp/dgp0.cc @ 18e1952

Last change on this file since 18e1952 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
RevLine 
[5f4d9c3]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{
[e7de6cc]16  sgraph[node].prop.forbidden.reset();
17  sgraph[node].prop.required=grammar.is_obl(mgraph[sgraph[node].mnode].cat);
[5f4d9c3]18}
19
20
21bool changing_constraints(int head, Role role)
22{
[e7de6cc]23  return grammar.is_sgl(role) || sgraph[head].prop.required[role];
[5f4d9c3]24}
25
26void apply_constraints(int head, Role role)
27{
[e7de6cc]28  if(grammar.is_sgl(role)) sgraph[head].prop.forbidden.set(role);
29  sgraph[head].prop.required.reset(role);
[5f4d9c3]30}
31
32NodeProp compute_prop_left(NodeProp headprop, Role role)
33{
34  NodeProp ret=headprop;
[e7de6cc]35  if(grammar.is_sgl(role)) ret.forbidden.set(role);
[5f4d9c3]36  ret.required.reset(role);
37  return ret;
38}
39
40NodeProp compute_prop_right(NodeProp headprop, Role role)
41{
42  NodeProp ret=headprop;
43
[e7de6cc]44  if(grammar.is_sgl(role)) ret.forbidden.set(role);
[5f4d9c3]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)
[e7de6cc]52    if(sgraph[*ps].prop==p && sgraph[*ps].LH==newheadLH && sgraph[*ps].LV==newheadLV)
[5f4d9c3]53      return *ps;
54  return -1;
55}
56
57void connect_left(list<int>::iterator h, list<int>::iterator d, Role r)
58{
[e7de6cc]59  NodeProp &oldheadprop = sgraph[*h].prop;
60  NodeProp newheadprop  = compute_prop_left(oldheadprop,r);
[5f4d9c3]61 
62  int newheadind;
63  if(oldheadprop==newheadprop)
64    newheadind = *h;
65  else
66  {
[e7de6cc]67    bitset<MAXNODES> newheadLH = sgraph[*h].LH;
68    bitset<MAXNODES> newheadLV = sgraph[*d].LV;
69    bitset<MAXNODES> newheadLD = sgraph[*h].LD;
[5f4d9c3]70
[e7de6cc]71    newheadind = get_node(mgraph[sgraph[*h].mnode], newheadprop, newheadLH, newheadLV);
[5f4d9c3]72    if( newheadind < 0 )
73    {
74      newheadind = sgraph.clone(*h,newheadprop);
75      list<int>::iterator nextit=h; ++nextit;
76      nodelist.insert(nextit,newheadind);
[e7de6cc]77      sgraph[newheadind].LH=newheadLH;
78      sgraph[newheadind].LD = newheadLD;
79      sgraph[newheadind].in_LH=true;
80      sgraph[newheadind].LV.reset();
[5f4d9c3]81     
82      if(debug) sgraph.print_node_debug(stderr," C ",newheadind);
83    }
84    else
[e7de6cc]85      sgraph[newheadind].LD |= newheadLD; // TYLKO DLA LD
[5f4d9c3]86  }
87
[e7de6cc]88  sgraph[newheadind].deps.push_back(Arc(*d,r,*h));
[5f4d9c3]89 
[e7de6cc]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;
[5f4d9c3]94 
[e7de6cc]95  if(debug) sgraph.print_arc(stderr,*d,newheadind,r,0), sgraph.print_node_debug(stderr," U ",newheadind);
[5f4d9c3]96}
97
98void connect_right(list<int>::iterator h, list<int>::iterator d, Role r)
99{
[e7de6cc]100  NodeProp &oldheadprop = sgraph[*h].prop;
101  NodeProp newheadprop = compute_prop_right(oldheadprop,r);
[5f4d9c3]102  int newheadind;
103 
104  if(oldheadprop==newheadprop)
105    newheadind = *h;
106  else
107  {
[e7de6cc]108    bitset<MAXNODES> newheadLH = sgraph[*h].LH;
109    bitset<MAXNODES> newheadLV = sgraph[*h].LV;
110    bitset<MAXNODES> newheadLD = sgraph[*h].LD;
[5f4d9c3]111
[e7de6cc]112    newheadind = get_node(mgraph[sgraph[*h].mnode], newheadprop, newheadLH, newheadLV);
[5f4d9c3]113    if( newheadind < 0 )
114    {
115      newheadind = sgraph.clone(*h,newheadprop);
116      list<int>::iterator nextit=h; ++nextit;
117      nodelist.insert(nextit,newheadind);
[e7de6cc]118      sgraph[newheadind].LH=newheadLH;
119      sgraph[newheadind].LD=newheadLD;
120      sgraph[newheadind].in_LH=false;
121      sgraph[newheadind].LV=newheadLV;
[5f4d9c3]122     
123      if(debug) sgraph.print_node_debug(stderr," C ",newheadind);
124    }
125    else
[e7de6cc]126      sgraph[newheadind].LD |= newheadLD; // TYLKO DLA LD
[5f4d9c3]127  }
128 
[e7de6cc]129  sgraph[*d].heads.push_back(Arc(newheadind,r,*h));
[5f4d9c3]130
[e7de6cc]131  sgraph[*d].LH.set(newheadind);
[5f4d9c3]132
[e7de6cc]133  if(sgraph[newheadind].saturated()) sgraph[*d].LH |= sgraph[newheadind].LH;
[5f4d9c3]134
[e7de6cc]135  if(debug) sgraph.print_arc(stderr,newheadind,*d,r,1), sgraph.print_node_debug(stderr," U ",*d);
[5f4d9c3]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    {
[e7de6cc]145      Roles& ji_roles = grammar.connectable( mgraph[sgraph[*j].mnode].cat, mgraph[sgraph[*i].mnode].cat );
[5f4d9c3]146      for(RolesIter r=ji_roles.begin(); r!=ji_roles.end();++r)
[e7de6cc]147        if(grammar.check_constr(sgraph[*j].prop,sgraph[*i].prop,0,*r))
[5f4d9c3]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    {
[e7de6cc]158      Roles& ij_roles = grammar.connectable( mgraph[sgraph[*i].mnode].cat, mgraph[sgraph[*j].mnode].cat );
[5f4d9c3]159      for(RolesIter r=ij_roles.begin(); r!=ij_roles.end();++r)
[e7de6cc]160        if(grammar.check_constr(sgraph[*i].prop,sgraph[*j].prop,1,*r))
[5f4d9c3]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    {
[e7de6cc]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));
[5f4d9c3]175    }
176}
177
178
179void dgp0()
180{
181
182  nodelist.clear();
183  nodelist.push_back(0); // BOS
184  processed=nodelist.begin();
185
[e7de6cc]186  for(int m=0; m<mgraph.size() ; ++m)
[5f4d9c3]187  {
[e7de6cc]188    int basenode = sgraph.add_base_snode(m); // ma zwracaæ SNode*
189
[5f4d9c3]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    }
[e7de6cc]202
[5f4d9c3]203  }
204  reverse_links();
205}
Note: See TracBrowser for help on using the repository browser.