| 1 | //--------------------------------------------------------------------------- |
|---|
| 2 | #include "word.h" |
|---|
| 3 | #include "auttools.h" |
|---|
| 4 | #include <istream> |
|---|
| 5 | #include <algorithm> |
|---|
| 6 | //--------------------------------------------------------------------------- |
|---|
| 7 | //--------------------------------------------------------------------------- |
|---|
| 8 | |
|---|
| 9 | void 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 | //--------------------------------------------------------------------------- |
|---|
| 26 | bool Word::cmp_w(Word a, Word b) { |
|---|
| 27 | return (a.w_suf() > b.w_suf()); |
|---|
| 28 | } |
|---|
| 29 | //--------------------------------------------------------------------------- |
|---|
| 30 | bool Word::cmp_w_rev(Word a, Word b) { |
|---|
| 31 | return (a.w_suf() < b.w_suf()); |
|---|
| 32 | } |
|---|
| 33 | //--------------------------------------------------------------------------- |
|---|
| 34 | bool cmp_w_fun(Word a, Word b) { |
|---|
| 35 | return (a.w_suf() > b.w_suf()); |
|---|
| 36 | } |
|---|
| 37 | //--------------------------------------------------------------------------- |
|---|
| 38 | bool cmp_w_rev_fun(Word a, Word b) { |
|---|
| 39 | return (a.w_suf() < b.w_suf()); |
|---|
| 40 | } |
|---|
| 41 | //--------------------------------------------------------------------------- |
|---|
| 42 | |
|---|
| 43 | istream& 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 | |
|---|
| 64 | ostream& 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 | //--------------------------------------------------------------------------- |
|---|
| 74 | Words::~Words() { |
|---|
| 75 | // for (int i=0; i<tab.size(); ++i) |
|---|
| 76 | // delete(tab[i]); |
|---|
| 77 | } |
|---|
| 78 | //--------------------------------------------------------------------------- |
|---|
| 79 | |
|---|
| 80 | int 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 | |
|---|
| 92 | int 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 | */ |
|---|
| 107 | int 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 | //--------------------------------------------------------------------------- |
|---|
| 123 | void Words::sort() { |
|---|
| 124 | std::sort(tab.begin(), tab.end(), Word::cmp_w); |
|---|
| 125 | } |
|---|
| 126 | |
|---|
| 127 | //--------------------------------------------------------------------------- |
|---|
| 128 | void Words::sort_rev() { |
|---|
| 129 | std::sort(tab.begin(), tab.end(), cmp_w_rev_fun); |
|---|
| 130 | } |
|---|
| 131 | |
|---|
| 132 | //--------------------------------------------------------------------------- |
|---|
| 133 | |
|---|
| 134 | int Words::add(const char* fo) |
|---|
| 135 | { |
|---|
| 136 | int i = find(fo); |
|---|
| 137 | if(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) |
|---|
| 166 | int 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 | |
|---|
| 190 | int 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 | //--------------------------------------------------------------------------- |
|---|
| 232 | void 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 | |
|---|
| 240 | ostream& 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 | |
|---|