| [25ae32e] | 1 | #include "lem.h" |
|---|
| 2 | |
|---|
| 3 | #include <assert.h> |
|---|
| 4 | |
|---|
| 5 | |
|---|
| 6 | /* Znajduje opisy slownikowe dla wyrazu. |
|---|
| 7 | * Parametry: |
|---|
| 8 | * form - wyraz, |
|---|
| 9 | * tab - referencja do tablicy Words (miejsce na wyniki) |
|---|
| 10 | * Wartosc: |
|---|
| 11 | * liczba dodanych opisow |
|---|
| 12 | */ |
|---|
| 13 | int Lem::ana(const char* form, Words& tab) { |
|---|
| 14 | |
|---|
| 15 | // sprawdzamy czy parametry wywolania sa poprawne |
|---|
| 16 | assert(form && &tab); |
|---|
| 17 | int count0 = tab.count(); |
|---|
| 18 | long l; |
|---|
| 19 | if ((l=_dict.next(_dict.gtra(0, form, FT::ftMAXPATH), ';'))>=0) |
|---|
| 20 | add_to_table(tab, form, l); |
|---|
| 21 | return tab.count()-count0; |
|---|
| 22 | } |
|---|
| 23 | |
|---|
| 24 | |
|---|
| 25 | /* Szukamy opisu slownikowego nastepnego wyrazu w buforze. |
|---|
| 26 | * Parametry: |
|---|
| 27 | * buf - bufor |
|---|
| 28 | * tab - miejsce na wyniki |
|---|
| 29 | * Wartosc: |
|---|
| 30 | * ilosc dodanych opisow |
|---|
| 31 | */ |
|---|
| 32 | int Lem::pref(char* buf, Words& tab) { |
|---|
| 33 | |
|---|
| 34 | // sprawdzamy czy parametry wywolania sa poprawne |
|---|
| 35 | assert(buf && &tab); |
|---|
| 36 | |
|---|
| 37 | int count0 = tab.count(); |
|---|
| 38 | long l; |
|---|
| 39 | char* buf0 = buf; |
|---|
| 40 | |
|---|
| 41 | if((l=_dict.pref(buf, ';'))>=0) { |
|---|
| 42 | char form[MAX_FORM]; |
|---|
| 43 | int len=buf-buf0; |
|---|
| 44 | form[len]='\0'; |
|---|
| 45 | add_to_table(tab,form,l); |
|---|
| 46 | } |
|---|
| 47 | return tab.count() - count0; |
|---|
| 48 | } |
|---|
| 49 | |
|---|
| 50 | /* Dodaje kolejne opisy do tablicy wynikow. |
|---|
| 51 | * Parametry: |
|---|
| 52 | * tab - tablica wynikow, |
|---|
| 53 | * f - wyraz, |
|---|
| 54 | * s - stan, na ktorym zaczyna sie pierwszy opis |
|---|
| 55 | */ |
|---|
| 56 | void Lem::add_to_table(Words& tab, const char* f, long s) { |
|---|
| 57 | |
|---|
| 58 | // sprawdzenie parametrow |
|---|
| 59 | assert(&tab); |
|---|
| 60 | assert(f); |
|---|
| 61 | |
|---|
| 62 | char des[FT::ftMAXPATH]; |
|---|
| 63 | |
|---|
| 64 | while (_dict.cont(s, des)) { |
|---|
| 65 | char* des1; |
|---|
| 66 | if ((des1=strtok(des, ";")) != NULL) |
|---|
| 67 | do { |
|---|
| 68 | if (tab.count() >= MAX_ALT) break; |
|---|
| 69 | tab.add(f, des1); |
|---|
| 70 | des1=strtok(NULL, ";"); |
|---|
| 71 | } while (des1!=NULL); |
|---|
| 72 | s=-1; |
|---|
| 73 | } |
|---|
| 74 | } |
|---|
| 75 | |
|---|
| 76 | void Lem::prn_dict() |
|---|
| 77 | { |
|---|
| 78 | |
|---|
| 79 | char des[FT::ftMAXPATH]; |
|---|
| 80 | |
|---|
| 81 | long s=0; |
|---|
| 82 | |
|---|
| 83 | while (_dict.cont(s, des)) |
|---|
| 84 | { |
|---|
| 85 | printf("%s\n",des); |
|---|
| 86 | s=-1; |
|---|
| 87 | } |
|---|
| 88 | } |
|---|
| 89 | |
|---|
| 90 | |
|---|
| 91 | AuxLem::AuxLem(const char* filename) |
|---|
| 92 | : Lem(), _dict(SIZE) |
|---|
| 93 | { |
|---|
| 94 | FILE* f; |
|---|
| 95 | char buf[MAX_LINE+2]; |
|---|
| 96 | f=fopen(filename,"r"); |
|---|
| 97 | for(long i=0; i<SIZE; ++i) info[i]=(char*)NULL; |
|---|
| 98 | while(fgets(buf,MAX_LINE,f)) |
|---|
| 99 | { |
|---|
| 100 | int l=strlen(buf); |
|---|
| 101 | if(l>=MAX_LINE-1) continue; // BEZ isalpha! |
|---|
| 102 | buf[l-1]='\0'; |
|---|
| 103 | char* sep=strchr(buf,';'); |
|---|
| 104 | if(sep==NULL) continue; |
|---|
| 105 | *sep='\0'; |
|---|
| 106 | long formind=_dict.add(buf); |
|---|
| 107 | if(formind>=0) |
|---|
| 108 | { |
|---|
| 109 | char* desc=strdup(sep+1); |
|---|
| 110 | info[formind]=desc; |
|---|
| 111 | } |
|---|
| 112 | else |
|---|
| 113 | fprintf(stderr,"AuxLem: Form not added: %s;%s.\n", buf,sep+1); |
|---|
| 114 | } |
|---|
| 115 | fclose(f); |
|---|
| 116 | }; |
|---|
| 117 | |
|---|
| 118 | //--------------------------------------------------------------------------- |
|---|
| 119 | |
|---|
| 120 | AuxLem::~AuxLem() |
|---|
| 121 | { |
|---|
| 122 | // for(long i=0; i<_dict.count(); ++i) |
|---|
| 123 | // free(info[_dict.hashindex(i)]); |
|---|
| 124 | for(long i=0; i<SIZE; ++i) |
|---|
| 125 | if(info[i]) free(info[i]); |
|---|
| 126 | } |
|---|
| 127 | |
|---|
| 128 | //--------------------------------------------------------------------------- |
|---|
| 129 | |
|---|
| 130 | int AuxLem::ana(const char* form, Words& tab) |
|---|
| 131 | { |
|---|
| 132 | if(!form) return 0; |
|---|
| 133 | int count0=tab.count(); |
|---|
| 134 | char des[MAX_LINE]; |
|---|
| 135 | long ind=_dict[form]; |
|---|
| 136 | if(ind>=0) |
|---|
| 137 | { |
|---|
| 138 | strcpy(des,info[ind]); |
|---|
| 139 | char* des1; |
|---|
| 140 | if((des1=strtok(des,";"))!=NULL) |
|---|
| 141 | do |
|---|
| 142 | { |
|---|
| 143 | if(tab.cnt>=MAXALT) break; |
|---|
| 144 | tab.add(form,des1); |
|---|
| 145 | des1=strtok(NULL,";"); |
|---|
| 146 | } while(des1!=NULL); |
|---|
| 147 | } |
|---|
| 148 | return tab.count()-count0; |
|---|
| 149 | } |
|---|
| 150 | |
|---|
| 151 | //--------------------------------------------------------------------------- |
|---|
| 152 | |
|---|