source: app/src/dgp/grammar.cc @ 0214596

help
Last change on this file since 0214596 was 0214596, checked in by pawelk <pawelk@…>, 16 years ago

Dodalismy do pakietu utt komponent dgp (brak configow i innych bajerow).

git-svn-id: svn://atos.wmid.amu.edu.pl/utt@24 e293616e-ec6a-49c2-aa92-f4a8b91c5d16

  • Property mode set to 100644
File size: 3.4 KB
Line 
1
2#include <stdio.h>
3
4#include "grammar.hh"
5
6bool (*constraint[MAXCONSTRS])(int head, int dep);
7
8
9int chk_type(const char* s, int lineno) // SIDE EFECTS!
10{
11  if(Role::index(s)>0) return 1;
12
13  fprintf(stderr,"%8d: Invalid type '%s'. Line ignored.\n",lineno,s);
14  return 0;
15}
16
17int chk_cat(const char* s, int lineno)
18{
19  if(Cat::index(s)>0) return 1;
20
21  fprintf(stderr,"%8d: Invalid category '%s'. Line ignored.\n",lineno,s);
22  return 0;
23}
24
25void Grammar::add_category(const char* s)
26{
27  Cat::add(s);
28  if(Cat::count()>cats_sz)
29  {
30    cats_sz += 16;
31    connect.resize(cats_sz);
32    for(int i=0; i<cats_sz; ++i)
33      connect[i].resize(cats_sz);
34    obl.resize(cats_sz);
35  }
36}
37
38void Grammar::add_type(const char* s)
39{ 
40  Role::add(s);
41  if(Role::count()>types_sz)
42  {
43    types_sz += 16;
44    lt.resize(types_sz);
45    gt.resize(types_sz);
46  }
47}
48
49
50void Grammar::set_lt(Role s, Role t)
51{
52  lt[s].set(t);
53  gt[t].set(s);
54  if(s==0||(int)t==0)
55    return;
56  else
57  {
58    for(int i=0; i<Role::count(); ++i)
59      if(lt[i][s])
60        set_lt(i,t);
61    for(int i=0; i<Role::count(); ++i)
62      if(lt[t][i])
63        set_lt(s,i);
64  }
65} 
66
67
68void Grammar::compute_gt()
69{
70  for(Role s=0; s<Role::count(); ++s)
71    for(Role t=0; t<Role::count(); ++t)
72      if(lt[s][t])
73        gt[t].set(s);
74}
75
76
77bool Grammar::read(FILE* f)
78{
79  int lineno=0;
80  char line[MAXLINE]; // line has the structure: key [arg1 [arg2 [arg3]]]
81  char key[MAXLINE];
82  char arg1[MAXLINE];
83  char arg2[MAXLINE];
84  char arg3[MAXLINE];
85
86  while(fgets(line,MAXLINE,f))
87  {
88    lineno++;
89    int fields=sscanf(line,"%s %s %s %s",key,arg1,arg2,arg3);
90
91    if(fields<1 || key[0]=='#') continue; // skip empty lines and comments
92
93    if     (strcmp(key,"CAT")==0 && fields>=2)
94    {
95      add_category(arg1);
96    }
97    else if(strcmp(key,"ROLE")==0 && fields>=2)
98    {
99      add_type(arg1);
100    }
101    else if(strcmp(key,"SGL")==0 && fields>=2)
102    { 
103      if(chk_type(arg1,lineno))
104        set_sgl(arg1);
105    }
106    else if(strcmp(key,"LEFT")==0 && fields>=2)
107    { 
108      if(chk_type(arg1,lineno))
109        set_left(arg1);
110    }
111    else if(strcmp(key,"RIGHT")==0 && fields>=2)
112    {
113      if(chk_type(arg1,lineno))
114        set_right(arg1);
115    }
116    else if(strcmp(key,"REQ")==0 && fields>=3)
117    {
118      if(chk_cat(arg1,lineno) + chk_type(arg2,lineno) == 2)
119        set_obl(arg1,arg2);
120    }
121    else if(strcmp(key,"LINK")==0 && fields>=4)
122    { 
123      if(chk_cat(arg1,lineno) + chk_cat(arg2,lineno) + chk_type(arg3,lineno) == 3)   
124        set_connect(arg1,arg2,arg3);
125    }
126
127    else fprintf(stderr,"Invalid line %d. Ignored.\n", lineno);
128  }
129
130//   compute_gt();
131
132  return true;
133 
134}
135
136void Grammar::write(FILE* f)
137{
138  for(Cat i=1; i<Cat::count(); ++i)
139    fprintf(f,"CAT\t%s\n",i.str());
140
141  for(Role i=1; i<Role::count(); ++i)
142    fprintf(f,"ROLE\t%s\n",i.str());
143
144  for(Role i=1; i<Role::count(); ++i)
145    if(sgl.test(i)) fprintf(f,"SGL\t%s\n",i.str());
146 
147  for(Role i=1; i<Role::count(); ++i)
148    if(left.test(i)) fprintf(f,"LEFT\t%s\n",i.str());
149
150  for(Role i=1; i<Role::count(); ++i)
151    if(right.test(i)) fprintf(f,"RIGHT\t%s\n",i.str());
152
153  for(Cat c=1; c<Cat::count(); ++c)
154    for(Role r=1; r<Role::count(); ++r)
155      if(obl[c].test(r)) fprintf(f,"REQ\t%s\t%s\n",c.str(),r.str());
156 
157  for(Cat c=1; c<Cat::count(); ++c)
158    for(Cat d=1; d<Cat::count(); ++d)
159      for(Role t=1; t<Role::count(); ++t)
160        if(connect[c][d].count(t))
161          fprintf(f,"LINK\t%s\t%s\t%s\n",c.str(),d.str(),t.str());
162}
163
Note: See TracBrowser for help on using the repository browser.