2 Module : Gargantext.Databse.Ngram
4 Copyright : (c) CNRS, 2017-Present
5 License : AGPL + CECILL v3
6 Maintainer : team@gargantext.org
7 Stability : experimental
10 Here is a longer description of this module, containing some
11 commentary with @some markup@.
14 {-# LANGUAGE Arrows #-}
15 {-# LANGUAGE FlexibleInstances #-}
16 {-# LANGUAGE FunctionalDependencies #-}
17 {-# LANGUAGE MultiParamTypeClasses #-}
18 {-# LANGUAGE NoImplicitPrelude #-}
19 {-# LANGUAGE TemplateHaskell #-}
21 module Gargantext.Database.Ngram where
24 import Data.Text (Text)
25 import Data.Maybe (Maybe)
26 import Data.Profunctor.Product.TH (makeAdaptorAndInstance)
27 import Control.Lens.TH (makeLensesWith, abbreviatedFields)
28 import qualified Database.PostgreSQL.Simple as PGS
33 import Data.List (find)
36 data NgramPoly id terms n = Ngram { ngram_id :: id
37 , ngram_terms :: terms
41 type NgramWrite = NgramPoly (Maybe (Column PGInt4))
45 type NgramRead = NgramPoly (Column PGInt4)
49 type Ngram = NgramPoly Int Text Int
51 $(makeAdaptorAndInstance "pNgram" ''NgramPoly)
52 $(makeLensesWith abbreviatedFields ''NgramPoly)
54 ngramTable :: Table NgramWrite NgramRead
55 ngramTable = Table "ngrams" (pNgram Ngram { ngram_id = optional "id"
56 , ngram_terms = required "terms"
57 , ngram_n = required "n"
61 queryNgramTable :: Query NgramRead
62 queryNgramTable = queryTable ngramTable
65 --selectUsers :: Query UserRead
66 --selectUsers = proc () -> do
67 -- --user@(i, p, ll, is, un, fn, ln, m, iff, ive, dj) <- queryUserTable -< ()
68 -- row@(User i p ll is un fn ln m iff ive dj) <- queryUserTable -< ()
69 -- O.restrict -< i .== 1
70 -- --returnA -< User i p ll is un fn ln m iff ive dj
74 findWith :: (Eq a1, Foldable t) => (a -> a1) -> a1 -> t a -> Maybe a
75 findWith f t = find (\x -> f x == t)
77 --userWithUsername :: Text -> [User] -> Maybe User
78 --userWithUsername t xs = userWith userUsername t xs
80 --userWithId :: Integer -> [User] -> Maybe User
81 --userWithId t xs = userWith userUserId t xs
83 -- | not optimized (get all ngrams without filters)
84 getNgrams :: PGS.Connection -> IO [Ngram]
85 getNgrams conn = runQuery conn queryNgramTable