dan*_*tin 3 string file-io haskell
此函数生成简单的.dot文件,以使用Graphviz可视化自动机转换函数.它的主要目的是调试大量自动生成的转换(例如,拉丁语动词的变形).
prepGraph :: ( ... ) => NFA c b a -> [String]
prepGraph nfa = "digraph finite_state_machine {"
: wrapSp "rankdir = LR"
: wrapSp ("node [shape = circle]" ++ (mapSp (states nfa \\ terminal nfa)))
: wrapSp ("node [shape = doublecircle]" ++ (mapSp $ terminal nfa))
: formatGraph nfa ++ ["}"]
formatGraph :: ( ... ) => NFA c b a -> [String]
formatGraph = map formatDelta . deltaTuples
where formatDelta (a, a', bc) = wrapSp (mkArrow a a' ++ " " ++ mkLabel bc)
mkArrow x y = show x ++ " -> " ++ show y
mkLabel (y, z) = case z of
(Just t) -> "[ label = \"(" ++ show y ++ ", " ++ show t ++ ")\" ]"
Nothing -> "[ label = \"(" ++ show y ++ ", " ++ "Null" ++ ")\" ]"
Run Code Online (Sandbox Code Playgroud)
where wrap,wrapSp和mapSp格式化函数,原样deltaTuples.
问题是formatGraph在字符串周围保留双引号,这会导致Graphviz中的错误.例如,当我打印unlines $ prepGraph到文件时,我得到的结果如下:
0 -> 1 [ label = "('a', "N. SF")" ];
Run Code Online (Sandbox Code Playgroud)
代替
0 -> 1 [ label = "('a', N. SF)" ];
Run Code Online (Sandbox Code Playgroud)
(但是,"Null"似乎工作正常,并且输出非常好).当然,字符串"N.SF"不是我用来存储变形的实际形式,但是该形式确实包含一个或两个String.那么我怎么能告诉Haskell:当你show输入String值时,不要双引号呢?
看看Martin Erwig如何在Data.Graph.Inductive.Graphviz中处理同样的问题:
您正在寻找的功能是底部的"sq":
sq :: String -> String
sq s@[c] = s
sq ('"':s) | last s == '"' = init s
| otherwise = s
sq ('\'':s) | last s == '\'' = init s
| otherwise = s
sq s = s
Run Code Online (Sandbox Code Playgroud)
(检查上下文并适应您自己的代码,当然)