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