fix minitoc/heading error
[vspell.git] / libvspell / penalty.cpp
blob7357843587763e0283848bb92758af5c2b29dfa3
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;
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 float myweight = 0;
38 for (uint i = 0;i < we->len;i ++)
39 if (we->fuzid & (1 << i))
40 myweight += syllable_weights[we->pos+i];
41 //if (myweight != 0)
42 //cerr << weight*myweight << endl;
43 value += (float)weight*myweight*value;
46 we = (WordEntry*)dag->node_info(node_from);
47 if (we) {
48 float myweight = 0;
49 for (uint i = 0;i < we->len;i ++)
50 if (we->fuzid & (1 << i))
51 myweight += syllable_weights[we->pos+i];
52 //if (myweight != 0)
53 //cerr << weight*myweight << endl;
54 value += (float)weight*myweight*value;
56 return value > 0 ? value : 0;
59 void Penalty2DAG::set_syllable_weights(const Segmentation &seg)
61 uint i,n = seg.size();
62 strid v[6],ov;
64 syllable_weights.resize(seg[n-1].pos+seg[n-1].len);
66 for (i = 0;i < n;i ++) {
67 v[4] = i >= 2 ? seg[i-2].node.node->get_id() : get_id(START_ID);
68 v[3] = i >= 1 ? seg[i-1].node.node->get_id() : get_id(START_ID);
69 v[2] = seg[i].node.node->get_id();
70 v[1] = i + 1 < n ? seg[i+1].node.node->get_id() : get_id(STOP_ID);
71 v[0] = i + 2 < n ? seg[i+2].node.node->get_id() : get_id(STOP_ID);
73 v[5] = Vocab_None;
74 double v1 = LogPtoProb(get_ngram().wordProb(v[2],&v[3]));
76 ov = v[4];
77 v[4] = Vocab_None;
78 double v2 = LogPtoProb(get_ngram().wordProb(v[1],&v[2]));
79 v[4] = ov;
81 ov = v[3];
82 v[3] = Vocab_None;
83 double v3 = LogPtoProb(get_ngram().wordProb(v[0],&v[1]));
84 v[3] = ov;
86 for (uint j = seg[i].pos;j < seg[i].pos+seg[i].len;j ++)
87 syllable_weights[j] = (v1+v2+v3)/3;