Skip to content

Commit 137a0a3

Browse files
committedJan 31, 2025·
Specify the delta function by a short name instead of FQN
1 parent 09535b5 commit 137a0a3

File tree

2 files changed

+23
-7
lines changed

2 files changed

+23
-7
lines changed
 

‎lib/Statistics/Krippendorff.pm

+21-4
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,12 @@ has units => (is => 'ro',
2626
required => 1,
2727
coerce => \&_units_array2hash);
2828

29-
has delta => (is => 'rw', default => sub { \&delta_nominal });
29+
has delta => (is => 'rw',
30+
default => sub { \&delta_nominal },
31+
trigger => sub ($self, $d) {
32+
$self->delta($self->_deltas->{$d})
33+
if exists $self->_deltas->{$d};
34+
});
3035

3136
has coincidence => (is => 'lazy', init_arg => undef);
3237

@@ -42,6 +47,17 @@ has _expected => (is => 'lazy',
4247
init_arg => undef,
4348
builder => '_build_expected');
4449

50+
has _deltas => (is => 'ro',
51+
init_arg => undef,
52+
default => sub { +{
53+
nominal => \&delta_nominal,
54+
interval => \&delta_interval,
55+
ordinal => \&delta_ordinal,
56+
ratio => \&delta_ratio,
57+
jaccard => \&delta_jaccard,
58+
masi => \&delta_masi
59+
} });
60+
4561
sub alpha($self) {
4662
my $d_o = sum(map {
4763
my $v = $_;
@@ -192,7 +208,7 @@ sub _build_expected($self) {
192208
{coder2 => 3, coder3 => 2});
193209
my $sk = 'Statistics::Krippendorff'->new(units => \@units);
194210
my $alpha1 = $sk->alpha;
195-
$sk->delta(\&Statistics::Krippendorff::delta_nominal); # Same as default.
211+
$sk->delta('nominal'); # Same as default.
196212
my $alpha2 = $sk->alpha;
197213
198214
my $ski = 'Statistics::Krippendorff'->new(
@@ -206,7 +222,7 @@ sub _build_expected($self) {
206222
207223
my $sk = 'Statistics::Krippendorff'->new(
208224
units => \@units,
209-
delta => \&Statistics::Krippendorff::delta_nominal);
225+
delta => 'nominal');
210226
211227
The constructor. It accepts the following named arguments:
212228
@@ -243,7 +259,7 @@ to validate this precondition, call C<is_valid>.
243259
An optional argument defaulting to delta_nominal. You can specify any function
244260
C<f($self, $v1, $v2)> that compares the two values C<$v1> and C<$v2> and
245261
returns their distance (a number between 0 and 1). Several common methods are
246-
predefined:
262+
predefined, you can use a code reference like C<&Statistics::Krippendorff::delta_nominal> or just a string C<nominal>:
247263
248264
=head4 delta_nominal
249265
@@ -289,6 +305,7 @@ Returns Krippendorff's alpha.
289305
=head2 delta
290306
291307
$sk->delta(sub($self, $v1, $v2) {});
308+
$sk->delta('jaccard');
292309
293310
The difference function used to calculate the alpha. You can specify it in the
294311
constructor (see above), but you can later change it so something else, too.

‎t/02-example.t

+2-3
Original file line numberDiff line numberDiff line change
@@ -72,10 +72,9 @@ subtest_buffered stackexchange_stats_511927 => sub {
7272
my @units = (['f,s', 'f', 's,e,m'],
7373
['s', 's', 's,e']);
7474
my $sk = 'Statistics::Krippendorff'->new(
75-
units => \@units,
76-
delta => \&Statistics::Krippendorff::delta_jaccard);
75+
units => \@units, delta => 'jaccard');
7776
is $sk->alpha, float(0.152, precision => 3);
7877

79-
$sk->delta(\&Statistics::Krippendorff::delta_masi);
78+
$sk->delta('masi');
8079
is $sk->alpha, float(0.1296, precision => 4); # nltk would say 0.1297
8180
};

0 commit comments

Comments
 (0)
Please sign in to comment.