]> Git — Sourcephile - gargantext.git/blob - src/Gargantext/Core.hs
[VERSION] +1 to 0.0.6.9.8.7
[gargantext.git] / src / Gargantext / Core.hs
1 {-|
2 Module : Gargantext.Core
3 Description : Supported Natural language
4 Copyright : (c) CNRS, 2017-Present
5 License : AGPL + CECILL v3
6 Maintainer : team@gargantext.org
7 Stability : experimental
8 Portability : POSIX
9
10 -}
11
12 {-# LANGUAGE DeriveAnyClass #-}
13
14 module Gargantext.Core
15 where
16
17 import Data.Text (Text, pack)
18 import Data.Aeson
19 import Data.Either(Either(Left))
20 import Data.Hashable (Hashable)
21 import Data.Morpheus.Types (GQLType)
22 import Data.Swagger
23 import GHC.Generics (Generic)
24 import Gargantext.Prelude
25 import Servant.API
26
27 ------------------------------------------------------------------------
28 -- | Language of a Text
29 -- For simplicity, we suppose text has an homogenous language
30 --
31 -- - EN == english
32 -- - FR == french
33 -- - DE == deutch
34 -- - IT == italian
35 -- - ES == spanish
36 -- - PL == polish
37 -- - CN == chinese
38 --
39 -- ... add your language and help us to implement it (:
40
41 -- | All languages supported
42 -- NOTE: Use international country codes
43 -- https://en.wikipedia.org/wiki/List_of_ISO_3166_country_codes
44 data Lang = EN | FR | DE | IT | PL | ES | CN | All
45 deriving (Show, Eq, Ord, Enum, Bounded, Generic, GQLType)
46
47 instance ToJSON Lang
48 instance FromJSON Lang
49 instance ToSchema Lang where
50 declareNamedSchema = genericDeclareNamedSchemaUnrestricted defaultSchemaOptions
51 instance FromHttpApiData Lang
52 where
53 parseUrlPiece "EN" = pure EN
54 parseUrlPiece "FR" = pure FR
55 parseUrlPiece "DE" = pure DE
56 parseUrlPiece "ES" = pure ES
57 parseUrlPiece "IT" = pure IT
58 parseUrlPiece "PL" = pure PL
59 parseUrlPiece "CN" = pure CN
60 parseUrlPiece "All" = pure All
61 parseUrlPiece _ = Left "Unexpected value of Lang"
62 instance ToHttpApiData Lang where
63 toUrlPiece = pack . show
64 instance Hashable Lang
65
66 allLangs :: [Lang]
67 allLangs = [minBound ..]
68
69 class HasDBid a where
70 toDBid :: a -> Int
71 fromDBid :: Int -> a
72
73 -- NOTE: We try to use numeric codes for countries
74 -- https://en.wikipedia.org/wiki/List_of_ISO_3166_country_codes
75 -- https://en.wikipedia.org/wiki/ISO_3166-1_numeric#004
76 instance HasDBid Lang where
77 toDBid All = 0
78 toDBid FR = 1
79 toDBid EN = 2
80 toDBid DE = 276
81 toDBid ES = 724
82 toDBid IT = 380
83 toDBid PL = 616
84 toDBid CN = 156
85
86 fromDBid 0 = All
87 fromDBid 1 = FR
88 fromDBid 2 = EN
89 fromDBid 276 = DE
90 fromDBid 724 = ES
91 fromDBid 380 = IT
92 fromDBid 616 = PL
93 fromDBid 156 = CN
94 fromDBid _ = panic "HasDBid lang, not implemented"
95
96 ------------------------------------------------------------------------
97 data NLPServerConfig = NLPServerConfig
98 { server :: !PosTagAlgo
99 , url :: !URI }
100 deriving (Show, Eq)
101 ------------------------------------------------------------------------
102 type Form = Text
103 type Lem = Text
104 ------------------------------------------------------------------------
105 data PosTagAlgo = CoreNLP | JohnSnowServer | Spacy
106 deriving (Show, Read, Eq, Ord, Generic)
107
108 instance Hashable PosTagAlgo
109
110 instance HasDBid PosTagAlgo where
111 toDBid CoreNLP = 1
112 toDBid JohnSnowServer = 2
113 toDBid Spacy = 3
114 fromDBid 1 = CoreNLP
115 fromDBid 2 = JohnSnowServer
116 fromDBid 3 = Spacy
117 fromDBid _ = panic "HasDBid posTagAlgo : Not implemented"