Sorensen–Dice coefficient: Difference between revisions

Content added Content deleted
(Added Wren)
Line 250: Line 250:
0.608696 Rhonda numbers
0.608696 Rhonda numbers
0.6 Lah numbers</pre>
0.6 Lah numbers</pre>

=={{header|Wren}}==
{{libheader|Wren-str}}
{{libheader|Wren-set}}
{{libheader|Wren-fmt}}
This assumes that a one letter word is treated as a bigram. It also assumes that all bigrams are matched whether duplicates or not.

The results on this basis are the same as the Raku example.
<syntaxhighlight lang="ecmascript">import "io" for File
import "./str" for Str
import "./set" for Bag
import "./fmt" for Fmt

var bigrams = Fn.new { |phrase|
var words = Str.splitNoEmpty(phrase, " ")
var res = []
for (word in words) {
var chars = Str.lower(word).toList
if (chars.count == 1) {
res.add(chars[0])
} else {
for (i in 0...chars.count-1) {
res.add(chars[i] + chars[i+1])
}
}
}
return res
}

var sorensen = Fn.new { |a, b|
var abi = Bag.new(bigrams.call(a))
var bbi = Bag.new(bigrams.call(b))
var common = abi.intersect(bbi)
return 2 * common.count / (abi.count + bbi.count)
}

var fileName = "rc_tasks_2022_09_24.txt" // local copy
var tasks = File.read(fileName).trimEnd().split("\n")
var tc = tasks.count
var tests = [
"Primordial primes", "Sunkist-Giuliani formula", "Sieve of Euripides", "Chowder numbers"
]
var sdis = List.filled(tc, null)
for (test in tests) {
for (i in 0...tasks.count) sdis[i] = [tasks[i], sorensen.call(tasks[i], test)]
var top5 = sdis.sort { |e1, e2| e1[1] >= e2[1] }.take(5).toList
System.print("%(test) >")
for (e in top5) Fmt.print(" $f $s", e[1], e[0])
System.print()
}</syntaxhighlight>

{{out}}
<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 Four sides of square
0.461538 Sieve of Pritchard
0.413793 Sieve of Eratosthenes
0.400000 Piprimes
0.384615 Sierpinski curve

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