]> Git — Sourcephile - gargantext.git/blob - src/Gargantext/Text/Parsers/Isidore.hs
[Text][Parsers][Isidore] acceptable query for next step (typing the result).
[gargantext.git] / src / Gargantext / Text / Parsers / Isidore.hs
1 {-# LANGUAGE NoImplicitPrelude #-}
2 {-# LANGUAGE ScopedTypeVariables #-}
3 {-# LANGUAGE OverloadedStrings #-}
4
5 module Gargantext.Text.Parsers.Isidore where
6
7 import Gargantext.Prelude
8 import Database.HSparql.Connection
9 import Database.HSparql.QueryGenerator
10
11 -- import Data.RDF hiding (triple)
12 import Data.Text hiding (groupBy)
13
14 import Control.Lens hiding (contains)
15 import Data.ByteString.Lazy (ByteString)
16 import Prelude (String)
17 import Network.Wreq
18
19 route :: EndPoint
20 route = "https://isidore.science/sparql/"
21
22 selectQueryRaw' :: String -> String -> IO (Response ByteString)
23 selectQueryRaw' uri q = getWith opts uri
24 where
25 opts = defaults & header "Accept" .~ ["application/sparql-results+xml"]
26 & header "User-Agent" .~ ["gargantext-hsparql-client"]
27 & param "query" .~ [Data.Text.pack q]
28
29 isidoreGet :: Text -> IO (Maybe [[BindingValue]])
30 isidoreGet q = do
31 let s = createSelectQuery $ isidoreSelect q
32 putStrLn s
33 r <- selectQueryRaw' route s
34 putStrLn $ show $ r ^. responseStatus
35 pure $ structureContent $ r ^. responseBody
36 -- res <- selectQuery route $ simpleSelect q
37 -- pure res
38
39 isidoreSelect :: Text -> Query SelectQuery
40 isidoreSelect q = do
41 -- See Predefined Namespace Prefixes:
42 -- https://isidore.science/sparql?nsdecl
43 isidore <- prefix "isidore" (iriRef "http://www.rechercheisidore.fr/class/")
44 rdf <- prefix "rdf" (iriRef "http://www.w3.org/1999/02/22-rdf-syntax-ns#")
45 dcterms <- prefix "dcterms" (iriRef "http://purl.org/dc/terms/")
46 dc <- prefix "dc" (iriRef "http://purl.org/dc/elements/1.1/")
47 --iso <- prefix "fra" (iriRef "http://lexvo.org/id/iso639-3/")
48 --ore <- prefix "ore" (iriRef "http://www.openarchives.org/ore/terms/")
49 --bif <- prefix "bif" (iriRef "bif:")
50
51 link <- var
52 title <- var
53 date <- var
54 abstract <- var
55 authors <- var
56 source <- var
57 langDoc <- var
58 publisher <- var
59 --langFr <- var
60 --agg <- var
61
62 triple_ link (rdf .:. "type") (isidore .:. "BibliographicalResource")
63 triple_ link (dcterms .:. "title") title
64 triple_ link (dcterms .:. "date") date
65 triple_ link (dcterms .:. "creator") authors
66 triple_ link (dcterms .:. "language") langDoc
67 triple_ link (dc .:. "description") abstract
68 --triple_ link (ore .:. "isAggregatedBy") agg
69 --triple_ agg (dcterms .:. "title") title
70
71 optional_ $ triple_ link (dcterms .:. "source") source
72 optional_ $ triple_ link (dcterms .:. "publisher") publisher
73
74 -- TODO FIX BUG with (.||.) operator
75 --filterExpr $ (.||.) (contains title q) (contains title q)
76 filterExpr_ (containsWith title q) -- (contains abstract q)
77 --filterExpr (containsWith abstract q) -- (contains abstract q)
78
79 -- TODO FIX filter with lang
80 --filterExpr $ langMatches title (str ("fra" :: Text))
81 --filterExpr $ (.==.) lang (str ("http://lexvo.org/id/iso639-3/fra" :: Text))
82
83 orderNextDesc date
84 limit_ 10
85 distinct_
86 selectVars [link, date, langDoc, authors, source, publisher, title, abstract]