-
Notifications
You must be signed in to change notification settings - Fork 31
/
getratingstsv.hs
executable file
·86 lines (73 loc) · 3.16 KB
/
getratingstsv.hs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
#!/usr/bin/env -S stack script --resolver lts-20.13 --package pretty-show --package extra --verbosity error
-- Dumb ever-growing script to extract judges' numeric ratings from README.md as TSV for easier processing.
-- Run this in the tiny-games-hs-private repo. Writes several "ratings*.tsv" files.
-- Depends on README.md's structure, keep synced.
{-# LANGUAGE RecordWildCards #-}
import Control.Monad
import Data.Bifunctor (first)
import Data.Char
import Data.List
import Data.Maybe
import Data.Ord
import Data.List.Extra (groupOn)
-- import Debug.Trace
import System.IO
import Text.Read
import Text.Show.Pretty
data Category = Prelude | Base | Default | Hackage deriving (Eq, Ord, Read, Show)
readCategory s = fromMaybe (error $ "could not read "<>show s) $ readMaybe $ capitalise s
showCategory = uncapitalise . show
capitalise s = a<>b where (a,b) = first (map toUpper) $ splitAt 1 s
uncapitalise s = a<>b where (a,b) = first (map toLower) $ splitAt 1 s
data Rating=Rating {
rcategory :: Category,
rgame :: String,
rreviewer :: String,
rscore :: Int
} deriving (Show)
showRatingTSV Rating{..} = intercalate "\t" [showCategory rcategory, rgame, rreviewer, show rscore]
main = do
f <- readFile "README.md"
let
-- all non-empty lines after "## Results" to file end
ls =
filter (not.null) $
init $ takeWhile ((/=) "<!-- END -->") $
drop 1 $ dropWhile ((/=) "<!-- START -->") $
lines f
ratingsbycategory =
flip concatMap (splitLinesByCategory ls) $ \(cat,ls1) ->
flip concatMap (splitLinesByGame ls1) $ \(game,ls2) ->
flip map (splitLinesByReviewer ls2) $ \(reviewer,ls3) ->
Rating (readCategory cat) game reviewer (getScore ls3)
ratingsbycategoryandscore = sortOn (\r -> (rcategory r, negate $ rscore r)) ratingsbycategory
-- withFile "ratings.tsv" WriteMode $ \h -> mapM_ (hPutStrLn h . showRatingTSV) ratingsbycategory
withFile "ratings.tsv" WriteMode $ \h -> mapM_ (hPutStrLn h . showRatingTSV) ratingsbycategoryandscore
splitLinesByCategory :: [String] -> [(String,[String])]
splitLinesByCategory [] = []
splitLinesByCategory (catl:ls) =
(cat, ls1) : splitLinesByCategory ls2
where
cat = map toLower $ dropWhile (not.isAlpha) catl
(ls1,ls2) = break ("### " `isPrefixOf`) ls
splitLinesByGame :: [String] -> [(String,[String])]
splitLinesByGame [] = []
splitLinesByGame (gamel:ls) =
(game, ls1) : splitLinesByGame ls2
where
game = map toLower $ takeWhile (not.isSpace) $ dropWhile (not.isAlpha) gamel
(ls1,ls2) = break ("#### " `isPrefixOf`) ls
splitLinesByReviewer :: [String] -> [(String,[String])]
splitLinesByReviewer [] = []
splitLinesByReviewer (reviewerl:ls) =
(reviewer, ls1) : splitLinesByReviewer ls2
where
reviewer = takeWhile (not.(==':')) $ dropWhile (=='*') reviewerl
(ls1,ls2) = break ("**" `isPrefixOf`) ls
-- Get the numeric score from a "Rating:" line, normally 0-10,
-- or -1 if there's no numeric score.
getScore :: [String] -> Int
getScore [] = error "getScore needs lines"
getScore ls = maybe (-1) id $ do
l <- listToMaybe $ filter ("Rating:" `isPrefixOf`) ls
readMaybe $ takeWhile isDigit $ dropWhile (\c -> not $ isDigit c || c=='/') l :: Maybe Int