source: _old/app/src/gue/guess.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
2#include "guess.h"
3
4#include <cstring>
5#include <iostream>
6#include <cstdlib>
7#include <cassert>
8#include <ctime>
9
10#define DICT 1
11#define COR 2
12#define DICT_P 3
13#define COR_P 4
14
15#define W_PRE 0.1
16#define W_SUF 0.9
17
18#define PREF_SIGN '_'
19
20using namespace std;
21
22Guess::Guess(const char* suf_file)
23  : _suf(suf_file) {
24  /*  _suf = NULL;
25  _pref = NULL;
26
27   if (strlen(suf_file) > 0)
28    _suf = new TFTiv<char, char>(suf_file);
29  if (strlen(pref_file) > 0)
30    _pref = new TFTiv<char, char>(corp_file);
31  */
32}
33
34
35  char buf[MAX_LINE];
36  char out[MAX_LINE];
37  char* buf0_s = buf;
38  char* word_t = NULL;
39  long state_s = 0;
40  unsigned length_s = buf0_s - buf;
41  long len = 0;
42  int i=0;
43
44int Guess::ana(const char* word, Words& result) {
45
46  assert(word && &result);
47
48  /* Word zawiera wyraz, ktory mamy zbadac.
49   * Nalezy przepisac go w odwrotnej kolejnosci do bufora,
50   * znalezc najdluzszy prefiks pasujacy do tego bufora
51   * separatorem jest '/' - za tym znakiem znajduje sie
52   * prawdopodobienstwo wystapienia danego opisu */
53
54  buf0_s = buf;
55  word_t = strdup(word);
56
57  if (reverse(word, buf) != 0)
58    return -1;
59
60 
61
62  state_s = -1;
63  //  printf("#buf0_s=%s, ", buf0_s);
64  state_s = _suf.pref(buf0_s, PREF_SIGN);
65  //  printf("#word=%s, buf0_s=%s\t", word, buf0_s);
66  /* jezeli state_s != -1 to oznacza, ze w slowniku jest zawarta
67   * informacja o prefiksie tego slowa.
68   * nie jest ona odwrocona, wiec porownujemy do word a nie do buf
69   */
70  //  printf("state_s=%d\t", state_s);
71  if (state_s != -1) {
72    state_s = _suf.pref(word_t, '~', state_s);
73    //    printf("state_s(wp)=%d, word_t=%s, word=%s\n", state_s, word_t, word);
74  }
75  if (state_s == -1) {
76  //  if (_suf != NULL)
77    buf0_s = buf;
78    state_s = _suf.pref(buf0_s, '~');
79    //    printf("state_s=%d\n", state_s);
80  }
81
82  length_s = buf0_s - buf;
83 
84  /* state jest stanem, od ktorego zaczyna sie sciezka opisujaca
85   * prawdopodobienstwo przeciwienstwa wystapienia opisu
86   * znajdujacego sie dalej na tej sciezce.
87   * Im mniejsza wartosc liczby tym wieksze prawdopodobienstwo */
88
89  len = 0;
90  i=0;
91 
92  //  if (_suf != NULL)
93    len = _suf.cont(state_s, out);
94  while (len > 0) {
95    i++;
96    add_word_prob(result, word, out, length_s, DICT);
97    len = _suf.cont(-1, out);
98  }
99   
100  return i;
101
102}
103
104
105int Guess::add_word_prob(Words& tab, const char* word, const char* path, unsigned len, int source) {
106
107  /* Dodaje do tablicy tab wyraz word wraz
108   * z prawdopodobienstwem i opisem zawartym
109   * w sciezce path */
110
111  //  printf("add_word_prob(");
112  //  fflush(stdout);
113  char p[MAX_LINE];
114
115  strcpy(p, path);
116
117  int probLen = strcspn(p, ";");
118  char prob[probLen+1];
119  strncpy(prob, p, probLen);
120  prob[probLen] = '\0';
121
122  char* desc = p + probLen+1; // +2 bo pomijamy jeszcze znak ';'
123
124  int i = tab.add(word, desc);
125
126  if (source==DICT) {
127    tab[i].len_suf(len);
128    tab[i].w_suf(atof(prob)); // + W_PRE*tab[i].w_suf()));
129    //    tab[i].w_suf((float)(W_SUF*(1000-atof(prob)) + W_PRE*tab[i].w_suf()));
130  }
131//   if (source==COR) {
132//     tab[i].len_pref(len);
133//     tab[i].w_pref(W_SUF*(1000-atof(prob)) + W_PRE*tab[i].w_pref());
134//   }
135//   printf(")\n");
136//   fflush(stdout);
137
138  return i;
139
140}
Note: See TracBrowser for help on using the repository browser.