source: src/gue/guess.cc @ 5f4d9c3

Last change on this file since 5f4d9c3 was 5f4d9c3, checked in by Maciej Prill <mprill@…>, 12 years ago

Rewritten the build system, added lem UTF-8 version.

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