[5f4d9c3] | 1 | #include "lem.h" |
---|
| 2 | #include <cstdlib> |
---|
| 3 | #include <cassert> |
---|
| 4 | |
---|
| 5 | /* Znajduje opisy slownikowe dla wyrazu. |
---|
| 6 | * Parametry: |
---|
| 7 | * form - wyraz, |
---|
| 8 | * tab - referencja do tablicy Words (miejsce na wyniki) |
---|
| 9 | * Wartosc: |
---|
| 10 | * liczba dodanych opisow |
---|
| 11 | */ |
---|
| 12 | int Lem::ana(const wchar_t* form, Words& tab) { |
---|
| 13 | assert(form && &tab); |
---|
| 14 | int count0 = tab.count(); |
---|
| 15 | LemFST::State l,d; |
---|
| 16 | int i = 0; |
---|
| 17 | LemFST::Word w = *LemFST::charToWord(form); |
---|
| 18 | d = _dict.next(_dict.start(), w); |
---|
| 19 | d = _dict.next(d, ';'); |
---|
| 20 | |
---|
| 21 | if (d>=0) |
---|
| 22 | add_to_table(tab, form, d); |
---|
| 23 | return tab.count()-count0; |
---|
| 24 | } |
---|
| 25 | |
---|
| 26 | |
---|
| 27 | |
---|
| 28 | /* Dodaje kolejne opisy do tablicy wynikow. |
---|
| 29 | * Parametry: |
---|
| 30 | * tab - tablica wynikow, |
---|
| 31 | * f - wyraz, |
---|
| 32 | * s - stan, na ktorym zaczyna sie pierwszy opis |
---|
| 33 | */ |
---|
| 34 | void Lem::add_to_table(Words& tab, const wchar_t* f, LemFST::State s) { |
---|
| 35 | assert(&tab); |
---|
| 36 | assert(f); |
---|
| 37 | |
---|
| 38 | LemFST::Word *w = new LemFST::Word(); |
---|
| 39 | int r; |
---|
| 40 | while (r = _dict.cont(s, w, 200)) { |
---|
| 41 | char fchar[200]; //Bufor do przetrzymywania f w postaci zwyklych char |
---|
| 42 | wcstombs(fchar, f, 200); // Zamien f w zwykle chary i umiesc w fchar |
---|
| 43 | const char* wChar = LemFST::wordToChar(w); // Aktualne slowo zamien w chary |
---|
| 44 | w->clear(); // czyscimy bufor ze slowem. |
---|
| 45 | tab.add(fchar, wChar); |
---|
| 46 | } |
---|
| 47 | _dict.cont(LemFST::noStateId, w, 200); // ubijamy generator |
---|
| 48 | delete w; |
---|
| 49 | } |
---|
| 50 | |
---|
| 51 | void Lem::prn_dict() |
---|
| 52 | { |
---|
| 53 | LemFST::Word des; |
---|
| 54 | LemFST::State s=_dict.start(); |
---|
| 55 | |
---|
| 56 | while (_dict.cont(s, &des, 200)) |
---|
| 57 | { |
---|
| 58 | wprintf(L"%ls\n",des.size()); // Ladne drukowanie! |
---|
| 59 | s=-1; |
---|
| 60 | // TODO: Ladne wyswiatlanie slownika; |
---|
| 61 | } |
---|
| 62 | } |
---|
| 63 | |
---|
| 64 | |
---|
| 65 | |
---|
| 66 | |
---|
| 67 | |
---|
| 68 | |
---|
| 69 | |
---|
| 70 | |
---|
| 71 | |
---|
| 72 | |
---|
| 73 | |
---|
| 74 | //====================================================================== |
---|
| 75 | // AUX LEM |
---|
| 76 | //====================================================================== |
---|
| 77 | AuxLem::AuxLem(const char* filename) |
---|
| 78 | : Lem(filename), _dict(SIZE) |
---|
| 79 | { |
---|
| 80 | FILE* f; |
---|
| 81 | char buf[MAX_LINE+2]; |
---|
| 82 | f=fopen(filename,"r"); |
---|
| 83 | for(long i=0; i<SIZE; ++i) info[i]=(char*)NULL; |
---|
| 84 | while(fgets(buf,MAX_LINE,f)) |
---|
| 85 | { |
---|
| 86 | int l=strlen(buf); |
---|
| 87 | if(l>=MAX_LINE-1) continue; // BEZ isalpha! |
---|
| 88 | buf[l-1]='\0'; |
---|
| 89 | char* sep=strchr(buf,';'); |
---|
| 90 | if(sep==NULL) continue; |
---|
| 91 | *sep='\0'; |
---|
| 92 | long formind=_dict.add(buf); |
---|
| 93 | if(formind>=0) |
---|
| 94 | { |
---|
| 95 | char* desc=strdup(sep+1); |
---|
| 96 | info[formind]=desc; |
---|
| 97 | } |
---|
| 98 | else |
---|
| 99 | fprintf(stderr,"AuxLem: Form not added: %s;%s.\n", buf,sep+1); |
---|
| 100 | } |
---|
| 101 | fclose(f); |
---|
| 102 | }; |
---|
| 103 | |
---|
| 104 | |
---|
| 105 | AuxLem::~AuxLem() |
---|
| 106 | { |
---|
| 107 | for(long i=0; i<SIZE; ++i) |
---|
| 108 | if(info[i]) free(info[i]); |
---|
| 109 | } |
---|
| 110 | |
---|
| 111 | |
---|
| 112 | int AuxLem::ana(const char* form, Words& tab) |
---|
| 113 | { |
---|
| 114 | if(!form) return 0; |
---|
| 115 | int count0=tab.count(); |
---|
| 116 | char des[MAX_LINE]; |
---|
| 117 | long ind=_dict[form]; |
---|
| 118 | if(ind>=0) |
---|
| 119 | { |
---|
| 120 | strcpy(des,info[ind]); |
---|
| 121 | char* des1; |
---|
| 122 | if((des1=strtok(des,";"))!=NULL) |
---|
| 123 | do |
---|
| 124 | { |
---|
| 125 | if(tab.cnt>=MAXALT) break; |
---|
| 126 | tab.add(form,des1); |
---|
| 127 | des1=strtok(NULL,";"); |
---|
| 128 | } while(des1!=NULL); |
---|
| 129 | } |
---|
| 130 | return tab.count()-count0; |
---|
| 131 | } |
---|
| 132 | |
---|
| 133 | |
---|