source: app/src/lib/word.cc @ 25ae32e

help
Last change on this file since 25ae32e was 25ae32e, checked in by obrebski <obrebski@…>, 16 years ago

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

  • Property mode set to 100644
File size: 4.8 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//---------------------------------------------------------------------------
25int 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  tab[cnt].form(fo);
127  tab[cnt].w_suf(0.0);
128
129  //  if(cnt<MAX-1) {
130  /* tab.push_back(new Word());
131    tab[cnt]->form(fo);
132    tab[cnt]->w_suf(0.0);
133    tab[cnt]->w_pref(0.0);*/
134    return cnt++;
135    //  }
136    //return -1;
137}
138
139//---------------------------------------------------------------------------
140
141int Words::add(const char* fo, const char* des)
142{
143  char d[Word::MAXDESCRLEN];
144  int l=strcspn(des,",");
145  int ok=1;
146  if( *(des+l) == ',' )
147    {
148      strcpy(d,des+l+1);
149      //  printf("\t%s->%s,\n", des, d);
150      int i=find(fo, d);
151      if(i!=-1)
152        return i;
153    }
154  else
155    ok=0;
156
157  if (cnt>=tab.capacity()-1)
158    tab.resize(tab.size()*2);
159
160  tab[cnt].form(fo);
161  if(ok)
162    tab[cnt].autodescr(fo, des);
163  else
164    tab[cnt].autodescr(fo, "?,?");
165 
166  tab[cnt].w_suf(0.0);
167  tab[cnt].returned = 0;
168  /*
169  //  if(cnt<MAX-1) {
170    tab.push_back(new Word());
171    tab[cnt]->form(fo);
172    tab[cnt]->autodescr(fo,des);
173    tab[cnt]->w_suf(0.0);
174    tab[cnt]->w_pref(0.0);
175    //    printf("ok!\n");*/
176    return cnt++;
177    //  }
178    //  printf("hm\n");
179  return -1;
180}
181
182//---------------------------------------------------------------------------
183void Words::prn(ostream& os)
184{
185  for(int i=0; i<count(); ++i)
186    os << "<W " << tab[i].lemma() << ',' << tab[i].descr() << ">";
187}
188
189//---------------------------------------------------------------------------
190
191ostream& operator<<(ostream& os, Words& tab)
192{
193  /*  for(int i=0; i<tab.count(); ++i)
194    os << i << ". " << tab[i] << '\n';
195    return os;*/
196}
197
198//---------------------------------------------------------------------------
199
Note: See TracBrowser for help on using the repository browser.