-
Notifications
You must be signed in to change notification settings - Fork 9
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
6 changed files
with
545 additions
and
0 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,98 @@ | ||
{-# LANGUAGE BlockArguments #-} | ||
{-# LANGUAGE OverloadedLabels #-} | ||
{-# LANGUAGE OverloadedRecordDot #-} | ||
{-# LANGUAGE OverloadedStrings #-} | ||
{-# LANGUAGE QuasiQuotes #-} | ||
{-# LANGUAGE NoFieldSelectors #-} | ||
{-# OPTIONS_GHC -Wall #-} | ||
|
||
module LS.XPile.PurescriptSpec (spec) where | ||
|
||
import Control.Monad (unless) | ||
import Data.Either (lefts, rights) | ||
import Data.Foldable qualified as DF | ||
import Data.Text.Lazy qualified as TL | ||
import Data.Text.Lazy.IO qualified as TL | ||
import LS qualified as SFL4 | ||
import LS.NLP.NLG (NLGEnv, allLangs, langEng, myNLGEnv, printLangs) | ||
import LS.XPile.Logging (XPileLogW, fmapE, mutter, xpLog) | ||
import LS.XPile.Purescript (translate2PS) | ||
import System.FilePath | ||
import System.IO.Unsafe (unsafeInterleaveIO) | ||
import Test.Hspec (Spec, describe, it, runIO) | ||
import Test.Hspec.Golden ( Golden(..) ) | ||
import Prelude hiding (exp, seq) | ||
import LS.Interpreter (l4interpret) | ||
|
||
goldenGeneric :: String -> TL.Text -> Golden TL.Text | ||
goldenGeneric name myoutput = | ||
Golden | ||
{ output = myoutput, | ||
encodePretty = TL.unpack, | ||
writeToFile = TL.writeFile, | ||
readFromFile = TL.readFile, | ||
goldenFile = testPath <.> "purs.expected", | ||
actualFile = Just (testPath <.> "purs.actual"), | ||
failFirstTime = False | ||
} | ||
where | ||
testPath = "test" </> "testdata" </> "golden" </> "PurescriptSpec" </> name | ||
|
||
data NLGData | ||
= MkNLGData | ||
{ env :: NLGEnv, | ||
allEnv :: [NLGEnv], | ||
allEnvErr :: XPileLogW, | ||
engErr :: XPileLogW | ||
} | ||
|
||
spec :: Spec | ||
spec = do | ||
describe "toMathLang for arithRule3" do | ||
let testPath = "test" </> "testdata" </> "golden" </> "PurescriptSpec" </> "must_sing.csv" | ||
opts = SFL4.defaultOptions {SFL4.file = [testPath]} | ||
nlgLangs <- runIO allLangs | ||
strLangs <- runIO $ printLangs allLangs | ||
rules <- runIO (SFL4.dumpRules opts) | ||
l4i <- runIO $ l4interpret rules | ||
(engE, engErr) <- runIO $ xpLog <$> langEng | ||
(_, nlgData) <- runIO $ | ||
case engE of | ||
Left err -> do | ||
putStrLn $ unlines $ "natural4: encountered error when obtaining langEng" : err | ||
pure (Nothing, Nothing) | ||
Right eng -> do | ||
(nlgEnv, _nlgEnvErr) <- unsafeInterleaveIO $ xpLog <$> myNLGEnv l4i eng -- Only load the NLG environment if we need it. | ||
(allNLGEnv, allNLGEnvErr) <- unsafeInterleaveIO do | ||
xps <- traverse (myNLGEnv l4i) nlgLangs | ||
return (xpLog $ sequenceA xps) | ||
|
||
case nlgEnv of | ||
Left err -> do | ||
putStrLn $ unlines $ "natural4: encountered error while obtaining myNLGEnv" : err | ||
pure (Nothing, Nothing) | ||
Right nlgEnvR -> do | ||
let allNLGEnvErrors = mconcat $ lefts allNLGEnv | ||
unless (null allNLGEnvErrors) do | ||
putStrLn "natural4: encountered error while obtaining allNLGEnv" | ||
DF.traverse_ putStrLn allNLGEnvErrors | ||
|
||
let allNLGEnvR = rights allNLGEnv | ||
|
||
let nlgData = | ||
MkNLGData | ||
nlgEnvR | ||
allNLGEnvR | ||
allNLGEnvErr | ||
engErr | ||
|
||
pure (Just nlgEnvR, Just nlgData) | ||
let Just justNLGDate = nlgData | ||
nlgEnvs = justNLGDate.allEnv | ||
eng = justNLGDate.env | ||
(psResult, _) = xpLog do | ||
mutter "* main calling translate2PS" | ||
fmapE (<> ("\n\n" <> "allLang = [\"" <> strLangs <> "\"]")) (translate2PS nlgEnvs eng rules) | ||
(Right justResult) = psResult | ||
finalResult = TL.pack justResult | ||
it "convert must sing to Purescript" $ goldenGeneric "must_sing" finalResult |
5 changes: 5 additions & 0 deletions
5
lib/haskell/natural4/test/testdata/golden/PurescriptSpec/must_sing.csv
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
,,EVERY,Person,,,,,,,,,,,, | ||
,,WHO,walks,,,,,,,,,,,, | ||
,,AND,,eats,,,,,,,,,,, | ||
,,,OR,drinks,,,,,,,,,,, | ||
,,MUST,sing,,,,,,,,,,,, |
92 changes: 92 additions & 0 deletions
92
lib/haskell/natural4/test/testdata/golden/PurescriptSpec/must_sing.purs.expected
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,92 @@ | ||
-- This file was automatically generated by natural4. | ||
-- Do not edit by hand. | ||
-- Instead, revise the toolchain starting at smucclaw/dsl/lib/haskell/natural4/app/Main.hs | ||
|
||
module RuleLib.Interview where | ||
|
||
import Prelude | ||
import Data.Either | ||
import Data.Maybe | ||
import Data.Tuple | ||
import Data.Map as Map | ||
import Foreign.Object as Object | ||
|
||
import AnyAll.Types | ||
|
||
interviewRules :: Item String | ||
interviewRules = All | ||
( Pre "all of:" ) | ||
[ Leaf "does the person walk?" | ||
, Any | ||
( Pre "any of:" ) | ||
[ Leaf "does the person eat?" | ||
, Leaf "does the person drink?" | ||
] | ||
] | ||
|
||
|
||
interviewRules_nl :: NLDict | ||
interviewRules_nl = | ||
Map.fromFoldable | ||
[ ] | ||
|
||
nl4chi :: Object.Object (Item String) | ||
nl4chi = Object.fromFoldable | ||
[ Tuple "Person" | ||
( All | ||
( Pre "all of:" ) | ||
[ Leaf "人 走 吗?" | ||
, Any | ||
( Pre "any of:" ) | ||
[ Leaf "人 吃 吗?" | ||
, Leaf "人 喝 吗?" | ||
] | ||
] | ||
) | ||
] | ||
|
||
nl4chiMarking :: Marking | ||
nl4chiMarking = Marking $ Map.fromFoldable | ||
[] | ||
|
||
nl4eng :: Object.Object (Item String) | ||
nl4eng = Object.fromFoldable | ||
[ Tuple "Person" | ||
( All | ||
( Pre "all of:" ) | ||
[ Leaf "does the person walk?" | ||
, Any | ||
( Pre "any of:" ) | ||
[ Leaf "does the person eat?" | ||
, Leaf "does the person drink?" | ||
] | ||
] | ||
) | ||
] | ||
|
||
nl4engMarking :: Marking | ||
nl4engMarking = Marking $ Map.fromFoldable | ||
[] | ||
|
||
nl4may :: Object.Object (Item String) | ||
nl4may = Object.fromFoldable | ||
[ Tuple "Person" | ||
( All | ||
( Pre "all of:" ) | ||
[ Leaf "adakah seseorang berjalan?" | ||
, Any | ||
( Pre "any of:" ) | ||
[ Leaf "adakah seseorang makan?" | ||
, Leaf "adakah seseorang minum?" | ||
] | ||
] | ||
) | ||
] | ||
|
||
nl4mayMarking :: Marking | ||
nl4mayMarking = Marking $ Map.fromFoldable | ||
[] | ||
|
||
|
||
|
||
allLang = ["nl4chi", "nl4eng", "nl4may"] |
27 changes: 27 additions & 0 deletions
27
lib/haskell/natural4/test/testdata/golden/PurescriptSpec/rodents.csv
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
,§,Covered If …,,,,,,,,,,, | ||
FALSE,DECIDE,Loss or Damage 1,IS,,Covered,,,,,,,, | ||
TRUE,IF,NOT,,Loss or Damage,caused by,rodents,FALSE,,,,,, | ||
,,,,,OR,insects,TRUE,,,,,, | ||
,,,,,OR,vermin,FALSE,,,,,, | ||
,,,,,OR,birds,FALSE,,,,,, | ||
FALSE,,,UNLESS,,,Loss or Damage,IS,to Contents,,FALSE,,, | ||
,,,,,AND,"""","""",caused by,birds,FALSE,,, | ||
TRUE,,,,OR,,"""","""",ensuing covered loss,TRUE,,,, | ||
FALSE,,,,,UNLESS,,any other exclusion applies,,FALSE,,,, | ||
FALSE,,,,,,OR,an animal caused water to escape from,,,a household appliance,,,FALSE | ||
,,,,,,,,,OR,a swimming pool,,,FALSE | ||
,,,,,,,,,OR,"a plumbing, heating, or air conditioning system",,,FALSE | ||
;;,,,,,,,,,,,,, | ||
,§,Not Covered If …,,,,,,,,,,, | ||
FALSE,DECIDE,Loss or Damage 2,IS,,Covered,,,,,,,, | ||
FALSE,IF,Loss or Damage,caused by,rodents,FALSE,,,,,,,, | ||
,,,OR,insects,FALSE,,,,,,,, | ||
,,,OR,vermin,FALSE,,,,,,,, | ||
,,,OR,birds,FALSE,,,,,,,, | ||
FALSE,UNLESS,,,Loss or Damage,IS,to Contents,,FALSE,,,,, | ||
,,,AND,"""","""",caused by,birds,FALSE,,,,, | ||
FALSE,,OR,,Loss or Damage,IS,ensuing covered loss,FALSE,,,,,, | ||
FALSE,,,UNLESS,,any other exclusion applies,,FALSE,,,,,, | ||
FALSE,,,,OR,an animal caused water to escape from,,,a household appliance,,,FALSE,, | ||
,,,,,,,OR,a swimming pool,,,FALSE,, | ||
,,,,,,,OR,"a plumbing, heating, or air conditioning system",,,FALSE,, |
Oops, something went wrong.