source: app/src/lib/word.cc @ ac7d970

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

Uaktualnilismy kora.

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

  • Property mode set to 100644
File size: 5.3 KB
Line 
1//---------------------------------------------------------------------------
2#include "word.h"
3#include "auttools.h"
4#include <istream.h>
5//---------------------------------------------------------------------------
6//---------------------------------------------------------------------------
7
8void Word::autodescr(const char* fo, const char* de)
9{
10  strcpy(f,fo);
11  //  len=strlen(f);
12
13  char lemd[MAXDESCRLEN];
14  int i=strcspn(de,",");
15  strncpy(lemd,de,i);
16  lemd[i]='\0';
17  if(isdigit(lemd[0]))
18    fullform(f,lemd,l);  // je¶li lemat zakodowany
19  else
20    strcpy(l,lemd);     // je¶li lemat w pe³nej postaci
21  strcpy(d,de+i+1);
22}
23
24//---------------------------------------------------------------------------
25bool Word::cmp_w(Word a, Word b) {
26  return (a.w_suf() > b.w_suf());
27}
28//---------------------------------------------------------------------------
29
30istream& operator>>(istream& is, Word& w)
31{
32  char temp[Word::MAXLEN+1];
33  char c;
34
35  int i=0;
36  while(i<Word::MAXLEN && is.get(c) && isalpha(c)) temp[i++]=c;
37  if(i==Word::MAXLEN) {
38    fprintf(stderr, "To long word");
39  }
40  if(i==0) is.clear(ios::badbit);
41  temp[i]='\0';
42  if(is)
43    is.putback(c);
44  strcpy(w.f,temp);
45  //  w.len=i;
46  return is;
47}
48
49//---------------------------------------------------------------------------
50
51ostream& operator<<(ostream& os, Word& w)
52{
53  if(*(w.f))
54    os << "<W " << w.form()
55       << ";" << w.lemma()
56       << ',' << w.descr() << '>';
57  return os;
58}
59
60//---------------------------------------------------------------------------
61Words::~Words() {
62  //   for (int i=0; i<tab.size(); ++i)
63//     delete(tab[i]);
64}
65//---------------------------------------------------------------------------
66
67int Words::find(const char* word) {
68  for (int i=0; i<cnt; ++i) {
69    if (strcmp(word, tab[i].form()) == 0) {
70        return i;
71    }
72  }
73  return -1;
74}
75
76
77//---------------------------------------------------------------------------
78
79int Words::find(const char* word, const char* descr) {
80  for (int i=0; i<cnt; ++i) {
81    if ((strcmp(word, tab[i].form()) == 0) && (strcmp(descr, tab[i].descr()) == 0)) {
82        return i;
83    }
84  }
85  return -1;
86}
87
88//---------------------------------------------------------------------------
89/* zwraca index nastepnego wyniku, podczas pierwszego wywolania
90 * zwraca index wyniku o najwiekszej wadze, przy drugim wywolaniu
91 * wynik z druga najwyzsza waga, itd.
92 * Jezeli nie ma juz wynikow - zwraca -1.
93 */
94int Words::next() {
95  float max = -1;
96  int result = -1;
97  for (int i=0; i<cnt; ++i) {
98    float w = tab[i].w_suf();
99    if (w>max && !tab[i].returned) {
100      max = w;
101      result = i;
102    }
103  }
104  if (result != -1)
105    tab[result].returned = 1;
106  return result;
107}
108
109//---------------------------------------------------------------------------
110void Words::sort() {
111  std::sort(tab.begin(), tab.end(), Word::cmp_w);
112}
113
114//---------------------------------------------------------------------------
115
116int Words::add(const char* fo)
117{
118  int i = find(fo);
119  if(i!=-1) {
120    return i;
121  }
122 
123  if (cnt>=tab.capacity()-1)
124    tab.resize(tab.size()*2);
125 
126
127  Word o;
128  o.form(fo);
129  o.w_suf(0.0);
130  tab.push_back(o);
131//  tab[cnt].form(fo);
132//  tab[cnt].w_suf(0.0);
133
134
135  //  if(cnt<MAX-1) {
136  /* tab.push_back(new Word());
137    tab[cnt]->form(fo);
138    tab[cnt]->w_suf(0.0);
139    tab[cnt]->w_pref(0.0);*/
140    return cnt++;
141    //  }
142    //return -1;
143}
144
145//---------------------------------------------------------------------------
146
147 //TYMCZASOWO TAK(DLA CORA)
148int Words::add(const char* fo, float weight)
149{
150  int i = find(fo);
151  if(i!=-1) {
152    return i;
153  }
154 
155  if (cnt>=tab.capacity()-1)
156    tab.resize(tab.size()*2);
157 
158  Word o;
159  o.form(fo);
160  o.w_suf(weight);
161  tab.push_back(o);
162//  tab[cnt].form(fo);
163//  tab[cnt].w_suf(weight);
164 
165    return cnt++;
166    //  }
167    //return -1;
168}
169
170//---------------------------------------------------------------------------
171
172int Words::add(const char* fo, const char* des)
173{
174  char d[Word::MAXDESCRLEN];
175  int l=strcspn(des,",");
176  int ok=1;
177  if( *(des+l) == ',' )
178    {
179      strcpy(d,des+l+1);
180      //  printf("\t%s->%s,\n", des, d);
181      int i=find(fo, d);
182      if(i!=-1)
183        return i;
184    }
185  else
186    ok=0;
187
188  if (cnt>=tab.capacity()-1)
189    tab.resize(tab.size()*2);
190
191  tab[cnt].form(fo);
192  if(ok)
193    tab[cnt].autodescr(fo, des);
194  else
195    tab[cnt].autodescr(fo, "?,?");
196 
197  tab[cnt].w_suf(0.0);
198  tab[cnt].returned = 0;
199  /*
200  //  if(cnt<MAX-1) {
201    tab.push_back(new Word());
202    tab[cnt]->form(fo);
203    tab[cnt]->autodescr(fo,des);
204    tab[cnt]->w_suf(0.0);
205    tab[cnt]->w_pref(0.0);
206    //    printf("ok!\n");*/
207    return cnt++;
208    //  }
209    //  printf("hm\n");
210  return -1;
211}
212
213//---------------------------------------------------------------------------
214void Words::prn(ostream& os)
215{
216  for(int i=0; i<count(); ++i)
217    os << "<W " << tab[i].lemma() << ',' << tab[i].descr() << ">";
218}
219
220//---------------------------------------------------------------------------
221
222ostream& operator<<(ostream& os, Words& tab)
223{
224  /*  for(int i=0; i<tab.count(); ++i)
225    os << i << ". " << tab[i] << '\n';
226    return os;*/
227}
228
229//---------------------------------------------------------------------------
230
Note: See TracBrowser for help on using the repository browser.