2 Module : Gargantext.Database.Schema.NgramsPostag
3 Description : Ngram connection to the Database
4 Copyright : (c) CNRS, 2017-Present
5 License : AGPL + CECILL v3
6 Maintainer : team@gargantext.org
7 Stability : experimental
10 Ngrams connection to the Database.
14 {-# LANGUAGE Arrows #-}
15 {-# LANGUAGE FunctionalDependencies #-}
16 {-# LANGUAGE QuasiQuotes #-}
17 {-# LANGUAGE TemplateHaskell #-}
19 module Gargantext.Database.Schema.Ngrams
22 import Data.Hashable (Hashable)
23 import Codec.Serialise (Serialise())
24 import Control.Lens (over)
25 import Control.Monad (mzero)
27 import Data.Aeson.Types (toJSONKeyText)
28 import Data.Map (Map, fromList, lookup)
29 import Data.Text (Text, splitOn, pack, strip)
30 import Gargantext.Core.Types (TODO(..), Typed(..))
31 import Gargantext.Prelude
32 import Prelude (Functor)
33 import Servant (FromHttpApiData, parseUrlPiece, Proxy(..))
34 import Text.Read (read)
35 import Gargantext.Database.Types
36 import Gargantext.Database.Schema.Prelude
37 import qualified Database.PostgreSQL.Simple as PGS
43 data NgramsPoly id terms n = NgramsDB { _ngrams_id :: !id
44 , _ngrams_terms :: !terms
48 type NgramsWrite = NgramsPoly (Maybe (Column PGInt4))
52 type NgramsRead = NgramsPoly (Column PGInt4)
56 type NgramsReadNull = NgramsPoly (Column (Nullable PGInt4))
57 (Column (Nullable PGText))
58 (Column (Nullable PGInt4))
60 type NgramsDB = NgramsPoly Int Text Int
62 $(makeAdaptorAndInstance "pNgramsDb" ''NgramsPoly)
63 makeLenses ''NgramsPoly
66 ngramsTable :: Table NgramsWrite NgramsRead
67 ngramsTable = Table "ngrams" (pNgramsDb NgramsDB { _ngrams_id = optional "id"
68 , _ngrams_terms = required "terms"
69 , _ngrams_n = required "n"
73 -- | Main Ngrams Types
75 -- Typed Ngrams localize the context of the ngrams
76 -- ngrams in source field of document has Sources Type
77 -- ngrams in authors field of document has Authors Type
78 -- ngrams in text fields of documents has Terms Type (i.e. either title or abstract)
79 data NgramsType = Authors | Institutes | Sources | NgramsTerms
80 deriving (Eq, Show, Read, Ord, Enum, Bounded, Generic)
82 instance Serialise NgramsType
84 ngramsTypes :: [NgramsType]
85 ngramsTypes = [minBound..]
87 instance ToSchema NgramsType
89 declareNamedSchema = genericDeclareNamedSchema (unPrefixSwagger "_nre_")
92 newtype NgramsTypeId = NgramsTypeId Int
93 deriving (Eq, Show, Ord, Num)
95 instance ToField NgramsTypeId where
96 toField (NgramsTypeId n) = toField n
98 instance FromField NgramsTypeId where
99 fromField fld mdata = do
100 n <- fromField fld mdata
101 if (n :: Int) > 0 then return $ NgramsTypeId n
104 instance FromJSON NgramsType
105 instance FromJSONKey NgramsType where
106 fromJSONKey = FromJSONKeyTextParser (parseJSON . String)
108 instance ToJSON NgramsType
109 instance ToJSONKey NgramsType where
110 toJSONKey = toJSONKeyText (pack . show)
112 instance FromHttpApiData NgramsType where
113 parseUrlPiece n = pure $ (read . cs) n
115 instance ToParamSchema NgramsType where
116 toParamSchema _ = toParamSchema (Proxy :: Proxy TODO)
119 instance QueryRunnerColumnDefault (Nullable PGInt4) NgramsTypeId
121 queryRunnerColumnDefault = fieldQueryRunnerColumn
123 pgNgramsType :: NgramsType -> Column PGInt4
124 pgNgramsType = pgNgramsTypeId . ngramsTypeId
126 pgNgramsTypeId :: NgramsTypeId -> Column PGInt4
127 pgNgramsTypeId (NgramsTypeId n) = pgInt4 n
129 ngramsTypeId :: NgramsType -> NgramsTypeId
130 ngramsTypeId Authors = 1
131 ngramsTypeId Institutes = 2
132 ngramsTypeId Sources = 3
133 ngramsTypeId NgramsTerms = 4
135 fromNgramsTypeId :: NgramsTypeId -> Maybe NgramsType
136 fromNgramsTypeId id = lookup id
137 $ fromList [ (ngramsTypeId nt,nt)
138 | nt <- [minBound .. maxBound] :: [NgramsType]
141 ------------------------------------------------------------------------
142 ------------------------------------------------------------------------
143 -- | TODO put it in Gargantext.Core.Text.Ngrams
144 data Ngrams = UnsafeNgrams { _ngramsTerms :: Text
147 deriving (Generic, Show, Eq, Ord)
150 instance PGS.ToRow Ngrams where
151 toRow (UnsafeNgrams t s) = [toField t, toField s]
153 instance FromField Ngrams where
154 fromField fld mdata = do
155 x <- fromField fld mdata
158 text2ngrams :: Text -> Ngrams
159 text2ngrams txt = UnsafeNgrams txt' $ length $ splitOn " " txt'
164 ------------------------------------------------------------------------
165 -------------------------------------------------------------------------
166 -- | TODO put it in Gargantext.Core.Text.Ngrams
167 -- Named entity are typed ngrams of Terms Ngrams
169 NgramsT { _ngramsType :: NgramsType
171 } deriving (Generic, Show, Eq, Ord)
175 instance Functor NgramsT where
178 -----------------------------------------------------------------------
179 withMap :: Map Text NgramsId -> Text -> NgramsId
180 withMap m n = maybe (panic "withMap: should not happen") identity (lookup n m)
182 indexNgramsT :: Map Text NgramsId -> NgramsT Ngrams -> NgramsT (Indexed Int Ngrams)
183 indexNgramsT = fmap . indexNgramsWith . withMap
185 -- | TODO replace NgramsT whith Typed NgramsType Ngrams
186 indexTypedNgrams :: Map Text NgramsId
187 -> Typed NgramsType Ngrams
188 -> Typed NgramsType (Indexed Int Ngrams)
189 indexTypedNgrams = fmap . indexNgramsWith . withMap
191 indexNgrams :: Map Text NgramsId -> Ngrams -> Indexed Int Ngrams
192 indexNgrams = indexNgramsWith . withMap
194 indexNgramsWith :: (Text -> NgramsId) -> Ngrams -> Indexed Int Ngrams
195 indexNgramsWith f n = Indexed (f $ _ngramsTerms n) n