summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorra.monique <ra.monique@gmail.com>2011-09-15 16:32:49 +0000
committerra.monique <ra.monique@gmail.com>2011-09-15 16:32:49 +0000
commitbdc77bf0e4c4b705a3deb5976271dc1fd3df3baf (patch)
treed231656ea3681cc1e801f5f6790603be541ebe1a /src
parent751fd79763b4c3e76f97dc7cbb990ef3dcbebdca (diff)
added topological sort module to PGF - to be used in example based grammar writing
Diffstat (limited to 'src')
-rw-r--r--src/runtime/haskell/PGF.hs7
-rw-r--r--src/runtime/haskell/PGF/SortTop.hs96
2 files changed, 102 insertions, 1 deletions
diff --git a/src/runtime/haskell/PGF.hs b/src/runtime/haskell/PGF.hs
index 8530d9a71..cff225f08 100644
--- a/src/runtime/haskell/PGF.hs
+++ b/src/runtime/haskell/PGF.hs
@@ -109,7 +109,8 @@ module PGF(
-- ** Morphological Analysis
Lemma, Analysis, Morpho,
lookupMorpho, buildMorpho, fullFormLexicon,
-
+ morphoMissing,
+
-- ** Tokenizing
mkTokenizer,
@@ -128,12 +129,16 @@ module PGF(
showProbabilities,
readProbabilitiesFromFile,
+ -- ** SortTop
+ forExample,
+
-- * Browsing
browse
) where
import PGF.CId
import PGF.Linearize
+import PGF.SortTop
import PGF.Generate
import PGF.TypeCheck
import PGF.Paraphrase
diff --git a/src/runtime/haskell/PGF/SortTop.hs b/src/runtime/haskell/PGF/SortTop.hs
new file mode 100644
index 000000000..275698af6
--- /dev/null
+++ b/src/runtime/haskell/PGF/SortTop.hs
@@ -0,0 +1,96 @@
+module PGF.SortTop
+ ( forExample
+ ) where
+
+import PGF.Linearize
+import PGF.Macros
+import System.IO
+import PGF.CId
+import PGF.Data
+import PGF.Macros
+import PGF.Expr
+import Data.Array.IArray
+import Data.List
+import Control.Monad
+import qualified Data.Map as Map
+import qualified Data.IntMap as IntMap
+import qualified Data.Set as Set
+import Data.Maybe
+import System.Environment (getArgs)
+import Data.Binary
+
+
+
+
+
+arguments :: Type -> [CId]
+arguments (DTyp [] _ _) = []
+arguments (DTyp hypos _ _) = [ t | (_,_, DTyp _ t _) <- hypos]
+
+-- topological order of functions
+-- in the order that they should be tested and generated in an example-based system
+
+showInOrder :: Abstr -> Set.Set CId -> Set.Set CId -> Set.Set CId -> IO [[((CId,CId),[CId])]]
+showInOrder abs fset remset avset =
+ let mtypes = typesInterm abs fset
+ nextsetWithArgs = Set.map (\(x,y) -> ((x, returnCat abs x), fromJust y)) $ Set.filter (isJust.snd) $ Set.map (\x -> (x, isArg abs mtypes avset x)) remset
+ nextset = Set.map (fst.fst) nextsetWithArgs
+ nextcat = Set.map (returnCat abs) nextset
+ diffset = Set.difference remset nextset
+ in
+ if Set.null diffset then do
+ return [Set.toList nextsetWithArgs]
+ else if Set.null nextset then do
+ putStrLn $ "not comparable : " ++ show diffset
+ return []
+ else do
+
+ rest <- showInOrder abs (Set.union fset nextset) (Set.difference remset nextset) (Set.union avset nextcat)
+ return $ (Set.toList nextsetWithArgs) : rest
+
+
+isArg :: Abstr -> Map.Map CId CId -> Set.Set CId -> CId -> Maybe [CId]
+isArg abs mtypes scid cid =
+ let p = Map.lookup cid $ funs abs
+ (ty,_,_,_) = fromJust p
+ args = arguments ty
+ setargs = Set.fromList args
+ cond = Set.null $ Set.difference setargs scid
+ in
+ if isNothing p then error $ "not found " ++ show cid ++ "here !!"
+ else if cond then return args
+ else Nothing
+
+typesInterm :: Abstr -> Set.Set CId -> Map.Map CId CId
+typesInterm abs fset =
+ let fs = funs abs
+ fsetTypes = Set.map (\x ->
+ let (DTyp _ c _,_,_,_)=fromJust $ Map.lookup x fs
+ in (x,c)) fset
+ in Map.fromList $ Set.toList fsetTypes
+
+
+takeArgs :: Map.Map CId CId -> Map.Map CId Expr -> CId -> Expr
+takeArgs mtypes mexpr ty =
+ let xarg = head $ Map.keys $ Map.filter (==ty) mtypes
+ in fromJust $ Map.lookup xarg mexpr
+
+doesReturnCat :: Type -> CId -> Bool
+doesReturnCat (DTyp _ c _) cat = c == cat
+
+returnCat :: Abstr -> CId -> CId
+returnCat abs cid =
+ let p = Map.lookup cid $ funs abs
+ (DTyp _ c _,_,_,_) = fromJust p
+ in if isNothing p then error $ "not found "++ show cid ++ " in abstract "
+ else c
+
+-- topological order of the categories
+forExample :: PGF -> IO [[((CId,CId),[CId])]]
+forExample pgf = let abs = abstract pgf
+ in showInOrder abs Set.empty (Set.fromList $ Map.keys $ funs abs) Set.empty
+
+
+
+
+