Entropy/Narcissist: Difference between revisions

(→‎{{header|jq}}: def sum():)
 
Line 613:
'''Works with jq, the C implementation of jq'''
 
'''Works with gojq, the Go implementation of jq''' provided keys_unsorted is replaced by keys
 
'''Works with jaq, the Rust implementation of jq'''
 
The program assumes it will be presented to itself using
Line 620 ⟶ 622:
jq -sR -f entropy-narcissist.jq < entropy-narcissist.jq
</pre>
 
If your jq supports `keys_unsorted`, feel free to use it instead of `keys`.
<syntaxhighlight lang="jq">
def chars: explode[] | [.] | implode;
 
# bag of words
def bow(stream):
reduce stream as $word ({}; .[($word|tostring)] += 1);
 
def sum(s): reduce s as $x (0; .+$x);
 
length as $l
| bow(chars) as $m
| reduce sum(keys[] as $mk |keys_unsorted .[$k]) as $kc | $c * (0;$c|log2) )
$m[$k] as $c
| . + $c * ($c|log2) )
| ($l|log2) - ./$l
</syntaxhighlight>
{{output}}
<pre>
{{output}}
4.759188941960586
<pre>
4.796499915496963
</pre>
 
2,472

edits