source: _old/app/src/lem/lem.cc @ 1e121f4

Last change on this file since 1e121f4 was 1e121f4, checked in by Adam Kędziora <s301614@…>, 14 years ago

Replacing old implementation with working implementation

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