Sorensen–Dice coefficient: Difference between revisions

Content added Content deleted
(Added Perl)
(Created Nim solution.)
Line 201: Line 201:
0.608696 Fermat numbers
0.608696 Fermat numbers
0.600000 Lah numbers </pre>
0.600000 Lah numbers </pre>

=={{header|Nim}}==
<syntaxhighlight lang=Nim>import std/[algorithm, strutils, sugar, tables]

func bigrams(text: string): CountTable[string] =
## Extract the bigrams from a text.
for word in text.toLower.split(' '):
if word.len == 1:
result.inc(word)
else:
for i in 0..(word.len - 2):
result.inc(word[i..(i+1)])

func intersectionCount(a, b: CountTable[string]): int =
## Compute the cardinal of the intersection of two
## count tables.
for key, count in a:
if key in b:
inc result, min(count, b[key])

func card(a: CountTable[string]): int =
## Return the cardinal of a count table (i.e. the sum of counts).
for count in a.values:
inc result, count

func sorensenDice(text1, text2: string): float =
## Compute the Sorensen-dice coefficient of "text1" and "text2".
let ct1 = text1.bigrams
let ct2 = text2.bigrams
result = 2 * intersectionCount(ct1, ct2) / (ct1.card + ct2.card)

# Build the list of tasks.
let tasks = collect:
for line in lines("Sorensen-Dice.txt"):
line

const Tests = ["Primordial primes", "Sunkist-Giuliani formula",
"Sieve of Euripides", "Chowder numbers"]

for test in Tests:
echo test
var scores: seq[(float, string)]
for task in tasks:
scores.add (sorensenDice(test, task), task)
scores.sort(Descending)
for i in 0..4:
echo " ", scores[i][0].formatFloat(ffDecimal, 6), ' ', scores[i][1]
echo()
</syntaxhighlight>

<pre>Primordial primes
0.685714 Sequence of primorial primes
0.666667 Factorial primes
0.571429 Primorial numbers
0.545455 Prime words
0.521739 Almost prime

Sunkist-Giuliani formula
0.565217 Almkvist-Giullera formula for pi
0.378378 Faulhaber's formula
0.342857 Haversine formula
0.333333 Check Machin-like formulas
0.307692 Resistance calculator

Sieve of Euripides
0.461538 Sieve of Pritchard
0.461538 Four sides of square
0.413793 Sieve of Eratosthenes
0.400000 Piprimes
0.384615 Sierpinski curve

Chowder numbers
0.782609 Chowla numbers
0.640000 Powerful numbers
0.608696 Rhonda numbers
0.608696 Fermat numbers
0.600000 Lah numbers
</pre>


=={{header|Perl}}==
=={{header|Perl}}==