00001
00002 #include <sstream>
00003
00004 #include "ug_deptree.h"
00005 #include "tpt_tokenindex.h"
00006
00007 using namespace std;
00008 namespace sapt
00009 {
00010
00011 bool
00012 Conll_Record::
00013 isDescendentOf(Conll_Record const* other) const
00014 {
00015 Conll_Record const* a = this;
00016 while (a != other && a->parent)
00017 a += a->parent;
00018 return a==other;
00019 }
00020
00021 Conll_Record&
00022 Conll_Record::
00023 operator=(Conll_Record const& o)
00024 {
00025 sform = o.sform;
00026 lemma = o.lemma;
00027 majpos = o.majpos;
00028 minpos = o.minpos;
00029 parent = o.parent;
00030 dtype = o.dtype;
00031 info[0] = o.info[0];
00032 info[1] = o.info[1];
00033 info[2] = o.info[2];
00034 return *this;
00035 }
00036
00037 Conll_Record::
00038 Conll_Record()
00039 : sform(0),lemma(0),majpos(0),minpos(0),parent(0),dtype(0)
00040 {
00041 info[0]=0;
00042 info[1]=0;
00043 info[2]=0;
00044 }
00045
00046 Conll_AllFields::
00047 Conll_AllFields()
00048 : Conll_Record::Conll_Record()
00049 {};
00050
00051 int
00052 Conll_AllFields::
00053 cmp(Conll_Record const& other) const
00054 {
00055 if (sform != other.sform) return sform < other.sform ? -1 : 1;
00056 if (lemma != other.lemma) return lemma < other.lemma ? -1 : 1;
00057 if (majpos != other.majpos) return majpos < other.majpos ? -1 : 1;
00058 if (minpos != other.minpos) return minpos < other.minpos ? -1 : 1;
00059 if (dtype != other.dtype) return dtype < other.dtype ? -1 : 1;
00060 if (info[0] != other.info[0]) return info[0] < other.info[0] ? -1 : 1;
00061 if (info[1] != other.info[1]) return info[1] < other.info[1] ? -1 : 1;
00062 if (info[2] != other.info[2]) return info[2] < other.info[2] ? -1 : 1;
00063 if (parent != other.parent) return parent < other.parent ? -1 : 1;
00064 return 0;
00065 }
00066
00067 Conll_WildCard::
00068 Conll_WildCard()
00069 : Conll_Record::Conll_Record()
00070 {};
00071
00072 int
00073 Conll_WildCard::
00074 cmp(Conll_Record const& other) const
00075 {
00076 return 0;
00077 }
00078
00079 #if 1
00080 bool
00081 Conll_AllFields::
00082 operator==(Conll_AllFields const& other) const
00083 {
00084 return (sform == other.sform
00085 && lemma == other.lemma
00086 && majpos == other.majpos
00087 && minpos == other.minpos
00088 && parent == other.parent
00089 && dtype == other.dtype
00090 && info[0] == other.info[0]
00091 && info[1] == other.info[1]
00092 && info[2] == other.info[2]
00093 );
00094 }
00095 #endif
00096
00097 #if 0
00098 Conll_Record::
00099 Conll_Record(string const& line,
00100 TokenIndex const& SF, TokenIndex const& LM,
00101 TokenIndex const& PS, TokenIndex const& DT)
00102 {
00103
00104 string surf,lem,pos1,pos2,dummy,drel;
00105 short id,gov;
00106 istringstream buf(line);
00107
00108 buf >> id >> surf >> lem >> pos1 >> pos2 >> dummy >> gov >> drel;
00109
00110 sform = SF[surf];
00111 lemma = LM[lem];
00112 if (PS[pos1] > 255 || PS[pos2] > 255 || DT[drel] > 255)
00113 {
00114 cerr << "error at this line:\n" << line << endl;
00115 exit(1);
00116 }
00117 majpos = rangeCheck(PS[pos1],256);
00118 minpos = rangeCheck(PS[pos2],256);
00119 dtype = rangeCheck(DT[drel],256);
00120 parent = gov ? gov-id : 0;
00121 info[0]=info[1]=info[2]=0;
00122 }
00123 void
00124 Conll_Record::
00125 store(ostream& out)
00126 {
00127 out.write(reinterpret_cast<char const*>(this),sizeof(*this));
00128 }
00129 #endif
00130
00131 #if 1
00132 Conll_Record const*
00133 Conll_Record::up(int length) const
00134 {
00135 Conll_Record const* ret = this;
00136 while (length-- > 0)
00137 if (!ret->parent) return NULL;
00138 else ret += ret->parent;
00139 return ret;
00140 }
00141 #endif
00142
00143 Conll_Sform::
00144 Conll_Sform()
00145 : Conll_Record::Conll_Record()
00146 {};
00147
00148 Conll_MinPos::
00149 Conll_MinPos()
00150 : Conll_Record::Conll_Record()
00151 {};
00152
00153 Conll_MinPos_Lemma::
00154 Conll_MinPos_Lemma()
00155 : Conll_Record::Conll_Record()
00156 {};
00157
00158 Conll_Lemma::
00159 Conll_Lemma()
00160 : Conll_Record::Conll_Record()
00161 {};
00162
00163 Conll_Lemma::
00164 Conll_Lemma(id_type _id)
00165 : Conll_Record::Conll_Record()
00166 {
00167 this->lemma = _id;
00168 };
00169
00170 Conll_MinPos::
00171 Conll_MinPos(id_type _id)
00172 : Conll_Record::Conll_Record()
00173 {
00174 this->minpos = _id;
00175 };
00176
00177 id_type
00178 Conll_MinPos::
00179 id() const
00180 {
00181 return this->minpos;
00182 }
00183
00184 Conll_MajPos::
00185 Conll_MajPos(id_type _id)
00186 : Conll_Record::Conll_Record()
00187 {
00188 this->majpos = _id;
00189 };
00190
00191 id_type
00192 Conll_MajPos::
00193 id() const
00194 {
00195 return this->majpos;
00196 }
00197
00198 id_type
00199 Conll_MinPos_Lemma::
00200 id() const
00201 {
00202 return this->minpos;
00203 }
00204
00205 int
00206 Conll_MajPos::
00207 cmp(Conll_Record const& other) const
00208 {
00209 return this->majpos < other.majpos ? -1 : this->majpos > other.majpos ? 1 : 0;
00210 }
00211
00212 int
00213 Conll_MinPos::
00214 cmp(Conll_Record const& other) const
00215 {
00216 return this->minpos < other.minpos ? -1 : this->minpos > other.minpos ? 1 : 0;
00217 }
00218
00219 int
00220 Conll_MinPos_Lemma::
00221 cmp(Conll_Record const& other) const
00222 {
00223 if (this->minpos != 0 && other.minpos != 0 && this->minpos != other.minpos)
00224 return this->minpos < other.minpos ? -1 : 1;
00225 if (this->lemma != 0 && other.lemma != 0 && this->lemma != other.lemma)
00226 return this->lemma < other.lemma ? -1 : 1;
00227 return 0;
00228 }
00229
00230 id_type
00231 Conll_Lemma::
00232 id() const
00233 {
00234 return this->lemma;
00235 }
00236
00237 int
00238 Conll_Lemma::
00239 cmp(Conll_Record const& other) const
00240 {
00241 #if 0
00242 for (Conll_Record const* x = this; x; x = x->parent ? x+x->parent : NULL)
00243 cout << (x!=this?".":"") << x->lemma;
00244 cout << " <=> ";
00245 for (Conll_Record const* x = &other; x; x = x->parent ? x+x->parent : NULL)
00246 cout << (x!=&other?".":"") << x->lemma;
00247 cout << (this->lemma < other.lemma ? -1 : this->lemma > other.lemma ? 1 : 0);
00248 cout << endl;
00249 #endif
00250 return this->lemma < other.lemma ? -1 : this->lemma > other.lemma ? 1 : 0;
00251 }
00252
00253 Conll_Sform::
00254 Conll_Sform(id_type _id)
00255 : Conll_Record::Conll_Record()
00256 {
00257 this->sform = _id;
00258 };
00259
00260 id_type
00261 Conll_Sform
00262 ::id() const
00263 {
00264 return this->sform;
00265 }
00266
00267 int
00268 Conll_Sform::
00269 cmp(Conll_Record const& other) const
00270 {
00271 return this->sform < other.sform ? -1 : this->sform > other.sform ? 1 : 0;
00272 }
00273
00274 #if 0
00275 dpSnt::
00276 dpSnt(Conll_Record const* first, Conll_Record const* last)
00277 {
00278 w.reserve(last-first);
00279 for (Conll_Record const* x = first; x < last; ++x)
00280 w.push_back(DTNode(x));
00281 for (size_t i = 0; i < w.size(); i++)
00282 {
00283 short p = w[i].rec->parent;
00284 if (p != 0)
00285 {
00286 if (p > 0) assert(i+p < w.size());
00287 else assert(i >= size_t(-p));
00288 w[i].parent = &(w[i+p]);
00289 w[i].parent->children.push_back(&(w[i]));
00290 }
00291 }
00292 }
00293 #endif
00294
00297 bool
00298 isCoherent(Conll_Record const* const start, Conll_Record const* const stop)
00299 {
00300 int outOfRange=0;
00301 for (Conll_Record const* x = start; outOfRange <= 1 && x < stop; ++x)
00302 {
00303 Conll_Record const* n = x->up();
00304 if (!n || n < start || n >= stop)
00305 outOfRange++;
00306 }
00307 return outOfRange<=1;
00308 }
00309
00310
00311
00312
00313 void
00314 fixParse(Conll_Record* start, Conll_Record* stop)
00315 {
00316 int len = stop-start;
00317 int i = 0;
00318 for (Conll_Record* x = start; x < stop; ++x,++i)
00319 {
00320 int p = i+x->parent;
00321 if (p < 0 || p >= len) x->parent = 0;
00322 }
00323 }
00324 }