summaryrefslogtreecommitdiff
path: root/src-3.0/GF/Data/Trie2.hs
diff options
context:
space:
mode:
authoraarne <aarne@cs.chalmers.se>2008-05-21 09:26:44 +0000
committeraarne <aarne@cs.chalmers.se>2008-05-21 09:26:44 +0000
commit055c0d0d5a5bb0dc75904fe53df7f2e4f5732a8f (patch)
tree0e63fb68c69c8f6ad0f78893c63420f0a3600e1c /src-3.0/GF/Data/Trie2.hs
parent915a1de71783ab8446b1af9e72c7ba7dfbc12d3f (diff)
GF/src is now for 2.9, and the new sources are in src-3.0 - keep it this way until the release of GF 3
Diffstat (limited to 'src-3.0/GF/Data/Trie2.hs')
-rw-r--r--src-3.0/GF/Data/Trie2.hs120
1 files changed, 120 insertions, 0 deletions
diff --git a/src-3.0/GF/Data/Trie2.hs b/src-3.0/GF/Data/Trie2.hs
new file mode 100644
index 000000000..36fcc3221
--- /dev/null
+++ b/src-3.0/GF/Data/Trie2.hs
@@ -0,0 +1,120 @@
+----------------------------------------------------------------------
+-- |
+-- Module : Trie2
+-- Maintainer : Markus Forsberg
+-- Stability : Stable
+-- Portability : Haskell 98
+--
+-- > CVS $Date: 2005/04/21 16:22:10 $
+-- > CVS $Author: bringert $
+-- > CVS $Revision: 1.7 $
+--
+-- (Description of the module)
+-----------------------------------------------------------------------------
+
+module GF.Data.Trie2 (
+ tcompile,
+ collapse,
+ Trie,
+ trieLookup,
+ decompose,
+ --- Attr, atW, atP, atWP,
+ emptyTrie
+ ) where
+
+import GF.Data.Map
+import Data.List
+
+newtype TrieT a b = TrieT ([(a,TrieT a b)],[b])
+
+newtype Trie a b = Trie (Map a (Trie a b), [b])
+
+emptyTrieT = TrieT ([],[])
+
+emptyTrie :: Trie a b
+emptyTrie = Trie (empty,[])
+
+optimize :: (Ord a,Eq b) => TrieT a b -> Trie a b
+optimize (TrieT (xs,res)) = Trie ([(c,optimize t) | (c,t) <- xs] |->+ empty,
+ nub res) --- nub by AR
+
+collapse :: Ord a => Trie a b -> [([a],[b])]
+collapse trie = collapse' trie []
+ where collapse' (Trie (map,(x:xs))) s = if (isEmpty map) then [(reverse s,(x:xs))]
+ else (reverse s,(x:xs)):
+ concat [ collapse' trie (c:s) | (c,trie) <- flatten map]
+ collapse' (Trie (map,[])) s
+ = concat [ collapse' trie (c:s) | (c,trie) <- flatten map]
+
+tcompile :: (Ord a,Eq b) => [([a],[b])] -> Trie a b
+tcompile xs = optimize $ build xs emptyTrieT
+
+build :: Ord a => [([a],[b])] -> TrieT a b -> TrieT a b
+build [] trie = trie
+build (x:xs) trie = build xs (insert x trie)
+ where
+ insert ([],ys) (TrieT (xs,res)) = TrieT (xs,ys ++ res)
+ insert ((s:ss),ys) (TrieT (xs,res))
+ = case (span (\(s',_) -> s' /= s) xs) of
+ (xs,[]) -> TrieT (((s,(insert (ss,ys) emptyTrieT)):xs),res)
+ (xs,(y,trie):zs) -> TrieT (xs ++ ((y,insert (ss,ys) trie):zs),res)
+
+trieLookup :: Ord a => Trie a b -> [a] -> ([a],[b])
+trieLookup trie s = apply trie s s
+
+apply :: Ord a => Trie a b -> [a] -> [a] -> ([a],[b])
+apply (Trie (_,res)) [] inp = (inp,res)
+apply (Trie (map,_)) (s:ss) inp
+ = case map ! s of
+ Just trie -> apply trie ss inp
+ Nothing -> (inp,[])
+
+-----------------------------
+-- from Trie for strings; simplified for GF by making binding always possible (AR)
+
+decompose :: Ord a => Trie a b -> [a] -> [[a]]
+decompose trie sentence = backtrack [(sentence,[])] trie
+
+react :: Ord a => [a] -> [[a]] -> [([a],[[a]])] ->
+ [a] -> Trie a b -> Trie a b -> [[a]]
+-- String -> [String] -> [(String,[String])] -> String -> Trie -> Trie -> [String]
+react input output back occ (Trie (arcs,res)) init =
+ case res of -- Accept = non-empty res.
+ [] -> continue back
+ _ -> let pushout = (occ:output)
+ in case input of
+ [] -> reverse $ map reverse pushout
+ _ -> let pushback = ((input,pushout):back)
+ in continue pushback
+ where continue cont = case input of
+ [] -> backtrack cont init
+ (l:rest) -> case arcs ! l of
+ Just trie ->
+ react rest output cont (l:occ) trie init
+ Nothing -> backtrack cont init
+
+backtrack :: Ord a => [([a],[[a]])] -> Trie a b -> [[a]]
+backtrack [] _ = []
+backtrack ((input,output):back) trie
+ = react input output back [] trie trie
+
+
+{- so this is not needed from the original
+type Attr = Int
+
+atW, atP, atWP :: Attr
+(atW,atP,atWP) = (0,1,2)
+
+decompose :: Ord a => Trie a (Int,b) -> [a] -> [[a]]
+decompose trie sentence = legal trie $ backtrack [(sentence,[])] trie
+
+-- The function legal checks if the decomposition is in fact a possible one.
+
+legal :: Ord a => Trie a (Int,b) -> [[a]] -> [[a]]
+legal _ [] = []
+legal trie input = if (test (map ((map fst).snd.(trieLookup trie)) input)) then input else []
+ where
+ test [] = False
+ test [xs] = elem atW xs || elem atWP xs
+ test (xs:xss) = (elem atP xs || elem atWP xs) && test xss
+-}