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