-- | Run this function to overwrite the file with Classifiers data
regenClassifiers :: Lang -> IO ()
regenClassifiers lang = do
  putStrLn $ "Regenerating " ++ filepath ++ "..."
  writeFile filepath $
    (headerComment ++) $
    prettyPrintWithMode baseMode $ (noLoc <$) m
  putStrLn "Done!"
  where
    filepath = "Duckling/Ranking/Classifiers/" ++ show lang ++ ".hs"

    rules = rulesFor lang . HashSet.singleton $ This Time

    -- | The trained classifier to write out
    classifiers = makeClassifiers rules trainSet

    -- | The training set (corpus)
    trainSet = case lang of
      AR -> (testContext, [])
      CS -> (testContext, [])
      DA -> DATime.corpus
      DE -> DETime.corpus
      EN -> ENTime.corpus
      ES -> ESTime.corpus
      ET -> (testContext, [])
      FR -> FRTime.corpus
      GA -> GATime.corpus
      HR -> HRTime.corpus
      HE -> HETime.corpus
      ID -> (testContext, [])
      IT -> ITTime.corpus
      JA -> (testContext, [])
      KO -> KOTime.corpus
      MY -> (testContext, [])
      NB -> NBTime.corpus
      NL -> (testContext, [])
      PL -> PLTime.corpus
      PT -> PTTime.corpus
      RO -> ROTime.corpus
      RU -> (testContext, [])
      SV -> SVTime.corpus
      TR -> (testContext, [])
      UK -> (testContext, [])
      VI -> VITime.corpus
      ZH -> ZHTime.corpus

    -- Data structure for the module
    m = Module () (Just header) pragmas imports decls

    -- Declares the top level options pragma
    pragmas = [ LanguagePragma () [Ident () "OverloadedStrings"] ]

    -- Declares the header for the module
    -- "module Duckling.Ranking.Classifiers (classifiers) where"
    header = ModuleHead ()
      (ModuleName () $ "Duckling.Ranking.Classifiers." ++ show lang)
      Nothing $
      Just $ ExportSpecList ()
       [ EVar () (unQual "classifiers")
       ]

    -- All imports the file will need
    imports =
      [ genImportModule "Prelude"
      , genImportModule "Duckling.Ranking.Types"
      , (genImportModule "Data.HashMap.Strict")
        { importQualified = True
        , importAs = Just (ModuleName () "HashMap")
        }
      , genImportModule "Data.String"
      ]

    -- The code body
    decls =
      [ -- Type Signature
        TypeSig () [Ident () "classifiers"] (TyCon () (unQual "Classifiers"))
        -- function body
      , FunBind ()
          [ Match () (Ident () "classifiers") []
              (UnGuardedRhs () (genList classifiers)) Nothing
          ]
      ]

    headerComment :: String
    headerComment = "\
\-- Copyright (c) 2016-present, Facebook, Inc.\n\
\-- All rights reserved.\n\
\--\n\
\-- This source code is licensed under the BSD-style license found in the\n\
\-- LICENSE file in the root directory of this source tree. An additional grant\n\
\-- of patent rights can be found in the PATENTS file in the same directory.\n\n\
\-----------------------------------------------------------------\n\
\-- Auto-generated by regenClassifiers\n\
\--\n\
\-- DO NOT EDIT UNLESS YOU ARE SURE THAT YOU KNOW WHAT YOU ARE DOING\n\
\--  @" ++ "generated\n\
\-----------------------------------------------------------------\n"

-- -----------------------------------------------------------------
-- Source generators

-- | Generates a line for an import
--
-- `genImportModule "Foo.Bar"` spits out:
-- "import Foo.Bar" in the code
regenClassifiers :: Lang -> IO ()
regenClassifiers lang = do
  putStrLn $ "Regenerating " ++ filepath ++ "..."
  writeFile filepath $
    (headerComment ++) $
    prettyPrintWithMode baseMode $ (noLoc <$) m
  putStrLn "Done!"
  where
    filepath = "Duckling/Ranking/Classifiers/" ++ show lang ++ ".hs"

    rules = rulesFor lang . HashSet.singleton $ This Time

    -- | The trained classifier to write out
    classifiers = makeClassifiers rules trainSet

    -- | The training set (corpus)
    trainSet = case lang of
      AR -> (testContext, [])
      CS -> (testContext, [])
      DA -> DATime.corpus
      DE -> DETime.corpus
      EN -> ENTime.corpus
      ES -> ESTime.corpus
      ET -> (testContext, [])
      FR -> FRTime.corpus
      GA -> GATime.corpus
      HR -> HRTime.corpus
      HE -> HETime.corpus
      ID -> (testContext, [])
      IT -> ITTime.corpus
      JA -> (testContext, [])
      KO -> KOTime.corpus
      MY -> (testContext, [])
      NB -> NBTime.corpus
      NL -> (testContext, [])
      PL -> PLTime.corpus
      PT -> PTTime.corpus
      RO -> ROTime.corpus
      RU -> (testContext, [])
      SV -> SVTime.corpus
      TR -> (testContext, [])
      UK -> (testContext, [])
      VI -> VITime.corpus
      ZH -> ZHTime.corpus

    -- Data structure for the module
    m = Module () (Just header) pragmas imports decls

    -- Declares the top level options pragma
    pragmas = [ LanguagePragma () [Ident () "OverloadedStrings"] ]

    -- Declares the header for the module
    -- "module Duckling.Ranking.Classifiers (classifiers) where"
    header = ModuleHead ()
      (ModuleName () $ "Duckling.Ranking.Classifiers." ++ show lang)
      Nothing $
      Just $ ExportSpecList ()
       [ EVar () (unQual "classifiers")
       ]

    -- All imports the file will need
    imports =
      [ genImportModule "Prelude"
      , genImportModule "Duckling.Ranking.Types"
      , (genImportModule "Data.HashMap.Strict")
        { importQualified = True
        , importAs = Just (ModuleName () "HashMap")
        }
      , genImportModule "Data.String"
      ]

    -- The code body
    decls =
      [ -- Type Signature
        TypeSig () [Ident () "classifiers"] (TyCon () (unQual "Classifiers"))
        -- function body
      , FunBind ()
          [ Match () (Ident () "classifiers") []
              (UnGuardedRhs () (genList classifiers)) Nothing
          ]
      ]

    headerComment :: String
    headerComment = "\
\-- Copyright (c) 2016-present, Facebook, Inc.\n\
\-- All rights reserved.\n\
\--\n\
\-- This source code is licensed under the BSD-style license found in the\n\
\-- LICENSE file in the root directory of this source tree. An additional grant\n\
\-- of patent rights can be found in the PATENTS file in the same directory.\n\n\
\-----------------------------------------------------------------\n\
\-- Auto-generated by regenClassifiers\n\
\--\n\
\-- DO NOT EDIT UNLESS YOU ARE SURE THAT YOU KNOW WHAT YOU ARE DOING\n\
\--  @" ++ "generated\n\
\-----------------------------------------------------------------\n"

-- -----------------------------------------------------------------
-- Source generators

-- | Generates a line for an import
--
-- `genImportModule "Foo.Bar"` spits out:
-- "import Foo.Bar" in the code
regenClassifiers lang = do
  putStrLn $ "Regenerating " ++ filepath ++ "..."
  writeFile filepath $
    (headerComment ++) $
    prettyPrintWithMode baseMode $ (noLoc <$) m
  putStrLn "Done!"
  where
    filepath = "Duckling/Ranking/Classifiers/" ++ show lang ++ ".hs"

    rules = rulesFor lang . HashSet.singleton $ This Time

    -- | The trained classifier to write out
    classifiers = makeClassifiers rules trainSet

    -- | The training set (corpus)
    trainSet = case lang of
      AR -> (testContext, [])
      CS -> (testContext, [])
      DA -> DATime.corpus
      DE -> DETime.corpus
      EN -> ENTime.corpus
      ES -> ESTime.corpus
      ET -> (testContext, [])
      FR -> FRTime.corpus
      GA -> GATime.corpus
      HR -> HRTime.corpus
      HE -> HETime.corpus
      ID -> (testContext, [])
      IT -> ITTime.corpus
      JA -> (testContext, [])
      KO -> KOTime.corpus
      MY -> (testContext, [])
      NB -> NBTime.corpus
      NL -> (testContext, [])
      PL -> PLTime.corpus
      PT -> PTTime.corpus
      RO -> ROTime.corpus
      RU -> (testContext, [])
      SV -> SVTime.corpus
      TR -> (testContext, [])
      UK -> (testContext, [])
      VI -> VITime.corpus
      ZH -> ZHTime.corpus

    -- Data structure for the module
    m = Module () (Just header) pragmas imports decls

    -- Declares the top level options pragma
    pragmas = [ LanguagePragma () [Ident () "OverloadedStrings"] ]

    -- Declares the header for the module
    -- "module Duckling.Ranking.Classifiers (classifiers) where"
    header = ModuleHead ()
      (ModuleName () $ "Duckling.Ranking.Classifiers." ++ show lang)
      Nothing $
      Just $ ExportSpecList ()
       [ EVar () (unQual "classifiers")
       ]

    -- All imports the file will need
    imports =
      [ genImportModule "Prelude"
      , genImportModule "Duckling.Ranking.Types"
      , (genImportModule "Data.HashMap.Strict")
        { importQualified = True
        , importAs = Just (ModuleName () "HashMap")
        }
      , genImportModule "Data.String"
      ]

    -- The code body
    decls =
      [ -- Type Signature
        TypeSig () [Ident () "classifiers"] (TyCon () (unQual "Classifiers"))
        -- function body
      , FunBind ()
          [ Match () (Ident () "classifiers") []
              (UnGuardedRhs () (genList classifiers)) Nothing
          ]
      ]

    headerComment :: String
    headerComment = "\
\-- Copyright (c) 2016-present, Facebook, Inc.\n\
\-- All rights reserved.\n\
\--\n\
\-- This source code is licensed under the BSD-style license found in the\n\
\-- LICENSE file in the root directory of this source tree. An additional grant\n\
\-- of patent rights can be found in the PATENTS file in the same directory.\n\n\
\-----------------------------------------------------------------\n\
\-- Auto-generated by regenClassifiers\n\
\--\n\
\-- DO NOT EDIT UNLESS YOU ARE SURE THAT YOU KNOW WHAT YOU ARE DOING\n\
\--  @" ++ "generated\n\
\-----------------------------------------------------------------\n"

-- -----------------------------------------------------------------
-- Source generators

-- | Generates a line for an import
--
-- `genImportModule "Foo.Bar"` spits out:
-- "import Foo.Bar" in the code