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