Skip to content

Commit

Permalink
edge case in weights
Browse files Browse the repository at this point in the history
  • Loading branch information
markvanderloo committed Aug 7, 2015
1 parent 394ff61 commit efcb127
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 10 deletions.
4 changes: 2 additions & 2 deletions pkg/src/dl.c
Original file line number Diff line number Diff line change
Expand Up @@ -111,10 +111,10 @@ double dl_dist(
){

if (!x){
return (double) y;
return (double) y * weight[1]; // ins weight
}
if (!y){
return (double) x;
return (double) x * weight[0]; // del weight
}

unsigned int swapCount, targetCharCount,i,j;
Expand Down
10 changes: 5 additions & 5 deletions pkg/src/lv.c
Original file line number Diff line number Diff line change
Expand Up @@ -28,23 +28,23 @@
* - Extended with custom weights and maxDistance
*/
double lv_dist(
unsigned int *a, int na,
unsigned int *b, int nb,
unsigned int *a, int na, // target
unsigned int *b, int nb, // source
double *weight,
double *scores){
if (!na){
return (double) nb;
return (double) nb * weight[1]; // del score
}
if (!nb){
return (double) na;
return (double) na * weight[0]; // ins score
}

int i, j;
int I = na+1, L = na+1, J = nb+1;
double sub;

for ( i = 0; i < I; ++i ){
scores[i] = i * weight[0];
scores[i] = i * weight[1];
}
for ( j = 1; j < J; ++j, L += I ){
scores[L] = j * weight[0];
Expand Down
6 changes: 3 additions & 3 deletions pkg/src/osa.c
Original file line number Diff line number Diff line change
Expand Up @@ -30,18 +30,18 @@
double osa_dist(unsigned int *a, int na, unsigned int *b, int nb, double *weight, double *scores){

if (!na){
return (double) nb;
return (double) nb * weight[1]; // ins weight
}
if (!nb){
return (double) na;
return (double) na * weight[0]; // del weight
}

int i, j;
int M, I = na+1, L=na+1, J = nb+1;
double sub, tran;

for ( i = 0; i < I; ++i ){
scores[i] = i * weight[0];
scores[i] = i * weight[1];
}
for ( j = 1; j < J; ++j, L += I ){
scores[L] = j * weight[0];
Expand Down
11 changes: 11 additions & 0 deletions pkg/tests/testthat/testStringdist.R
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,17 @@ test_that("weights are handled correctly",{
stringdist("abc","ac",method='osa',weight=c(0.5,1,1,1)),
stringdist("ac","abc",method='osa',weight=c(1,0.5,1,1))
)

expect_equal( # two deletions from source (b) to target (a)
stringdist("","aa",weight=c(0.5,1,1,1),method="osa"),1
)
expect_equal( # two deletions from source (b) to target (a)
stringdist("","aa",weight=c(0.5,1,1,1),method="lv"),1
)
expect_equal( # two deletions from source (b) to target (a)
stringdist("","aa",weight=c(0.5,1,1,1),method="dl"),1
)

# Thanks to Zach Price for reporting this bug.
expect_equal(
stringdist("ABC", "BC", method = "lv", weight = c(i=.1, d=.1, s=.1)),.1
Expand Down

0 comments on commit efcb127

Please sign in to comment.