-
Notifications
You must be signed in to change notification settings - Fork 1
/
parseStories.hs
265 lines (223 loc) · 9.79 KB
/
parseStories.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
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
{-# LANGUAGE DeriveGeneric, RecordWildCards, TupleSections #-}
import Control.Monad (join)
import GHC.Generics
import Data.Char (ord)
import Prelude hiding (id)
import qualified Data.Csv as Csv
import Text.HTML.TagSoup
import qualified Data.HashMap.Strict as M
import qualified Data.Text as T
import qualified Data.Text.IO as TIO
import qualified Data.ByteString.Lazy.Char8 as B
import Data.List (nub, elemIndex, sortOn, sort)
import Data.Vector (toList)
import qualified Data.Aeson as J
import Debug.Trace
data Story = Story
{ id :: String
, img :: String
, title :: String
, tags :: [String]
, level :: Int
, completed :: Int
, definitions :: [Definition]
, date :: String
, content :: String
, popups :: [Definition]
} deriving (Show, Generic)
data Definition = Definition
{ word_ :: String
, index :: Int
} deriving (Eq, Ord, Show, Generic)
instance J.ToJSON Story where
toJSON = J.genericToJSON J.defaultOptions
toEncoding = J.genericToEncoding J.defaultOptions
instance J.ToJSON Definition where
toJSON = J.genericToJSON J.defaultOptions
toEncoding = J.genericToEncoding J.defaultOptions
parseSection :: [Tag String] -> Story
parseSection s =
let
cols = partitions (~== "<td>") s
[image, title, tag1, tag2, tag3, level, completed, date, _] = reverse $ foldl accTxt [] (take 9 cols)
accTxt ss t = trim (innerText t) : ss
image' = parseImage (head cols)
ts = filter (not . null) [tag1, tag2, tag3]
rem = drop 9 cols
id_ = parseIdFromLink (head rem)
(content, defs, popups) = parseContent (head (tail rem))
in
Story id_ image' title ts (read level) (read completed) (nub defs) (parseDate date) content popups
parseDefinition :: String -> [Tag String] -> Maybe Definition
parseDefinition d ts = case d `elemIndex` ["define","define2","define3","define4"] of
Just i -> case innerText $ takeWhile (~/= TagClose "span") ts of
"Words in pop-ups" -> Nothing -- There is one story where this happens
txt -> Just $ Definition (trim txt) i
Nothing -> error ("Unhandled span class " ++ d)
parsePopUps :: [Tag String] -> [Definition]
parsePopUps [] = error "End of input while parsing popup words"
parsePopUps (t:ts) = case t of
TagOpen "span" (("class", d):_cs) ->
case parseDefinition d ts of
Just defn -> defn : parsePopUps (tail ts)
Nothing -> parsePopUps ts
TagClose "td" -> []
_ -> parsePopUps ts
parseContent :: [Tag String] -> (String, [Definition], [Definition])
parseContent ts = go ("", [], []) (drop 1 ts)
where
go acc [] = acc
go acc@(content, defs, popups) (t:ts) =
case t of
TagOpen "span" [("style", _)] -> go acc ts
TagOpen "font" _ -> go acc ts
TagClose "font" -> go acc ts
TagOpen "div" _ -> go acc ts
TagClose "div" -> go acc ts
TagOpen "center" _ -> go acc ts
TagClose "center" -> go acc ts
TagOpen "o:p" _ -> go acc ts
TagClose "o:p" -> go acc ts
TagClose "span" -> go acc ts
TagOpen "span" (("class", "apple-converted-space"):_) -> go acc ts
TagOpen "span" (("class", d):_cs) ->
let
newAcc = case parseDefinition d ts of
Nothing -> acc
Just defn -> (content ++ word_ defn, defn : defs, popups)
in
go newAcc (dropWhile (~/= TagClose "span") ts)
TagOpen "span" [] -> go acc ts
TagOpen "em" _ -> go (content ++ "_", defs, popups) ts
TagOpen "p" _ -> go (content ++ "\n\n", defs, popups) ts
TagOpen "a" _ ->
let
url = fromAttrib "href" t
inside = takeWhile (~/= TagClose "a") ts
lnk = "[" ++ innerText inside ++ "](" ++ url ++ ")"
in
go (content ++ lnk, defs, popups) (tail $ dropWhile (~/= TagClose "a") ts)
TagClose"em" -> go (content ++ "_ ", defs, popups) ts
TagOpen "strong\"" _ -> go acc (drop 2 ts) -- specific tag error in J Cooper story
TagOpen "strong" _ -> case ts of
(TagText s : TagClose "strong" : ts') -> case trim s of
"Words in pop-ups" -> (content, defs, parsePopUps ts')
_ -> go (content ++ "__" ++ s ++ "__", defs, popups) ts'
_ -> case head ts of
TagOpen "span" _ -> go acc (tail ts) -- style span within strong
_ -> error ("open strong tag with weird successors" ++ show (head ts))
TagOpen "img" _ ->
let
src = fromAttrib "src" t
img = "![fixme](" ++ src ++ ")"
in
go (content ++ img, defs, popups) (tail ts)
TagClose "img" -> go acc ts
TagOpen "ul" _ -> go acc (dropWhile (~/= "<li>") ts)
TagOpen "li" _ ->
let
txt = innerText $ takeWhile (~/= TagClose "li") ts
in
go (content ++ "\n+" ++ txt, defs, popups) (dropWhile (~/= TagClose "li") ts)
TagClose "li" -> go acc ts
TagClose "ul" -> go acc ts
TagClose "td" -> (trim content, defs, popups)
_ -> go (content ++ renderTags [t], defs, popups) ts
parseImage :: [Tag String] -> String
parseImage = takeWhile (/= ')') . drop 23 . trim . fromAttrib "style" . head . dropWhile (~/= TagOpen "div" [])
parseIdFromLink :: [Tag String] -> String
parseIdFromLink = tail . dropWhile (/= '=') . fromAttrib "href" . head . dropWhile (~/= "<a>")
parseDate :: String -> String
parseDate = trim . take 19
trim :: String -> String
trim = T.unpack . T.strip . T.pack
findStartOfStories :: [Tag String] -> [Tag String]
findStartOfStories = dropWhile (~/= TagOpen "tr" []) . dropWhile (~/= TagOpen "tbody" [])
parseSections = partitions (~== "<tr>") . findStartOfStories . parseTags
decodeStories = do
htmlStories <- readFile "allstories.txt"
let secs = parseSections htmlStories
return $ map parseSection secs
printStories stories = do
putStrLn "{ \"stories\": "
B.putStr $ J.encode stories
putStrLn "\n}"
main :: IO ()
main = do
dict <- makeSciDict
stories <- decodeStories
let subDefs = mergeDuplicates $ nub $ sort $ join $ map (subDefinitions dict) stories
newDict = sortOn (T.toLower . fst) $ addSubDefsToDict subDefs (M.toList dict)
B.putStr $ J.encode $ M.fromList newDict
addSubDefsToDict :: [(Definition, [Definition])] -> [(T.Text, [T.Text])] -> [(T.Text, [(T.Text, [(T.Text, Int)])])]
addSubDefsToDict _ [] = []
addSubDefsToDict defs ((w, ds):rem) =
let
toTuple d = (T.pack $ word_ d, index d)
matchingDefs = map (map toTuple . snd) $ filter ((== w) . T.pack . word_ . fst) defs
in
(w, zip ds (matchingDefs ++ [[],[],[],[],[]] )) : addSubDefsToDict defs rem
mergeDuplicates :: [(Definition, [Definition])] -> [(Definition, [Definition])]
mergeDuplicates [] = []
mergeDuplicates [d] = [d]
mergeDuplicates (x@(d,ds):y@(d1,d1s):rem) =
if d == d1
then mergeDuplicates $ (d, nub (ds ++ d1s)) : rem
else x : mergeDuplicates (y : rem)
subDefinitions :: M.HashMap T.Text [T.Text] -> Story -> [(Definition, [Definition])]
subDefinitions dict story = filter (not . null . snd) $ defPopups ++ popupsForPopups
where
defPopups = map (`popupsForDefinition` storyPopups) (definitions story)
storyPopups = popups story
go d = popupsForDefinition d storyPopups
popupsForPopups = map (\p -> popupsForDefinition p (filter (/= p) storyPopups)) storyPopups
popupsForDefinition d ps =
let
wordToLookup = T.pack (word_ d)
dictEntry = case M.lookup wordToLookup dict of
Nothing -> error $ "No dict entry for " ++ show (word_ d)
Just e -> if index d >= length e
then error $ "There is no entry for " ++ show (word_ d) ++ " at index " ++ show (index d) ++ " in " ++ show e
else e !! index d
dPopups = filter (\p -> T.pack (word_ p) `T.isInfixOf` dictEntry) ps
in
(d, dPopups)
myCSVOptions = Csv.defaultDecodeOptions {
Csv.decDelimiter = fromIntegral (ord '\t')
}
makeSciDict = do
bytes <- B.readFile "scidict.txt"
return $ case Csv.decodeWith myCSVOptions Csv.NoHeader bytes of
Left e -> error e
Right v -> foldl insertDef M.empty (Data.List.sortOn word (toList v))
insertDef :: M.HashMap T.Text [T.Text] -> Def -> M.HashMap T.Text [T.Text]
insertDef dict Def {..} =
let
word' = T.strip word
(w, i) = case T.unpack $ T.takeEnd 2 word' of
" 2" -> (T.dropEnd 2 word', 1)
" 3" -> (T.dropEnd 2 word', 2)
" 4" -> (T.dropEnd 2 word', 3)
" 5" -> (T.dropEnd 2 word', 4)
_ -> (word', 0)
entry = M.lookup w dict
in
case entry of
Just [] -> error $ "Empty dict entry for " ++ T.unpack w
Nothing ->
if i > 0
then error $ "non zero index defn but no existing entry for " ++ (show word) ++ (show def)
else M.insert w [def] dict
Just ds ->
if i > 0 && length ds == i
then M.insert w (reverse $ def : reverse ds) dict
else if i == 0 && length ds == 1
then M.insert w [def] dict -- overwrite the existing entry
else error $ "Not enough preceding entries for " ++ T.unpack w ++ " " ++ show i ++ (show ds)
data Def = Def
{ word :: T.Text
, def :: T.Text
} deriving (Eq, Show, Generic)
instance Csv.FromRecord Def
instance Csv.ToRecord Def
instance J.FromJSON Def