-
Notifications
You must be signed in to change notification settings - Fork 10
/
Translator.hs
37 lines (32 loc) · 1.02 KB
/
Translator.hs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
module Main where
-- Translates from STDIN to STDOUT, from Lang1 to Lang2.
-- The (obligatory) command-line arguments are:
--
-- * PGF file
-- * start category
-- * source language
-- * target language
--
-- Usage:
--
-- cat tests/acewiki_aceowl/sentences.txt | ./Translator TestAttempto.pgf ACEText TestAttemptoAce TestAttemptoCat
import PGF
import Data.Maybe
import System.Environment (getArgs)
main :: IO ()
main = do
file:cat:lang1:lang2:_ <- getArgs
pgf <- readPGF file
loop (translate pgf (fromJust (readType cat)) (fromJust (readLanguage lang1)) (fromJust (readLanguage lang2)))
loop :: (String -> String) -> IO ()
loop trans = do
s <- getLine
putStr s
putStrLn $ trans s
loop trans
translate :: PGF -> Type -> Language -> Language -> String -> String
translate pgf cat lang1 lang2 s =
case parse_ pgf lang1 cat (Just 4) s of
(ParseFailed num, _) -> "|FAIL " ++ unwords (take num (words s))
(ParseOk trees, _) -> "|OK (" ++ show (length trees) ++ ") " ++ (unlines [linearize pgf lang2 t | t <- trees])
_ -> "|FAIL"