summaryrefslogtreecommitdiff
path: root/examples/PennTreebank/training.hs
blob: 31bc74492e3286dba781559f32dba2f05fc89a0b (plain)
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
import PGF
import qualified Data.Map as Map
import Data.Maybe
import Data.List

main = do
  pgf <- readPGF "ParseEngAbs.pgf"
  ls <- fmap (filterExprs . lines) $ readFile "log3.txt"
  putStrLn ""
  putStrLn ("trees: "++show (length ls))
  let stats = foldl' (collectStats pgf)
                     (initStats pgf)
                     [(fromMaybe (error l) (readExpr (toQ l)),Just (mkCId "Phr"),Nothing) | l <- ls]

  putStrLn ("coverage: "++show (coverage stats))

  putStrLn ("Writing ParseEngAbs.probs...")
  writeFile "ParseEngAbs.probs"  (unlines [show f ++ "\t" ++ show p | (f,p) <- uprobs pgf stats])
  
  putStrLn ("Writing ParseEngAbs2.probs...")
  writeFile "ParseEngAbs2.probs" (unlines [show cat1 ++ "\t" ++ show cat2 ++ "\t" ++ show p | (cat1,cat2,p) <- bprobs pgf stats])
  
  putStrLn ("Writing global.probs...")
  writeFile "global.probs" (unlines [show f ++ "\t" ++ show p | (f,p) <- gprobs pgf stats])
  where
    toQ []       = []
    toQ ('[':cs) = let (xs,']':ys) = break (==']') cs
                   in toQ ('?' : ys)
    toQ ('?':cs) = 'Q' : toQ cs
    toQ (c:cs)   = c   : toQ cs

filterExprs []          = []
filterExprs (l:ls)
  | null l              = filterExprs ls
  | elem (head l) "+#*" = drop 2 l : filterExprs ls
  | otherwise           = filterExprs ls

initStats pgf =
  (Map.fromListWith (+)
      ([(f,1) | f <- functions pgf] ++
       [(cat pgf f,1) | f <- functions pgf])
  ,Map.empty
  ,0
  )

collectStats pgf (ustats,bstats,count) (e,mb_cat1,mb_cat2) =
  case unApp e of
    Just (f,args) -> let fcat = fromMaybe (cat2 pgf f e) mb_cat1
                         cf   = fromMaybe 0 (Map.lookup f ustats)
                         cc   = fromMaybe 0 (Map.lookup fcat ustats)
                     in cf `seq` cc `seq` bstats `seq` count `seq`
                        foldl' (collectStats pgf)
                               (Map.insert f (cf+1) (Map.insert fcat (cc+1) ustats)
                               ,(if null args
                                   then Map.insertWith (+) (fcat,wildCId) 1
                                   else id)
                                (maybe bstats (\cat2 -> Map.insertWith (+) (cat2,fcat) 1 bstats) mb_cat2)
                               ,count+1
                               )
                               (zip3 args (argCats f) (repeat (Just fcat)))
    Nothing       -> case unStr e of
                       Just _        -> (ustats,bstats,count+1)
                       Nothing       -> error ("collectStats ("++show e++")")
  where
	argCats f =
	  case fmap unType (functionType pgf f) of
	    Just (arg_tys,_,_) -> let tyCat (_,_,ty) = let (_,cat,_) = unType ty in Just cat
	                          in map tyCat arg_tys
	    Nothing            -> repeat Nothing

coverage (ustats,bstats,count) =
  let c = fromMaybe 0 (Map.lookup (mkCId "Q") ustats)
  in (fromIntegral (count - c) / fromIntegral count) * 100

uprobs pgf (ustats,bstats,count) =
  [toProb f (cat pgf f) | f <- functions pgf]
  where
    toProb f cat =
      let count    = fromMaybe 0 (Map.lookup f ustats)
          cat_mass = fromMaybe 0 (Map.lookup cat ustats)
      in (f, fromIntegral count / fromIntegral cat_mass :: Double)

bprobs pgf (ustats,bstats,count) =
  concat [toProb cat | cat <- categories pgf]
  where
    toProb cat =
      let mass = sum [count | ((cat1,cat2),count) <- Map.toList bstats, cat1==cat]
      in [(cat1,cat2,fromIntegral count / fromIntegral mass) 
					| ((cat1,cat2),count) <- Map.toList bstats, cat1==cat]

gprobs pgf (ustats,bstats,count) =
  sortBy (\x y -> compare (snd y) (snd x)) [toProb f | f <- functions pgf]
  where
    toProb f =
      let fcount = fromMaybe 0 (Map.lookup f ustats)
      in (f, fromIntegral fcount / fromIntegral count :: Double)

cat pgf f =
  case fmap unType (functionType pgf f) of
    Just (_,cat,_) -> cat
    Nothing        -> error ("Unknown function "++showCId f)

cat2 pgf f e =
  case fmap unType (functionType pgf f) of
    Just (_,cat,_) -> cat
    Nothing        -> error ("Unknown function "++showCId f++" "++show e)