Locales support
Summary:
* Locales support for the library, following `<Lang>_<Region>` with ISO 639-1 code for `<Lang>` and ISO 3166-1 alpha-2 code for `<Region>` (#33)
* `Locale` opaque type (composite of `Lang` and `Region`) with `makeLocale` smart constructor to only allow valid `(Lang, Region)` combinations
* API: `Context`'s `lang` parameter has been replaced by `locale`, with optional `Region` and backward compatibility.
* `Rules/<Lang>.hs` exposes
- `langRules`: cross-locale rules for `<Lang>`, from `<Dimension>/<Lang>/Rules.hs`
- `localeRules`: locale-specific rules, from `<Dimension>/<Lang>/<Region>/Rules.hs`
- `defaultRules`: `langRules` + specific rules from select locales to ensure backward-compatibility
* Corpus, tests & classifiers
- 1 classifier per locale, with default classifier (`<Lang>_XX`) when no locale provided (backward-compatible)
- Default classifiers are built on existing corpus
- Locale classifiers are built on
- `<Dimension>/<Lang>/Corpus.hs` exposes a common `corpus` to all locales of `<Lang>`
- `<Dimension>/<Lang>/<Region>/Corpus.hs` exposes `allExamples`: a list of examples specific to the locale (following `<Dimension>/<Lang>/<Region>/Rules.hs`).
- Locale classifiers use the language corpus extended with the locale examples as training set.
- Locale examples need to use the same `Context` (i.e. reference time) as the language corpus.
- For backward compatibility, `<Dimension>/<Lang>/Corpus.hs` can expose also `defaultCorpus`, which is `corpus` augmented with specific examples. This is controlled by `getDefaultCorpusForLang` in `Duckling.Ranking.Generate`.
- Tests run against each classifier to make sure runtime works as expected.
* MM/DD (en_US) vs DD/MM (en_GB) example to illustrate
Reviewed By: JonCoens, blandinw
Differential Revision: D6038096
fbshipit-source-id: f29c28d
2017-10-13 18:15:32 +03:00
|
|
|
-- Copyright (c) 2016-present, Facebook, Inc.
|
|
|
|
-- All rights reserved.
|
|
|
|
--
|
|
|
|
-- This source code is licensed under the BSD-style license found in the
|
|
|
|
-- LICENSE file in the root directory of this source tree. An additional grant
|
|
|
|
-- of patent rights can be found in the PATENTS file in the same directory.
|
|
|
|
|
|
|
|
|
|
|
|
{-# LANGUAGE DeriveAnyClass #-}
|
|
|
|
{-# LANGUAGE DeriveGeneric #-}
|
|
|
|
{-# LANGUAGE NoRebindableSyntax #-}
|
|
|
|
|
|
|
|
module Duckling.Locale
|
|
|
|
( Lang(..)
|
|
|
|
, Locale(..)
|
|
|
|
, Region(..)
|
|
|
|
, allLocales
|
|
|
|
, makeLocale
|
|
|
|
) where
|
|
|
|
|
|
|
|
import Data.Hashable
|
|
|
|
import Data.HashMap.Strict (HashMap)
|
|
|
|
import Data.HashSet (HashSet)
|
|
|
|
import GHC.Generics
|
|
|
|
import Prelude
|
|
|
|
import TextShow (TextShow)
|
|
|
|
import qualified Data.HashMap.Strict as HashMap
|
|
|
|
import qualified Data.HashSet as HashSet
|
|
|
|
import qualified TextShow as TS
|
|
|
|
|
|
|
|
-- | ISO 639-1 Language.
|
|
|
|
-- See https://en.wikipedia.org/wiki/List_of_ISO_639-1_codes
|
|
|
|
data Lang
|
|
|
|
= AR
|
|
|
|
| BG
|
|
|
|
| CS
|
|
|
|
| DA
|
|
|
|
| DE
|
|
|
|
| EN
|
|
|
|
| ES
|
|
|
|
| ET
|
|
|
|
| FR
|
|
|
|
| GA
|
|
|
|
| HE
|
|
|
|
| HR
|
|
|
|
| HU
|
|
|
|
| ID
|
|
|
|
| IT
|
|
|
|
| JA
|
|
|
|
| KA
|
|
|
|
| KO
|
|
|
|
| MY
|
|
|
|
| NB
|
|
|
|
| NL
|
|
|
|
| PL
|
|
|
|
| PT
|
|
|
|
| RO
|
|
|
|
| RU
|
|
|
|
| SV
|
|
|
|
| TR
|
|
|
|
| UK
|
|
|
|
| VI
|
|
|
|
| ZH
|
|
|
|
deriving (Bounded, Enum, Eq, Generic, Hashable, Ord, Read, Show)
|
|
|
|
|
|
|
|
instance TextShow Lang where
|
|
|
|
showb = TS.fromString . show
|
|
|
|
|
|
|
|
-- | ISO 3166-1 alpha-2 Country code (includes regions and territories).
|
|
|
|
-- See https://en.wikipedia.org/wiki/ISO_3166-1_alpha-2
|
|
|
|
data Region
|
2017-10-16 19:43:53 +03:00
|
|
|
= CA
|
|
|
|
| CN
|
2017-10-14 02:36:00 +03:00
|
|
|
| GB
|
|
|
|
| HK
|
|
|
|
| MO
|
|
|
|
| TW
|
Locales support
Summary:
* Locales support for the library, following `<Lang>_<Region>` with ISO 639-1 code for `<Lang>` and ISO 3166-1 alpha-2 code for `<Region>` (#33)
* `Locale` opaque type (composite of `Lang` and `Region`) with `makeLocale` smart constructor to only allow valid `(Lang, Region)` combinations
* API: `Context`'s `lang` parameter has been replaced by `locale`, with optional `Region` and backward compatibility.
* `Rules/<Lang>.hs` exposes
- `langRules`: cross-locale rules for `<Lang>`, from `<Dimension>/<Lang>/Rules.hs`
- `localeRules`: locale-specific rules, from `<Dimension>/<Lang>/<Region>/Rules.hs`
- `defaultRules`: `langRules` + specific rules from select locales to ensure backward-compatibility
* Corpus, tests & classifiers
- 1 classifier per locale, with default classifier (`<Lang>_XX`) when no locale provided (backward-compatible)
- Default classifiers are built on existing corpus
- Locale classifiers are built on
- `<Dimension>/<Lang>/Corpus.hs` exposes a common `corpus` to all locales of `<Lang>`
- `<Dimension>/<Lang>/<Region>/Corpus.hs` exposes `allExamples`: a list of examples specific to the locale (following `<Dimension>/<Lang>/<Region>/Rules.hs`).
- Locale classifiers use the language corpus extended with the locale examples as training set.
- Locale examples need to use the same `Context` (i.e. reference time) as the language corpus.
- For backward compatibility, `<Dimension>/<Lang>/Corpus.hs` can expose also `defaultCorpus`, which is `corpus` augmented with specific examples. This is controlled by `getDefaultCorpusForLang` in `Duckling.Ranking.Generate`.
- Tests run against each classifier to make sure runtime works as expected.
* MM/DD (en_US) vs DD/MM (en_GB) example to illustrate
Reviewed By: JonCoens, blandinw
Differential Revision: D6038096
fbshipit-source-id: f29c28d
2017-10-13 18:15:32 +03:00
|
|
|
| US
|
|
|
|
deriving (Bounded, Enum, Eq, Generic, Hashable, Ord, Read, Show)
|
|
|
|
|
|
|
|
instance TextShow Region where
|
|
|
|
showb = TS.fromString . show
|
|
|
|
|
|
|
|
data Locale = Locale Lang (Maybe Region)
|
|
|
|
deriving (Eq, Generic, Hashable, Ord)
|
|
|
|
|
|
|
|
instance Show Locale where
|
|
|
|
show (Locale lang Nothing) = show lang ++ "_XX"
|
|
|
|
show (Locale lang (Just region)) = show lang ++ "_" ++ show region
|
|
|
|
|
|
|
|
instance TextShow Locale where
|
|
|
|
showb = TS.fromString . show
|
|
|
|
|
|
|
|
makeLocale :: Lang -> Maybe Region -> Locale
|
|
|
|
makeLocale lang Nothing = Locale lang Nothing
|
|
|
|
makeLocale lang (Just region)
|
|
|
|
| HashSet.member region locales = Locale lang $ Just region
|
|
|
|
| otherwise = Locale lang Nothing
|
|
|
|
where
|
|
|
|
locales = HashMap.lookupDefault HashSet.empty lang allLocales
|
|
|
|
|
|
|
|
allLocales :: HashMap Lang (HashSet Region)
|
|
|
|
allLocales = HashMap.fromList
|
2017-10-16 19:43:53 +03:00
|
|
|
[ (EN, HashSet.fromList [CA, GB, US])
|
2017-10-14 02:36:00 +03:00
|
|
|
, (ZH, HashSet.fromList [CN, HK, MO, TW])
|
Locales support
Summary:
* Locales support for the library, following `<Lang>_<Region>` with ISO 639-1 code for `<Lang>` and ISO 3166-1 alpha-2 code for `<Region>` (#33)
* `Locale` opaque type (composite of `Lang` and `Region`) with `makeLocale` smart constructor to only allow valid `(Lang, Region)` combinations
* API: `Context`'s `lang` parameter has been replaced by `locale`, with optional `Region` and backward compatibility.
* `Rules/<Lang>.hs` exposes
- `langRules`: cross-locale rules for `<Lang>`, from `<Dimension>/<Lang>/Rules.hs`
- `localeRules`: locale-specific rules, from `<Dimension>/<Lang>/<Region>/Rules.hs`
- `defaultRules`: `langRules` + specific rules from select locales to ensure backward-compatibility
* Corpus, tests & classifiers
- 1 classifier per locale, with default classifier (`<Lang>_XX`) when no locale provided (backward-compatible)
- Default classifiers are built on existing corpus
- Locale classifiers are built on
- `<Dimension>/<Lang>/Corpus.hs` exposes a common `corpus` to all locales of `<Lang>`
- `<Dimension>/<Lang>/<Region>/Corpus.hs` exposes `allExamples`: a list of examples specific to the locale (following `<Dimension>/<Lang>/<Region>/Rules.hs`).
- Locale classifiers use the language corpus extended with the locale examples as training set.
- Locale examples need to use the same `Context` (i.e. reference time) as the language corpus.
- For backward compatibility, `<Dimension>/<Lang>/Corpus.hs` can expose also `defaultCorpus`, which is `corpus` augmented with specific examples. This is controlled by `getDefaultCorpusForLang` in `Duckling.Ranking.Generate`.
- Tests run against each classifier to make sure runtime works as expected.
* MM/DD (en_US) vs DD/MM (en_GB) example to illustrate
Reviewed By: JonCoens, blandinw
Differential Revision: D6038096
fbshipit-source-id: f29c28d
2017-10-13 18:15:32 +03:00
|
|
|
]
|