/
Solution.hs
82 lines (57 loc) · 2.53 KB
/
Solution.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
{-# LANGUAGE OverloadedStrings, QuasiQuotes #-}
module Y2017.M11.D09.Solution where
{--
Okay, today, back to reading from the database. We have a new set of articles
with new recommendations, but with just the id,title, and score. We have the
keyphrases (which we will need to store in the database), so we need the other
data contained in our database. We also have the data structure we need as a
result, so: let's go get us some data!
--}
import Control.Arrow ((&&&))
import qualified Data.Map as Map
import Database.PostgreSQL.Simple
import Database.PostgreSQL.Simple.SqlQQ
import Database.PostgreSQL.Simple.FromRow
-- below imports available via 1HaskellADay git repository
import Store.SQL.Connection
import Y2017.M11.D03.Solution -- for KeyPhrase
import Y2017.M11.D06.Solution -- for Value and Score
import Y2017.M11.D07.Solution -- marrying Recommended articles to scores
import Y2017.M11.D08.Solution -- attaching keyphrases to recommended articles
-- here's our SQL to get our structure
recommendsStmt :: Query
recommendsStmt =
[sql|SELECT a.id,a.title,s.summary,a.author,a.publish_dt,a.view_count
FROM article a
LEFT JOIN article_summary s ON s.article_id = a.id
WHERE a.id IN ?|]
fetchRecommend :: Connection -> [Integer] -> IO [Recommend]
fetchRecommend conn = query conn recommendsStmt . Only . In
-- of course, we need the row instance of Recommend:
instance FromRow Recommend where
fromRow = Rec <$> (show <$> (field :: RowParser Integer))
<*> field <*> field <*> field <*> field <*> field
-- Now that we have our data on the recommendations, the scores and the keywords
-- (see imports above): generate the JSON as before
-- step 1: read in article-set.csv using definitions from Y2017.M11.D07.Exercise
articleSet :: FilePath
articleSet = "Y2017/M11/D09/article-set.csv"
{--
>>> scores <- readScoreFile articleSet
step 2: read in the keywords, as before, using Y2017.M11.D03.Exercise's
definitions
>>> kws <- readCompressedKeywords "Y2017/M11/D03/refinedEr_kws.txt.gz"
step 3: read in the article-data using the SQL here.
>>> connectInfo
ConnectInfo {connectHost = "...",...}
>>> conn <- connect it
>>> arts <- fetchRecommend conn (Data.Map.keys scores)
>>> length arts
26
step 4: generate the pretty JSON, using Y2017.M11.D08.Exercise's definitions
(first I need to convert the arts into a Map:)
>>> recs = Map.fromList (map (((read . recIdx) :: Recommend -> Integer) &&& id) arts)
(now I can JSONify everyting)
>>> saveRecs "Y2017/M11/D09/new-recs.json" kws (marry recs scores)
There you go!
--}