diff options
| author | aarne <aarne@chalmers.se> | 2010-01-26 21:08:04 +0000 |
|---|---|---|
| committer | aarne <aarne@chalmers.se> | 2010-01-26 21:08:04 +0000 |
| commit | dd4c792e67a3124706bef57ab23ff542d2d0d961 (patch) | |
| tree | 5fc9953ce5b0809568bbdbf3f010d0909477bfe0 /src/runtime/haskell | |
| parent | e91c610e5afd0083574d2f28cda07a03fe52ea8f (diff) | |
probability ranking (rt) and gr -probs=FILE
Diffstat (limited to 'src/runtime/haskell')
| -rw-r--r-- | src/runtime/haskell/PGF/Probabilistic.hs | 19 |
1 files changed, 15 insertions, 4 deletions
diff --git a/src/runtime/haskell/PGF/Probabilistic.hs b/src/runtime/haskell/PGF/Probabilistic.hs index c0422a784..e42698cfe 100644 --- a/src/runtime/haskell/PGF/Probabilistic.hs +++ b/src/runtime/haskell/PGF/Probabilistic.hs @@ -2,8 +2,10 @@ module PGF.Probabilistic ( probTree -- :: Probabilities -> Tree -> Double ,rankTreesByProbs -- :: Probabilities -> [Tree] -> [Tree] ,Probabilities -- data + ,prProbabilities -- Probabilities -> String ,catProbs ,getProbsFromFile -- :: FilePath -> PGF -> IO Probabilities + ,defaultProbabilities -- :: PGF -> Probabilities ) where import PGF.CId @@ -18,6 +20,10 @@ data Probabilities = Probs { catProbs :: M.Map CId [(Double, (CId,[CId]))] -- prob and arglist } +prProbabilities :: Probabilities -> String +prProbabilities = unlines . map pr . M.toList . funProbs where + pr (f,d) = showCId f ++ "\t" ++ show d + getProbsFromFile :: FilePath -> PGF -> IO Probabilities getProbsFromFile file pgf = do s <- readFile file @@ -33,8 +39,8 @@ fillProbs pgf funs = | (cat,_) <- M.toList (cats (abstract pgf)), let fs = functionsToCat pgf cat] cats1 = map fill cats0 - funs1 = M.fromList [(f,p) | (_,cf) <- cats1, (p,(f,_)) <- cf] - in Probs funs1 (M.fromList cats1) + funs1 = [(f,p) | (_,cf) <- cats1, (p,(f,_)) <- cf] + in Probs (M.fromList funs1) (M.fromList cats1) where fill (cat,fs) = (cat, pad [(getProb0 f,(f,xs)) | (f,xs) <- fs]) where @@ -43,8 +49,13 @@ fillProbs pgf funs = pad :: [(Double,a)] -> [(Double,a)] pad pfs = [(if p== -1 then deflt else p,f) | (p,f) <- pfs] where - deflt = 1 - sum poss / fromIntegral (length negs) - (poss,negs) = partition (> (-1)) (map fst pfs) + deflt = case length negs of + 0 -> 0 + _ -> (1 - sum poss) / fromIntegral (length negs) + (poss,negs) = partition (> (-0.5)) (map fst pfs) + +defaultProbabilities :: PGF -> Probabilities +defaultProbabilities pgf = fillProbs pgf M.empty -- | compute the probability of a given tree probTree :: Probabilities -> Expr -> Double |
