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
12 {-# LANGUAGE DeriveAnyClass #-}
14 module Gargantext.Core
18 import Data.Either(Either(Left))
19 import Data.Hashable (Hashable)
20 import Data.Morpheus.Types (GQLType)
22 import Data.Text (Text, pack)
23 import Data.Tuple.Extra (swap)
24 import GHC.Generics (Generic)
25 import Gargantext.Prelude
27 import Test.QuickCheck
28 import qualified Data.Map as Map
30 ------------------------------------------------------------------------
31 -- | Language of a Text
32 -- For simplicity, we suppose text has an homogenous language
42 -- ... add your language and help us to implement it (:
44 -- | All languages supported
45 -- NOTE: Use international country codes
46 -- https://en.wikipedia.org/wiki/List_of_ISO_3166_country_codes
59 deriving (Show, Eq, Ord, Enum, Bounded, Generic, GQLType)
62 instance FromJSON Lang
63 instance ToSchema Lang where
64 declareNamedSchema = genericDeclareNamedSchemaUnrestricted defaultSchemaOptions
65 instance FromHttpApiData Lang
67 parseUrlPiece "All" = pure All
68 parseUrlPiece "DE" = pure DE
69 parseUrlPiece "EL" = pure EL
70 parseUrlPiece "EN" = pure EN
71 parseUrlPiece "ES" = pure ES
72 parseUrlPiece "FR" = pure FR
73 parseUrlPiece "IT" = pure IT
74 parseUrlPiece "PL" = pure PL
75 parseUrlPiece "PT" = pure PT
76 parseUrlPiece "RU" = pure RU
77 parseUrlPiece "UK" = pure UK
78 parseUrlPiece "ZH" = pure ZH
79 parseUrlPiece _ = Left "Unexpected value of Lang"
80 instance ToHttpApiData Lang where
81 toUrlPiece = pack . show
82 instance Hashable Lang
83 instance Arbitrary Lang where
84 arbitrary = arbitraryBoundedEnum
86 -- | https://en.wikipedia.org/wiki/List_of_ISO_639-1_codes
87 toISO639Lang :: Lang -> Maybe Text
88 toISO639Lang All = Nothing
89 toISO639Lang DE = Just "de"
90 toISO639Lang EL = Just "el"
91 toISO639Lang EN = Just "en"
92 toISO639Lang ES = Just "es"
93 toISO639Lang FR = Just "fr"
94 toISO639Lang IT = Just "it"
95 toISO639Lang PL = Just "pl"
96 toISO639Lang PT = Just "pt"
97 toISO639Lang RU = Just "ru"
98 toISO639Lang UK = Just "uk"
99 toISO639Lang ZH = Just "zh"
102 allLangs = [minBound .. maxBound]
104 class HasDBid a where
108 -- NOTE: We try to use numeric codes for countries
109 -- https://en.wikipedia.org/wiki/List_of_ISO_3166_country_codes
110 -- https://en.wikipedia.org/wiki/ISO_3166-1_numeric#004
111 dbIds :: [(Lang, Int)]
126 instance HasDBid Lang where
127 toDBid lang = case Map.lookup lang $ Map.fromList dbIds of
129 Nothing -> panic "[G.Core] Add this lang to DB ids"
131 fromDBid dbId = case Map.lookup dbId $ Map.fromList $ map swap dbIds of
133 Nothing -> panic "HasDBid lang, not implemented"
135 ------------------------------------------------------------------------
136 data NLPServerConfig = NLPServerConfig
137 { server :: !PosTagAlgo
139 deriving (Show, Eq, Generic)
140 ------------------------------------------------------------------------
143 ------------------------------------------------------------------------
144 data PosTagAlgo = CoreNLP | JohnSnowServer | Spacy
145 deriving (Show, Read, Eq, Ord, Generic, GQLType)
147 instance Hashable PosTagAlgo
149 instance HasDBid PosTagAlgo where
151 toDBid JohnSnowServer = 2
154 fromDBid 2 = JohnSnowServer
156 fromDBid _ = panic "HasDBid posTagAlgo : Not implemented"