source: src/lib/word.cc @ 5f4d9c3

Last change on this file since 5f4d9c3 was 5f4d9c3, checked in by Maciej Prill <mprill@…>, 12 years ago

Rewritten the build system, added lem UTF-8 version.

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