-
Notifications
You must be signed in to change notification settings - Fork 1
/
gather-props.hs
209 lines (161 loc) · 6.75 KB
/
gather-props.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
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
{-# LANGUAGE ScopedTypeVariables #-}
{-# LANGUAGE RecordWildCards #-}
module Main where
import Control.Monad
import Text.HTML.TagSoup
import qualified Data.ByteString.Lazy as L
import Control.Applicative
import Data.Monoid
import Control.Arrow
import Prose.Properties.Types
import Data.Map ((!))
import Data.Char (chr,toLower)
import Data.List (intersperse,intercalate)
import Data.List.Split
import qualified Data.Map as M
import qualified Data.Set as S
import Control.Exception
import Data.Binary as Bin
import Data.Tuple (swap)
import Lens.Micro
{-
propsMap :: [(String, String -> CharProps -> CharProps)]
propsMap = [
("na", set name),
("gc", set generalCategory . read),
("NFD_QC", set nfd_qc . readYN),
("NFKD_QC", set nfkd_qc . readYN),
("NFC_QC", set nfc_qc . readQCValue),
("NFKC_QC", set nfkc_qc . readQCValue),
("Upper", set upper . readYN),
("OUpper", set otherUpper . readYN),
("Lower", set lower . readYN),
("OLower", set otherLower . readYN),
("ccc", set combiningClass . read),
("Dash", set dash . readYN),
("Hyphen", set hyphen . readYN),
("QMark", set quotationMark . readYN),
("Term", set terminalPunctuation . readYN)
]
-}
type PropertiesDB = [(Char,CharProps)]
toProp :: Tag String -> PropertiesDB
toProp (TagOpen _ psm) = [ (ix, CharProps{..}) | ix <- ixs ]
where _name = ps!"na"
_generalCategory = read $ ps!"gc"
ixs = case (fmap readCodePoint . (`M.lookup` ps)) <$> ["cp","first-cp","last-cp"] of
[Just x, Nothing, Nothing] -> [x]
[Nothing, Just a, Just b] -> [a..b]
[_nfd_qc, _nfkd_qc] = readYN . (ps!) <$> ["NFD_QC", "NFKD_QC"]
[_nfc_qc, _nfkc_qc] = readQCValue . (ps!) <$> ["NFC_QC", "NFKC_QC"]
[_upper, _otherUpper, _lower, _otherLower] = readYN . (ps!) <$> ["Upper","OUpper","Lower","OLower"]
_combiningClass = read $ ps!"ccc"
_dash = readYN $ ps!"Dash"
_hyphen = readYN $ ps!"Hyphen"
_quotationMark = readYN $ ps!"QMark"
_terminalPunctuation = readYN $ ps!"Term"
_diactric = readYN $ ps!"Dia"
_extender = readYN $ ps!"Ext"
_decomposition = readDecomp $ ps!"dm"
_decompositionType = readDecompType $ ps!"dt"
_fullDecompositionExclusion = readYN $ ps!"Comp_Ex"
ps = M.fromList psm
readQCValue :: String -> QCValue
readQCValue "Y" = QCYes
readQCValue "N" = QCNo
readQCValue "M" = QCMaybe
readYN :: String -> Bool
readYN "Y" = True
readYN "N" = False
readCodePoint :: String -> Char
readCodePoint = chr . read . ("0x"++)
readDecomp "#" = DCSelf
readDecomp s = DC . map readCodePoint $ words s
readDecompType "none" = Nothing
readDecompType s = Just (table!s)
where table = M.fromList [
("can",DTCanonical),("com",DTCompat),("enc",DTCircle),
("fin",DTFinal),("font",DTFont),("fra",DTFraction),
("init",DTInitial),("iso",DTIsolated),("med",DTMedial),
("nar",DTNarrow),("nb",DTNoBreak),("sml",DTSmall),
("sqr",DTSquare),("sub",DTSub),("sup",DTSuper),
("vert",DTVertical),("wide",DTWide)
]
genDecompositions props = do
gen "DecompD" "Map Char [Char]" . show . decompositionsD $ props
gen "DecompKD" "Map Char [Char]" . show . decompositionsKD $ props
decompositionsD, decompositionsKD :: PropertiesDB -> M.Map Char [Char]
decompositionsD = filteredMap ((== Just DTCanonical) . _decompositionType)
decompositionsKD = filteredMap (const True)
filteredMap :: (CharProps -> Bool) -> PropertiesDB -> M.Map Char [Char]
filteredMap filt = M.fromList
. map (\(c,pro) -> (c, decompositionOf c (_decomposition pro)))
. filter (\(_,pro) -> filt pro
&& _decomposition pro /= DCSelf)
genCompositions props = gen "Comp" "Map [Char] Char" (show dat)
where dat = decomposeToComposeMap (fullDecExclData props) (decompositionsD props)
dat :: M.Map [Char] Char
decomposeToComposeMap ex = M.fromList
. map swap
. filter (\(k,_) -> S.notMember k ex)
. filter (\(k,v) -> length v == 1 || length v == 2)
. M.toList
genCCC = gen "CombiningClass" "Map Char Int" . show . dat
where dat :: PropertiesDB -> M.Map Char Int
dat = M.fromList
. filter (\(_,cc) -> cc /= 0)
. map (second _combiningClass)
genFullDecExcl = gen "FullDecompExclusions" "Set Char" . show . fullDecExclData
fullDecExclData = S.fromList
. map fst
. filter (_fullDecompositionExclusion . snd)
gen :: String -> String -> String -> IO ()
gen name typ dat = writeFile ("Prose/Properties/" <> name <> ".hs") $
unlines [
"-- autogenerated by gather-props",
"module Prose.Properties."<>name<>" where",
"import Data." <> head (words typ),
"import Prose.Properties.Types",
name' <> " :: " <> typ,
name' <> "=" <> dat
]
where name' = map toLower name
main = do props <- (readSavedProps
`catch` \(e::IOException) ->
saveProps)
print $ length props
genDecompositions props
genCCC props
genFullDecExcl props
genCompositions props
readSavedProps = Bin.decode <$> L.readFile "properties.data"
saveProps = do
input <- readFile "data/ucd.all.flat.xml"
let parsed = parseTags input
let props = concatMap toProp . filter isChar $ parsed :: [(Char,CharProps)]
writeUCD props
writeBinary props
pure props
where isChar (TagOpen "char" _) = True
isChar _ = False
instance Binary CharProps
instance Binary GeneralCategory
instance Binary QCValue
instance Binary Decomp
instance Binary DecompType
writeBinary props = L.writeFile "properties.data" (Bin.encode props)
writeUCD props = do
let chunks = chunksOf 1024 props
writeFile file $ unlines ["module Prose.UCD (ucd) where",
"import Prose.Properties.Types"]
forM_ (zip [1..] chunks) $ \(i,p) -> writePart i p
appendFile file $ "ucd = concat["
<> intercalate "++" ["ucd"<>show i | i<-[1..length chunks]]
<> "]"
where file = "Prose/UCD.hs"
writePart :: Int -> [(Char,CharProps)] -> IO ()
writePart n pp = appendFile file $ concat [
"ucd"<>show n<>" :: [(Char,CharProps)]\n",
"ucd"<>show n<>" = \n",
" " <> show pp,
"\n"]