forked from GrammaticalFramework/gf-ud
-
Notifications
You must be signed in to change notification settings - Fork 1
/
UDVisualization.hs
78 lines (65 loc) · 2.56 KB
/
UDVisualization.hs
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
module UDVisualization where
import RTree
import UDConcepts
import GFConcepts
import UDAnnotations
import DBNF as D
import PGF
import System.Process (system)
visualizeAbsTrees :: UDEnv -> [AbsTree] -> IO ()
visualizeAbsTrees env ts = do
let astfile = "_gfud_ast_tmp"
absTrees2latex env astfile ts
system $ "pdflatex " ++ (astfile ++ ".tex") ++ " >/dev/null"
system $ "open " ++ (astfile ++ ".pdf") ---- TODO: parameterize open command
return ()
absTrees2latex :: UDEnv -> FilePath -> [AbsTree] -> IO ()
absTrees2latex env file ts = do
let exps = map abstree2expr ts
let codes = map (graphvizAbstractTree (pgfGrammar env) (True,False)) exps
let astDotFile i suff = "_" ++ show i ++ file ++ "." ++ suff
mapM_ (\ (i,code) -> writeFile (astDotFile i "dot") code) (zip [1..] codes)
mapM_ (\i -> system ("dot -Teps " ++ astDotFile i "dot" ++ " >" ++ astDotFile i "eps")) [1.. length ts]
writeFile (file ++ ".tex") $ unlines [
"\\documentclass{article}",
"\\usepackage[utf8]{inputenc}",
"\\usepackage{graphicx}",
"\\begin{document}",
""
]
let treeSize t = case length (leavesRTree t) of
n | n < 3 -> 0.2
n | n < 5 -> 0.4
n | n < 8 -> 0.6
n | n < 12 -> 0.8
_ -> 1.0
let width i = show (treeSize (ts !! i))
mapM_ (\i -> appendFile (file ++ ".tex") ("\n\n\\includegraphics[width=" ++ width (i-1) ++ "\\textwidth]{" ++ astDotFile i "eps" ++ "}")) [1.. length ts]
appendFile (file ++ ".tex") "\\end{document}"
visualizeUDSentences :: [UDSentence] -> IO ()
visualizeUDSentences uds = do
let doc = ud2latex uds
writeFile "_ud_tmp.tex" doc
system $ "pdflatex _ud_tmp.tex >/dev/null"
system $ "open _ud_tmp.pdf" ---- TODO: parameterize open command
return ()
ud2latex :: [UDSentence] -> String
ud2latex =
conlls2latexDoc .
map (unlines . map (trim . prt) . udWordLines)
where
trim = concatMap (\c -> if elem c "%$&" then "\\"++[c] else [c])
--- pretends that parse trees are abstract trees, for easy visualization
visualizeParseTrees :: [D.ParseTree] -> IO ()
visualizeParseTrees = visualizeAbsTrees initUDEnv . map p2a
where
p2a pt = case pt of
PT (cat,_,_,_) pts -> RTree (mkCId cat) (map p2a pts)
PL (cat,tok) _ -> RTree (mkCId cat) [RTree (mkCId tok) []]
-- real parse tree
-- graphvizBracketedString :: GraphvizOptions -> Maybe Labels -> Tree -> [BracketedString] -> String
-- graphvizDefaults :: GraphvizOptions
-- type Labels = Map CId [String]
-- Tree
selectParseTrees :: [String] -> [String]
selectParseTrees ls = [unwords ws | l <- ls, "#":"parsetree": "=" : ws <- [words l]]