Sorensen–Dice coefficient: Difference between revisions

J draft
m (→‎{{header|Phix}}: and I forgot to update the output!)
(J draft)
Line 42:
 
If there is a built-in or easily, freely available library implementation for Sørensen–Dice coefficient calculations it is acceptable to use that with a pointer to where it may be obtained.
 
 
=={{header|J}}==
Tentative implementation:
<syntaxhighlight lang=J>TASKS=: fread '~/tasks.txt' NB. from Sorensen–Dice_coefficient/Tasks
stok=: [: (#~ ' '*/ .~:~])2]\ 7 u: tolower@rplc&(LF,' ')
union=: ,
inter=: [-.-.
SDI=: ((inter +&#~ inter~) % #@union)&stok S:0
nearest=: {{ m{.\:~ x (] ;"0~ SDI) cutLF y }}</syntaxhighlight>
 
This is slightly different from the task description, which suggests that <code>SDI = 2 × (A ∩ B) / (A ⊎ B)</code> produces a number between 0 and 1. If A and B are sets, each containing the same tokens, the result here would be 2 rather than 1. But we can make sense of this by assuming that the original algorithm was working with sequences rather than sets. The sequence difference is not commutative, so if <code>∩</code> represents sequence difference, and <code>⊎</code> represents sequence addition, it would make sense to define <code>SDI= ((A ∩ B) + (B ∩ A)) / (A ⊎ B)</code>, which is what we have done here.
 
With this implementation, here's the task examples:
<syntaxhighlight lang=J> 'Primordial primes' 5 nearest TASKS
┌────────┬────────────────────────────┐
│0.740741│Factorial primes │
├────────┼────────────────────────────┤
│0.714286│Sequence of primorial primes│
├────────┼────────────────────────────┤
│0.681818│Prime words │
├────────┼────────────────────────────┤
│0.652174│Almost prime │
├────────┼────────────────────────────┤
│0.642857│Primorial numbers │
└────────┴────────────────────────────┘
'Sunkist-Giuliani formula' 5 nearest TASKS
┌────────┬───────────────────────────────────┐
│0.608696│Almkvist-Giullera formula for pi │
├────────┼───────────────────────────────────┤
│0.378378│Faulhaber's formula │
├────────┼───────────────────────────────────┤
│0.371429│Haversine formula │
├────────┼───────────────────────────────────┤
│0.357143│Check Machin-like formulas │
├────────┼───────────────────────────────────┤
│0.340426│Shoelace formula for polygonal area│
└────────┴───────────────────────────────────┘
'Sieve of Euripides' 5 nearest TASKS
┌────────┬────────────────────────┐
│0.461538│Sieve of Pritchard │
├────────┼────────────────────────┤
│0.461538│Four sides of square │
├────────┼────────────────────────┤
│0.413793│Sieve of Eratosthenes │
├────────┼────────────────────────┤
│0.4 │Piprimes │
├────────┼────────────────────────┤
│0.392857│Law of cosines - triples│
└────────┴────────────────────────┘
'Chowder numbers' 5 nearest TASKS
┌────────┬──────────────┐
│0.826087│Chowla numbers│
├────────┼──────────────┤
│0.666667│Bell numbers │
├────────┼──────────────┤
│0.652174│Rhonda numbers│
├────────┼──────────────┤
│0.652174│Humble numbers│
├────────┼──────────────┤
│0.65 │Lah numbers │
└────────┴──────────────┘</syntaxhighlight>
 
 
 
6,962

edits