]> gitweb.factorcode.org Git - factor.git/blob - extra/benchmark/knucleotide/knucleotide.factor
scryfall: parse mtga deck format
[factor.git] / extra / benchmark / knucleotide / knucleotide.factor
1 ! Copyright (C) 2008, 2010 Slava Pestov.
2 ! See https://factorcode.org/license.txt for BSD license.
3 USING: ascii assocs formatting grouping io io.encodings.ascii
4 io.files kernel math math.statistics sequences ;
5 IN: benchmark.knucleotide
6
7 CONSTANT: knucleotide-in "vocab:benchmark/knucleotide/knucleotide-input.txt"
8
9 : discard-lines ( -- )
10     readln
11     [ ">THREE" head? [ discard-lines ] unless ] when* ;
12
13 : read-input ( -- input )
14     discard-lines
15     ">" read-until drop
16     CHAR: \n swap remove >upper ;
17
18 : handle-table ( inputs n -- )
19     clump
20     [ sorted-histogram reverse ] [ length ] bi
21     '[
22         [ first write bl ]
23         [ second 100 * _ /f "%.3f" printf nl ] bi
24     ] each ;
25
26 : handle-n ( input x -- )
27     [ nip ] [ length clump histogram ] 2bi at 0 or "%d\t" printf ;
28
29 : process-input ( input -- )
30     [ 1 handle-table nl ]
31     [ 2 handle-table nl ]
32     [
33         { "GGT" "GGTA" "GGTATT" "GGTATTTTAATT" "GGTATTTTAATTTATAGT" }
34         [ [ handle-n ] keep print ] with each
35     ]
36     tri ;
37
38 : knucleotide-benchmark ( -- )
39     knucleotide-in
40     ascii [ read-input ] with-file-reader
41     process-input ;
42
43 MAIN: knucleotide-benchmark