fixed bugs in Text::penalty2_construct(), Penalty2DAG::set_syllable_weights()
[vspell.git] / libvspell / penalty.cpp
blobb02f80ac51c4f90d3eef1be3d5373d5830892d20
1 #include "penalty.h" // -*- tab-width: 2 -*-
2 #include "spell.h"
4 //using namespace Dictionary;
5 using namespace std;
7 float PenaltyDAG::edge_value(uint node_from,uint node_to) const
9 float value = dag->edge_value(node_from,node_to);
11 WordEntry *we = (WordEntry*)dag->node_info(node_to);
12 if (we) {
13 uint count = 0;
14 for (uint i = 0;i < we->len;i ++)
15 if (we->fuzid & (1 << i))
16 count ++;
17 value -= (float)count*weight*value;
20 we = (WordEntry*)dag->node_info(node_from);
21 if (we) {
22 uint count = 0;
23 for (uint i = 0;i < we->len;i ++)
24 if (we->fuzid & (1 << i))
25 count ++;
26 value -= (float)count*weight*value;
28 return value > 0 ? value : 0;
31 float Penalty2DAG::edge_value(uint node_from,uint node_to) const
33 float value = dag->edge_value(node_from,node_to);
35 WordEntry *we = (WordEntry*)dag->node_info(node_to);
36 if (we) {
37 uint count = 0;
38 float myweight = 0;
39 for (uint i = 0;i < we->len;i ++)
40 if (we->fuzid & (1 << i))
41 myweight += weight*syllable_weights[we->pos+i];
42 value += (float)myweight*value;
45 we = (WordEntry*)dag->node_info(node_from);
46 if (we) {
47 uint count = 0;
48 float myweight = 0;
49 for (uint i = 0;i < we->len;i ++)
50 if (we->fuzid & (1 << i))
51 myweight += weight*syllable_weights[we->pos+i];
52 value += (float)count*weight*value;
54 return value > 0 ? value : 0;
57 void Penalty2DAG::set_syllable_weights(const Segmentation &seg)
59 uint i,n = seg.size();
60 strid v[5],ov;
62 syllable_weights.resize(seg[n-1].pos+seg[n-1].len);
64 for (i = 0;i < n;i ++) {
65 v[0] = i >= 2 ? seg[i-2].node.node->get_id() : get_id(START_ID);
66 v[1] = i >= 1 ? seg[i-1].node.node->get_id() : get_id(START_ID);
67 v[2] = seg[i].node.node->get_id();
68 v[3] = i + 1 < n ? seg[i+1].node.node->get_id() : get_id(STOP_ID);
69 v[4] = i + 2 < n ? seg[i+2].node.node->get_id() : get_id(STOP_ID);
71 ov = v[2];
72 v[2] = Vocab_None;
73 float v1 = LogPtoProb(get_ngram().wordProb(ov,v));
74 v[2] = ov;
76 ov = v[3];
77 v[3] = Vocab_None;
78 float v2 = LogPtoProb(get_ngram().wordProb(ov,v+1));
79 v[3] = ov;
81 ov = v[4];
82 v[4] = Vocab_None;
83 float v3 = LogPtoProb(get_ngram().wordProb(ov,v+2));
84 v[4] = ov;
86 for (uint j = seg[i].pos;j < seg[i].pos+seg[i].len;j ++)
87 syllable_weights[j] = (v1+v2+v3)/3;