summaryrefslogtreecommitdiff
path: root/src/GF/Speech/FiniteState.hs
blob: 100335a2d4b1a2906d38da595b1a7f95f13e1e8b (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
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
----------------------------------------------------------------------
-- |
-- Module      : FiniteState
-- Maintainer  : BB
-- Stability   : (stable)
-- Portability : (portable)
--
-- > CVS $Date: 2005/09/12 22:32:24 $ 
-- > CVS $Author: bringert $
-- > CVS $Revision: 1.6 $
--
-- A simple finite state network module.
-----------------------------------------------------------------------------
module GF.Speech.FiniteState (FA, State,
			      startState, finalStates,
			      states, transitions,
			      newFA, 
			      addFinalState,
			      newState, newTransition,
			      mapStates, mapTransitions,
			      moveLabelsToNodes, minimize,
			      prGraphGraphviz) where

import Data.List
import Data.Maybe (fromJust)

import Debug.Trace

data FA a b = FA (Graph a b) State [State]

type State = Node

startState :: FA a b -> State
startState (FA _ s _) = s

finalStates :: FA a b -> [State]
finalStates (FA _ _ ss) = ss

states :: FA a b -> [(State,a)]
states (FA g _ _) = nodes g

transitions :: FA a b -> [(State,State,b)]
transitions (FA g _ _) = edges g

newFA :: a -- ^ Start node label
      -> FA a b
newFA l = FA g s []
    where (g,s) = newNode l newGraph

addFinalState :: Node -> FA a b -> FA a b
addFinalState f (FA g s ss) = FA g s (f:ss)

newState :: a -> FA a b -> (FA a b, State)
newState x (FA g s ss) = (FA g' s ss, n)
    where (g',n) = newNode x g

newTransition :: Node -> Node -> b -> FA a b -> FA a b
newTransition f t l = onGraph (newEdge f t l)

mapStates :: (a -> c) -> FA a b -> FA c b
mapStates f = onGraph (nmap f)

mapTransitions :: (b -> c) -> FA a b -> FA a c
mapTransitions f = onGraph (emap f)

asGraph :: FA a b -> Graph a b
asGraph (FA g _ _) = g

minimize :: FA () (Maybe a) -> FA () (Maybe a)
minimize = onGraph mimimizeGr1

-- | Transform a standard finite automaton with labelled edges
--   to one where the labels are on the nodes instead. This can add
--   up to one extra node per edge.
moveLabelsToNodes :: Eq a => FA () (Maybe a) -> FA (Maybe a) ()
moveLabelsToNodes = onGraph moveLabelsToNodes_

prGraphGraphviz :: FA String String -> String
prGraphGraphviz (FA (Graph _ ns es) _ _) = 
    "digraph {\n" ++ unlines (map prNode ns)
		  ++ "\n"
		  ++ unlines (map prEdge es)
			 ++ "\n}\n"
    where prNode (n,l) = show n ++ " [label = " ++ show l ++ "]"
	  prEdge (f,t,l) = show f ++ " -> " ++ show t ++ " [label = " ++ show l ++ "]"

--
-- * Graphs 
--
type Node = Int

data Graph a b = Graph Node [(Node,a)] [(Node,Node,b)]
		 deriving (Eq,Show)

onGraph :: (Graph a b -> Graph c d) -> FA a b -> FA c d
onGraph f (FA g s ss) = FA (f g) s ss

-- graphToFA :: State -> [State] -> Graph a b -> FA a b
-- graphToFA s fs (Graph _ ss ts) = buildFA s fs ss ts

newGraph :: Graph a b
newGraph = Graph 0 [] []

nodes :: Graph a b -> [(Node,a)]
nodes (Graph _ ns _) = ns

edges :: Graph a b -> [(Node,Node,b)]
edges (Graph _ _ es) = es

nmap :: (a -> c) -> Graph a b -> Graph c b
nmap f (Graph c ns es) = Graph c [(n,f l) | (n,l) <- ns] es

emap :: (b -> c) -> Graph a b -> Graph a c
emap f (Graph c ns es) = Graph c ns [(x,y,f l) | (x,y,l) <- es]

newNode :: a -> Graph a b -> (Graph a b,State)
newNode l (Graph c ns es) = (Graph s ((s,l):ns) es, s)
  where s = c+1

newEdge :: State -> State -> b -> Graph a b -> Graph a b
newEdge f t l (Graph c ns es) = Graph c ns ((f,t,l):es)

incoming :: Graph a b -> [(Node,a,[(Node,Node,b)])]
incoming (Graph _ ns es) = snd $ mapAccumL f (sortBy compareDest es) (sortBy compareFst ns)
  where destIs d (_,t,_) = t == d
        compareDest (_,t1,_) (_,t2,_) = compare t1 t2
	compareFst p1 p2 = compare (fst p1) (fst p2)
	f es' (n,l) = let (nes,es'') = span (destIs n) es' in (es'',(n,l,nes))

moveLabelsToNodes_ :: Eq a => Graph () (Maybe a) -> Graph (Maybe a) ()
moveLabelsToNodes_ gr@(Graph c _ _) = mimimizeGr2 $ Graph c' (zip ns ls) (concat ess)
  where is = incoming gr
	(c',is') = mapAccumL fixIncoming c is
	(ns,ls,ess) = unzip3 (concat is')

fixIncoming :: Eq a => Node -> (Node,(),[(Node,Node,Maybe a)]) -> (Node,[(Node,Maybe a,[(Node,Node,())])])
fixIncoming next c@(n,(),es) = (next', (n,Nothing,es'):newContexts)
  where ls = nub $ map getLabel es
	next' = next + length ls
	newNodes = zip [next..next'-1] ls
	es' = [ (x,n,()) | x <- map fst newNodes ]
	-- separate cyclic and non-cyclic edges
	(cyc,ncyc) = partition (\ (f,_,_) -> f == n) es
	       -- keep all incoming non-cyclic edges with the right label
	to x l = [ (f,x,()) | (f,_,l') <- ncyc, l == l']
	       -- for each cyclic edge with the right label, 
	       -- add an edge from each of the new nodes (including this one)
		 ++ [ (y,x,()) | (f,_,l') <- cyc, l == l', (y,_) <- newNodes]
	newContexts = [ (x, l, to x l) | (x,l) <- newNodes ]

getLabel :: (Node,Node,b) -> b
getLabel (_,_,l) = l

mimimizeGr1 :: Graph () (Maybe a) -> Graph () (Maybe a)
mimimizeGr1 = removeEmptyLoops1

removeEmptyLoops1 :: Graph () (Maybe a) -> Graph () (Maybe a)
removeEmptyLoops1 (Graph c ns es) = Graph c ns (filter (not . isEmptyLoop) es)
    where isEmptyLoop (f,t,Nothing) | f == t = True
	  isEmptyLoop _ = False

mimimizeGr2 :: Graph (Maybe a) () -> Graph (Maybe a) ()
mimimizeGr2 = id

removeDuplicateEdges :: Ord b => Graph a b -> Graph a b
removeDuplicateEdges (Graph c ns es) = Graph c ns (sortNub es)




--
-- * Utilities
-- 

sortNub :: Ord a => [a] -> [a]
sortNub = map head . group . sort