-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathBankParser.hs
207 lines (173 loc) · 8.04 KB
/
BankParser.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
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
{-# LANGUAGE ViewPatterns #-}
{-# LANGUAGE PatternSynonyms #-}
module BankParser where
-- import Text.ParserCombinators.Parsek
import Text.ParserCombinators.Parsek.Position
-- import Text.ParserCombinators.Class
import Data.Char
import Data.List
import Data.Function
import Data.List.Split
import Text.Printf
import AnswerTypes
data SExpr = Atom String | SExpr [SExpr] | Variants deriving Show
keyword :: String -> Parser ()
keyword x = spaces >> string x >> return ()
identifier :: Parser String
identifier = spaces >> munch1 (\x -> isAlphaNum x || x `elem` "_")
parens :: Parser x -> Parser x
parens a = do
keyword "("
x <- a
keyword ")"
return x
expr :: Parser SExpr
expr = (Atom <$> identifier) <|> (SExpr <$> parens exprs) <|> (keyword "variants{}" >> pure Variants)
exprs :: Parser [SExpr]
exprs = sepBy1 expr space
def :: Parser (String, SExpr)
def = do
keyword "lin"
x <- identifier
keyword "="
val <- expr
keyword ";"
return (x,val)
bank :: Parser [(String, SExpr)]
bank = do
_prelude <- munch (\x -> x /= '{')
keyword "{"
keyword "lincat FraCaSPhrase = SS;"
defs <- many def
keyword "}"
return defs
showPb :: Int -> String
showPb n = printf "%03d" n
-- >>> showPb 3
-- "003"
processSuite :: [[(HypID,SExpr)]] -> [String]
processSuite pbs = "suite :: (Int -> (Phr,Phr,[Bool]) -> IO ()) -> IO ()" :
"suite handleProblem = do" :
concat
[[" handleProblem " ++ show pb ++ " " ++ pbName pb] | (((pb,_,_),_):_) <- pbs ]
parseBank :: IO (ParseResult SourcePos [(String, SExpr)])
parseBank = parseFromFile bank longestResult "FraCaS-treebank/src/FraCaSBankI.gf"
processProblem :: [(HypID,SExpr)] -> [String]
processProblem defs = concatMap processDef defs ++ problemDef (reverse (map fst defs))
pbName :: Int -> String
pbName pb = "p_" ++ showPb pb
problemDef :: [HypID] -> [String]
problemDef ((th@(pb,_,_):hs))
= [pbName pb ++ " :: (Phr,Phr,[Bool])"
,pbName pb ++ " = (" ++ intercalate " ### " (map hypName hyps') ++ "," ++ hypName th ++ "," ++ show rs ++ ")"]
where hyps' = [h | h@(_,_,[_]) <- reverse hs]
rs = case lookup pb expectResults of
Nothing -> [True]
Just x -> x
problemDef [] = error "problem without hypothesis"
hypName :: HypID -> String
hypName (pb,h,t) = "s_" ++ showPb pb ++ "_" ++ show h ++ "_" ++ t
oParens :: [Char] -> [Char]
oParens x = "(" ++ x ++ ")"
processDef :: (HypID, SExpr) -> [String]
processDef (h,e) = [x ++ " :: Phr"
,x ++ "=" ++ e']
where x = hypName h
e' = case overrides h of
Nothing -> processExp e
Just v -> v
processExp :: SExpr -> String
processExp (MoreThanSNP cn s) = processExp (SExpr [Atom "MoreThanQuant",cn,s])
processExp (MoreThanNPNP cn np) = processExp (SExpr [Atom "MoreThanNPQuant",cn,np])
processExp (NMoreThanNPNP n cn np) = processExp (SExpr [Atom "NMoreThanNPQuant",n,cn,np])
processExp (TwiceAsManyAs cn np) = processExp (SExpr [Atom "TwiceAsManyAs",cn,np])
-- fix for problem 230 and following
processExp (SExpr xs) = oParens (intercalate " " (map processExp xs))
processExp (Atom []) = error "empty identifer"
processExp (Atom s@(x:xs)) = oParens $ case reverse s of
('A':'_':_) -> "lexemeA " ++ show s
('N':'_':_) -> "lexemeN " ++ show s
('2':'N':'_':_) -> "lexemeN2 " ++ show s
('N':'P':'_':_) -> "lexemePN " ++ show s
('P':'R':'_':_) -> "lexemeRP " ++ show s
('V':'_':_) -> "lexemeV " ++ show s
('P':'V':'_':_) -> "lexemeVP " ++ show s
('2':'V':'_':_) -> "lexemeV2 " ++ show s
('2':'A':'_':_) -> "lexemeA2 " ++ show s
('V':'V':'_':_) -> "lexemeVV " ++ show s
('S':'2':'V':'_':_) -> "lexemeV2S " ++ show s
('S':'V':'_':_) -> "lexemeVS " ++ show s
('V':'2':'V':'_':_) -> "lexemeV2V " ++ show s
('3':'V':'_':_) -> "lexemeV3 " ++ show s
('v':'d':'A':'_':_) -> "lexemeAdv " ++ show s
('V':'d':'A':'_':_) -> "lexemeAdV " ++ show s
('A':'d':'A':'_':_) -> "lexemeAdA " ++ show s
('p':'e':'r':'P':'_':_) -> "lexemePrep " ++ show s
('j':'b':'u':'S':'_':_) -> "lexemeSubj " ++ show s
_ -> toLower x : xs
processExp Variants = "variants"
pattern MoreThanSNP :: SExpr -> SExpr -> SExpr
pattern MoreThanSNP cn s = SExpr [Atom "DetCN",SExpr [Atom "DetQuant",SExpr [Atom "IndefArt"],SExpr [Atom "NumPl"]],SExpr [Atom "AdvCN",SExpr [Atom "AdjCN",SExpr [Atom "UseComparA_prefix",SExpr [Atom "many_A"]],cn],SExpr [Atom "SubjS",SExpr [Atom "than_Subj"],s]]]
pattern MoreThanNPNP :: SExpr -> SExpr -> SExpr
pattern MoreThanNPNP cn np = SExpr [Atom "DetCN",SExpr [Atom "DetQuant",SExpr [Atom "IndefArt"],SExpr [Atom "NumPl"]],SExpr [Atom "AdvCN",SExpr [Atom "AdjCN",SExpr [Atom "UseComparA_prefix",SExpr [Atom "many_A"]],cn],SExpr [Atom "PrepNP",SExpr [Atom "than_Prep"],np]]]
pattern TwiceAsManyAs :: SExpr -> SExpr -> SExpr
pattern TwiceAsManyAs cn np = SExpr [Atom "DetCN",SExpr [Atom "twice_as_many_Det"],SExpr [Atom "AdvCN",cn,SExpr [Atom "PrepNP",SExpr [Atom "than_Prep"],np]]]
pattern NMoreThanNPNP :: SExpr -> SExpr -> SExpr -> SExpr
pattern NMoreThanNPNP n cn np = SExpr [Atom "DetCN",SExpr [Atom "DetQuant",SExpr [Atom "IndefArt"],SExpr [Atom "NumCard",SExpr [Atom "NumNumeral",n]]],SExpr [Atom "AdvCN",SExpr [Atom "AdjCN",SExpr [Atom "UseComparA_prefix",SExpr [Atom "many_A"]],cn],SExpr [Atom "PrepNP",SExpr [Atom "than_Prep"],np]]]
frst :: (t2, t1, t) -> t2
frst (x,_,_) = x
main :: IO ()
main = do
Right inp <- parseBank
let debugged = [((pbNumber,hypNumber,hypTyp),e)
| (x,e) <- inp,
let (pbNumber, hypNumber, hypTyp) = parseHName x,
pbNumber == 243,
hypTyp /= "q"]
-- mapM_ print debugged
let handled = [((pbNumber,hypNumber,hypTyp),e)
| (x,e) <- inp,
let (pbNumber, hypNumber, hypTyp) = parseHName x,
-- pbNumber >= 114, -- start of anaphora section
-- pbNumber <= 141, -- end of anaphora section
-- pbNumber <= 251, -- end of ellipsis section
(pbNumber >= 326) || (pbNumber < 311),
hypTyp /= "q"]
problems = filter (not . (`elem` disabledProblems) . frst . fst . head) $
groupBy ((==) `on` (frst . fst)) handled
putStrLn $ unlines $
("module Bank where" :
"import MS" :
concatMap processProblem problems ++
processSuite problems)
parseHName :: [Char] -> HypID
parseHName x = case splitOn "_" x of
("s": pbNumber : hypNumber : rest) -> (read pbNumber, read hypNumber, intercalate "_" rest)
_ -> error ("statement with unexpected format: " ++ x)
type HypID = (Int, Int, [Char])
expectResults :: [(Int, [Bool])]
expectResults = [(i,case a of
Yes -> [True]
No -> [False]
_ -> [True,False])
| (i,a) <- answers]
overrides :: HypID -> Maybe String
overrides (177,1,"p")= Just "s_177_1_p_NEW"
overrides (122,4,"h")= Just "s_122_4_h_ALT"
overrides (155,2,"p")= Just "s_155_2_p_ALT"
overrides (086,3,"h")= Just "s_086_2_h_ALT" -- gf syntax has the wrong noun (accountant vs. lawyer)
overrides (323,4,"h") = Just "s_323_4_h_NEW"
overrides (323,1,"p") = Just "s_323_1_p_NEW"
overrides _ = Nothing
disabledProblems :: [Int]
disabledProblems =
[137,171,172
,216,217 -- syntax wrong: should be (john is (fatter politician than
-- bill)) not ((john is fatter politician) than bill)
,244,245 -- syntax wrong
,276 -- degenerate problem
,285,286 -- incomprehensible syntax
,305 -- degenerate problem
,309 -- degenerate problem
,310 -- degenerate problem
] ++ [i | (i,Undef) <- answers]