summaryrefslogtreecommitdiff
path: root/src/GF/UseGrammar/Generate.hs
blob: d19b42f2f96f81d40ecced99cda1770db64dd080 (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
177
178
179
180
181
182
----------------------------------------------------------------------
-- |
-- Module      : Generate
-- Maintainer  : AR
-- Stability   : (stable)
-- Portability : (portable)
--
-- > CVS $Date: 2005/03/03 16:10:37 $ 
-- > CVS $Author: aarne $
-- > CVS $Revision: 1.10 $
--
-- Generate all trees of given category and depth. AR 30\/4\/2004
--
-- (c) Aarne Ranta 2004 under GNU GPL
--
-- Purpose: to generate corpora. We use simple types and don't
-- guarantee the correctness of bindings\/dependences.
-----------------------------------------------------------------------------

module Generate (generateTrees) where

import GFC
import LookAbs
import PrGrammar
import Macros
import Values
import Grammar (Cat)

import Operations
import Zipper

import List

-- Generate all trees of given category and depth. AR 30/4/2004
-- (c) Aarne Ranta 2004 under GNU GPL
--
-- Purpose: to generate corpora. We use simple types and don't
-- guarantee the correctness of bindings/dependences.


-- | the main function takes an abstract syntax and returns a list of trees
generateTrees :: GFCGrammar -> Bool -> Cat -> Int -> Maybe Int -> Maybe Tree -> [Exp]
generateTrees gr ifm cat n mn mt = map str2tr $ generate gr' ifm cat' n mn mt'
  where
    gr' = gr2sgr gr
    cat' = prt $ snd cat
    mt' = maybe Nothing (return . tr2str) mt

------------------------------------------
-- translate grammar to simpler form and generated trees back

gr2sgr :: GFCGrammar -> SGrammar
gr2sgr gr = buildTree [(c,rs) | rs@((_,(_,c)):_) <- rules] where
  rules =
    groupBy (\x y -> scat x == scat y) $
      sortBy (\x y -> compare (scat x) (scat y)) 
        [(trId f, ty') | (f,ty) <- funRulesOf gr, ty' <- trTy ty]
  trId = prt . snd
  trTy ty = case catSkeleton ty of
    Ok (mcs,mc) -> [(map trCat mcs, trCat mc)]
    _ -> []
  trCat (m,c) = prt c ---
  scat (_,(_,c)) = c

-- str2tr :: STree -> Exp
str2tr t = case t of
  SApp (f,ts) -> mkApp (trId f) (map str2tr ts) 
  SMeta _     -> mkMeta 0
----  SString s   -> K s
 where
   trId = cn . zIdent

-- tr2str :: Tree -> STree
tr2str (Tr (N (_,at,val,_,_),ts)) = case (at,val) of
  (AtC (_,f), _)         -> SApp (prt_ f,map tr2str ts)
  (AtM _,     VCn (_,c)) -> SMeta (prt_ c)
  (AtL s,     _)         -> SString s
  (AtI i,     _)         -> SInt i
  _ -> SMeta "FAILED_TO_GENERATE" ---- err monad!

------------------------------------------
-- do the main thing with a simpler data structure
-- the first Int gives tree depth, the second constrains subtrees
-- chosen for each branch. A small number, such as 2, is a good choice
-- if the depth is large (more than 3)
-- If a tree is given as argument, generation concerns its metavariables.

generate :: SGrammar -> Bool -> SCat -> Int -> Maybe Int -> Maybe STree -> [STree]
generate gr ifm cat i _ Nothing = errVal [] $ lookupTree id cat $ genAll i 
 where
  -- lazy bottom-up dynamic generation
  genAll :: Int -> BinTree (SCat,[STree])
  genAll i = iter i genNext (mapTree (\ (c,_) -> (c,[])) gr)

  iter 0 f tr = tr
  iter n f tr = f (iter (n-1) f tr)

  genNext tr = mapTree (genNew tr) tr

  genNew tr (cat,ts) = 
    (cat, [SApp (f, xs) | 
            (f,(cs,_)) <- funs cat, 
            xs <- combinations (map look cs),
            let fxs = SApp (f, xs),
            notElem fxs ts] 
         ++ ts)
   where
     look c = errVal [] $ lookupTree id c tr

  funs cat = errVal [] $ lookupTree id cat gr

generate gr ifm cat i mn mt = case mt of
  Nothing -> [t | (c,t) <- gen i [], c == cat] 

  Just t -> genM i t

 where

  gen :: Int -> [(SCat,STree)] -> [(SCat,STree)]
  gen n cts = if n==0 then cts else
    gen (n-1) ([(c,SApp (f, xs)) | (f,(cs,c)) <- allRules gr, xs <- args cs cts] ++ cts)

  args :: [SCat] -> [(SCat,STree)] -> [[STree]]
  args cs cts = combinations 
    [constr (ifmetas c [t | (k,t) <- cts, k == c]) | c <- cs]

  constr = maybe id take mn

  ifmetas c = if ifm then (SMeta c :) else id

  genM n t = case t of
    SApp (f,ts) -> [SApp (f,ts') | ts' <- combinations (map (genM (n-1)) ts)]
    SMeta k -> [t | (c,t) <- gen n [], c == k] 
    _ -> [t]

type SGrammar = BinTree (SCat,[SRule])
type SIdent = String
type SRule = (SFun,SType)
type SType = ([SCat],SCat)
type SCat = SIdent
type SFun = SIdent

allRules gr = concat [rs  | (c,rs) <- tree2list gr]

data STree = 
    SApp (SFun,[STree]) 
  | SMeta SCat
  | SString String
  | SInt Int
   deriving (Show,Eq)

------------------------------------------
-- to test

prSTree t = case t of
  SApp (f,ts) -> f ++ concat (map pr1 ts)
  SMeta c -> '?':c
  SString s -> prQuotedString s
  SInt i -> show i 
 where
  pr1 t@(SApp (_,ts)) = ' ' : (if null ts then id else prParenth) (prSTree t)
  pr1 t = prSTree t

pSRule :: String -> SRule
pSRule s = case words s of
  f : _ : cs -> (f,(init cs', last cs')) 
    where cs' = [cs !! i | i <- [0,2..length cs - 1]]
  _ -> error $ "not a rule" +++ s

exSgr = map pSRule [
   "Pred   : NP -> VP -> S"
  ,"Compl  : TV -> NP -> VP" 
  ,"PredVV : VV -> VP -> VP"
  ,"DefCN  : CN -> NP"
  ,"ModCN  : AP -> CN -> CN" 
  ,"john   : NP"
  ,"walk   : VP"
  ,"love   : TV"
  ,"try    : VV"
  ,"girl   : CN"
  ,"big    : AP"
  ]