1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
/// Maps raw rule weights to the score algebra used by one-best algorithms.
///
/// `Explicit` stores the raw weights it was built with. Algorithms that combine
/// many weights use a scorer to decide how to interpret those raw values.
pub trait WeightScorer {
/// Score for an impossible derivation.
fn zero(&self) -> f64;
/// Score for an empty product.
fn one(&self) -> f64;
/// Convert one raw rule weight into this scorer's representation.
fn rule_score(&self, weight: f64) -> f64;
/// Combine two scores along one derivation.
fn times(&self, left: f64, right: f64) -> f64;
/// Convert a final score back to a conventional raw weight.
fn score_to_weight(&self, score: f64) -> f64;
/// Return whether `candidate` is strictly better than `current`.
#[inline]
fn better(&self, candidate: f64, current: f64) -> bool {
candidate > current
}
}
/// Interpret raw weights as ordinary multiplicative weights.
#[derive(Clone, Copy, Debug, Default)]
pub struct ProbabilityScorer;
impl WeightScorer for ProbabilityScorer {
#[inline]
fn zero(&self) -> f64 {
0.0
}
#[inline]
fn one(&self) -> f64 {
1.0
}
#[inline]
fn rule_score(&self, weight: f64) -> f64 {
weight
}
#[inline]
fn times(&self, left: f64, right: f64) -> f64 {
left * right
}
#[inline]
fn score_to_weight(&self, score: f64) -> f64 {
score
}
}
/// Interpret raw weights as probabilities and combine them in log space.
#[derive(Clone, Copy, Debug, Default)]
pub struct LogProbabilityScorer;
impl WeightScorer for LogProbabilityScorer {
#[inline]
fn zero(&self) -> f64 {
f64::NEG_INFINITY
}
#[inline]
fn one(&self) -> f64 {
0.0
}
#[inline]
fn rule_score(&self, weight: f64) -> f64 {
if weight == 0.0 {
f64::NEG_INFINITY
} else {
weight.ln()
}
}
#[inline]
fn times(&self, left: f64, right: f64) -> f64 {
left + right
}
#[inline]
fn score_to_weight(&self, score: f64) -> f64 {
score.exp()
}
}