mirror of
https://github.com/GrammaticalFramework/gf-core.git
synced 2026-10-25 08:43:17 -06:00
now include only a minimal support for dependency trees
This commit is contained in:
1 parent
0a4bb035f9
commit
9f3d1b6424
2 files changed
+4
-150
No files matched your search
@@ -501,76 +501,6 @@ pgfCommands = Map.fromList [
|
|||||||
]
|
]
|
||||||
}),
|
}),
|
||||||
|
|
||||||
("vd", emptyCommandInfo {
|
|
||||||
longname = "visualize_dependency",
|
|
||||||
synopsis = "show word dependency tree graphically",
|
|
||||||
explanation = unlines [
|
|
||||||
"Prints a dependency tree in the .dot format (the graphviz format, default)",
|
|
||||||
"or LaTeX (flag -output=latex)",
|
|
||||||
"or the CoNLL/MaltParser format (flag -output=conll for training, malt_input",
|
|
||||||
"for unanalysed input).",
|
|
||||||
"By default, the last argument is the head of every abstract syntax",
|
|
||||||
"function; moreover, the head depends on the head of the function above.",
|
|
||||||
"The graph can be saved in a file by the wf command as usual.",
|
|
||||||
"If the -view flag is defined, the graph is saved in a temporary file",
|
|
||||||
"which is processed by dot (graphviz) and displayed by the program indicated",
|
|
||||||
"by the view flag. The target format is png, unless overridden by the",
|
|
||||||
"flag -format. Results from multiple trees are combined to pdf with convert (ImageMagick).",
|
|
||||||
"See also 'vp -showdep' for another visualization of dependencies."
|
|
||||||
],
|
|
||||||
exec = needPGF $ \ opts arg pgf -> do
|
|
||||||
let absname = abstractName pgf
|
|
||||||
let es = toExprs arg
|
|
||||||
let debug = isOpt "v" opts
|
|
||||||
let abslabels = valStrOpts "abslabels" (valStrOpts "file" "" opts) opts
|
|
||||||
let cnclabels = valStrOpts "cnclabels" "" opts
|
|
||||||
let outp = valStrOpts "output" "dot" opts
|
|
||||||
let graphvizDependencyTree = error "TODO: graphvizDependencyTree"
|
|
||||||
let getDepLabels = error "TODO: getDepLabels"
|
|
||||||
let getCncDepLabels = error "TODO: getCncDepLabels"
|
|
||||||
let conlls2latexDoc = error "TODO: conlls2latexDoc"
|
|
||||||
mlab <- case abslabels of
|
|
||||||
"" -> return Nothing
|
|
||||||
_ -> (Just . getDepLabels) `fmap` restricted (readFile abslabels)
|
|
||||||
mclab <- case cnclabels of
|
|
||||||
"" -> return Nothing
|
|
||||||
_ -> (Just . getCncDepLabels) `fmap` restricted (readFile cnclabels)
|
|
||||||
concr <- optLang pgf opts
|
|
||||||
let grphs = map (graphvizDependencyTree outp debug mlab mclab concr) es
|
|
||||||
if isOpt "conll2latex" opts
|
|
||||||
then return $ fromString $ conlls2latexDoc $ stanzas $ unlines $ toStrings arg
|
|
||||||
else if isFlag "view" opts && valStrOpts "output" "" opts == "latex"
|
|
||||||
then do
|
|
||||||
let view = optViewGraph opts
|
|
||||||
viewLatex view "_grphd_" grphs
|
|
||||||
else if isFlag "view" opts || isFlag "format" opts
|
|
||||||
then do
|
|
||||||
let view = optViewGraph opts
|
|
||||||
let format = optViewFormat opts
|
|
||||||
viewGraphviz view format "_grphd_" grphs
|
|
||||||
else return $ fromString $ unlines $ intersperse "" grphs,
|
|
||||||
examples = [
|
|
||||||
mkEx "gr | vd -- generate a tree and show dependency tree in .dot",
|
|
||||||
mkEx "gr | vd -view=open -- generate a tree and display dependency tree on with Mac's 'open'",
|
|
||||||
mkEx "gr | vd -view=open -output=latex -- generate a tree and display latex dependency tree with Mac's 'open'",
|
|
||||||
mkEx "gr -number=1000 | vd -abslabels=Lang.labels -cnclabels=LangSwe.labels -output=conll -- generate a random treebank",
|
|
||||||
mkEx "rf -file=ex.conll | vd -conll2latex | wf -file=ex.tex -- convert conll file to latex"
|
|
||||||
],
|
|
||||||
options = [
|
|
||||||
("v","show extra information"),
|
|
||||||
("conll2latex", "convert conll to latex")
|
|
||||||
],
|
|
||||||
flags = [
|
|
||||||
("abslabels","abstract configuration file for labels, format per line 'fun label*'"),
|
|
||||||
("cnclabels","concrete configuration file for labels, format per line 'fun {words|*} pos label head'"),
|
|
||||||
("file", "same as abslabels (abstract configuration file)"),
|
|
||||||
("format", "format of the visualization file using dot (default \"png\")"),
|
|
||||||
("output", "output format of graph source (latex, conll, dot (default but deprecated))"),
|
|
||||||
("view", "program to open the resulting graph file (default \"open\")"),
|
|
||||||
("lang", "the language of analysis")
|
|
||||||
]
|
|
||||||
}),
|
|
||||||
|
|
||||||
("vp", emptyCommandInfo {
|
("vp", emptyCommandInfo {
|
||||||
longname = "visualize_parse",
|
longname = "visualize_parse",
|
||||||
synopsis = "show parse tree graphically",
|
synopsis = "show parse tree graphically",
|
||||||
@@ -587,7 +517,7 @@ pgfCommands = Map.fromList [
|
|||||||
let gvOptions = GraphvizOptions {noLeaves = isOpt "noleaves" opts && not (isOpt "showleaves" opts),
|
let gvOptions = GraphvizOptions {noLeaves = isOpt "noleaves" opts && not (isOpt "showleaves" opts),
|
||||||
noFun = isOpt "nofun" opts || not (isOpt "showfun" opts),
|
noFun = isOpt "nofun" opts || not (isOpt "showfun" opts),
|
||||||
noCat = isOpt "nocat" opts && not (isOpt "showcat" opts),
|
noCat = isOpt "nocat" opts && not (isOpt "showcat" opts),
|
||||||
noDep = not (isOpt "showdep" opts),
|
noDep = True,
|
||||||
nodeFont = valStrOpts "nodefont" "" opts,
|
nodeFont = valStrOpts "nodefont" "" opts,
|
||||||
leafFont = valStrOpts "leaffont" "" opts,
|
leafFont = valStrOpts "leaffont" "" opts,
|
||||||
nodeColor = valStrOpts "nodecolor" "" opts,
|
nodeColor = valStrOpts "nodecolor" "" opts,
|
||||||
@@ -605,8 +535,7 @@ pgfCommands = Map.fromList [
|
|||||||
else return $ fromString $ unlines grphs,
|
else return $ fromString $ unlines grphs,
|
||||||
examples = [
|
examples = [
|
||||||
mkEx "p \"John walks\" | vp -- generate a tree and show parse tree as .dot script",
|
mkEx "p \"John walks\" | vp -- generate a tree and show parse tree as .dot script",
|
||||||
mkEx "gr | vp -view=open -- generate a tree and display parse tree on a Mac",
|
mkEx "gr | vp -view=open -- generate a tree and display parse tree on a Mac"
|
||||||
mkEx "p \"she loves us\" | vp -view=open -showdep -file=uddeps.labels -nocat -- show a visual variant of a dependency tree"
|
|
||||||
],
|
],
|
||||||
options = [
|
options = [
|
||||||
("showcat","show categories in the tree nodes (default)"),
|
("showcat","show categories in the tree nodes (default)"),
|
||||||
|
|||||||
@@ -114,7 +114,6 @@ module PGF(
|
|||||||
graphvizAbstractTree,
|
graphvizAbstractTree,
|
||||||
graphvizParseTree,
|
graphvizParseTree,
|
||||||
graphvizParseTreeDep,
|
graphvizParseTreeDep,
|
||||||
graphvizDependencyTree,
|
|
||||||
graphvizBracketedString,
|
graphvizBracketedString,
|
||||||
graphvizAlignment,
|
graphvizAlignment,
|
||||||
gizaAlignment,
|
gizaAlignment,
|
||||||
@@ -122,7 +121,6 @@ module PGF(
|
|||||||
PGF2.graphvizDefaults,
|
PGF2.graphvizDefaults,
|
||||||
-- extra:
|
-- extra:
|
||||||
Labels, getDepLabels,
|
Labels, getDepLabels,
|
||||||
CncLabels, getCncDepLabels,
|
|
||||||
|
|
||||||
) where
|
) where
|
||||||
|
|
||||||
@@ -410,72 +408,12 @@ graphvizParseTree gr (CId lang) opts t =
|
|||||||
Nothing -> error ("Unknown language: " ++ lang)
|
Nothing -> error ("Unknown language: " ++ lang)
|
||||||
|
|
||||||
type Labels = Map.Map CId [String]
|
type Labels = Map.Map CId [String]
|
||||||
type CncLabels = [CncLabel]
|
|
||||||
|
|
||||||
data CncLabel =
|
|
||||||
CncSyncat (String, String -> Maybe (String -> String,String,String))
|
|
||||||
-- (fun, word/lemma -> (pos,label,target))
|
|
||||||
-- the pos can remain unchanged, as in the current notation in the article
|
|
||||||
| CncMorpho (String,[String])
|
|
||||||
-- (category, features in ascending order)
|
|
||||||
| CncForm (String,(String,String))
|
|
||||||
-- (wordform, (lemma,features))
|
|
||||||
|
|
||||||
-- | Prepare lines obtained from a configuration file for labels for
|
-- | Prepare lines obtained from a configuration file for labels for
|
||||||
-- use with 'graphvizDependencyTree'. Format per line /fun/ /label/@*@.
|
-- use with 'graphvizParseTreeDep'. Format per line /fun/ /label/@*@.
|
||||||
--- ignore other gf-ud annotatations than #fun and #cat at this point
|
--- ignore other gf-ud annotatations than #fun and #cat at this point
|
||||||
getDepLabels :: String -> Labels
|
getDepLabels :: String -> Labels
|
||||||
getDepLabels s = Map.fromList [(mkCId f,ls) | f:ls <- map (words . rmcomments) (lines s), not (head f == '#')]
|
getDepLabels s = Map.fromList [(mkCId f,ls) | f:ls <- map (words . rmcomments) (lines s), not (take 1 f == "#")]
|
||||||
|
|
||||||
getCncDepLabels :: String -> CncLabels
|
|
||||||
getCncDepLabels s = wlabels ws ++ flabels fs
|
|
||||||
where
|
|
||||||
wlabels =
|
|
||||||
map CncSyncat .
|
|
||||||
map merge .
|
|
||||||
groupBy (\ (x,_) (a,_) -> x == a) .
|
|
||||||
sortBy (comparing fst) .
|
|
||||||
concatMap analyse .
|
|
||||||
filter chooseW
|
|
||||||
|
|
||||||
flabels =
|
|
||||||
map CncMorpho .
|
|
||||||
map collectTags .
|
|
||||||
map words
|
|
||||||
|
|
||||||
(fs,ws) = partition chooseF $ map uncomment $ lines s
|
|
||||||
|
|
||||||
--- choose is for compatibility with the general notation
|
|
||||||
chooseW line = notElem '(' line &&
|
|
||||||
elem '{' line
|
|
||||||
--- ignoring non-local (with "(") and abstract (without "{") rules
|
|
||||||
---- TODO: this means that "(" cannot be a token
|
|
||||||
|
|
||||||
chooseF line = take 1 line == "@" --- feature assignments have the form e.g. @N SgNom SgGen ; no spaces inside tags
|
|
||||||
|
|
||||||
uncomment line = case line of
|
|
||||||
'-':'-':_ -> ""
|
|
||||||
c:cs -> c : uncomment cs
|
|
||||||
_ -> line
|
|
||||||
|
|
||||||
analyse line = case break (=='{') line of
|
|
||||||
(beg,_:ws) -> case break (=='}') ws of
|
|
||||||
(toks,_:target) -> case (getToks beg, words target) of
|
|
||||||
(funs,[ label,j]) -> [(fun, (tok, (id, label,j))) | fun <- funs, tok <- getToks toks]
|
|
||||||
(funs,[pos,label,j]) -> [(fun, (tok, (const pos,label,j))) | fun <- funs, tok <- getToks toks]
|
|
||||||
_ -> []
|
|
||||||
_ -> []
|
|
||||||
_ -> []
|
|
||||||
merge rules@((fun,_):_) = (fun, \tok ->
|
|
||||||
case lookup tok (map snd rules) of
|
|
||||||
Just new -> return new
|
|
||||||
_ -> lookup "*" (map snd rules)
|
|
||||||
)
|
|
||||||
getToks = map unquote . filter (/=",") . toks
|
|
||||||
toks s = case lex s of [(t,"")] -> [t] ; [(t,cc)] -> t:toks cc ; _ -> []
|
|
||||||
unquote s = case s of '"':cc@(_:_) | last cc == '"' -> init cc ; _ -> s
|
|
||||||
|
|
||||||
collectTags (w:ws) = (tail w,ws)
|
|
||||||
|
|
||||||
-- auxiliaries for UD conversion PK 15/12/2018
|
-- auxiliaries for UD conversion PK 15/12/2018
|
||||||
rmcomments :: String -> String
|
rmcomments :: String -> String
|
||||||
@@ -486,19 +424,6 @@ rmcomments s = case s of
|
|||||||
x:xs -> x : rmcomments xs
|
x:xs -> x : rmcomments xs
|
||||||
_ -> []
|
_ -> []
|
||||||
|
|
||||||
-- | Visualize word dependency tree.
|
|
||||||
graphvizDependencyTree
|
|
||||||
:: String -- ^ Output format: @"latex"@, @"conll"@, @"malt_tab"@, @"malt_input"@ or @"dot"@
|
|
||||||
-> Bool -- ^ Include extra information (debug)
|
|
||||||
-> Maybe Labels -- ^ abstract label information obtained with 'getDepLabels'
|
|
||||||
-> Maybe CncLabels -- ^ concrete label information obtained with ' ' (was: unused (was: @Maybe String@))
|
|
||||||
-> PGF
|
|
||||||
-> CId -- ^ The language of analysis
|
|
||||||
-> Tree
|
|
||||||
-> String -- ^ Rendered output in the specified format
|
|
||||||
graphvizDependencyTree format debug mb_labels mb_cnclabels gr (CId lang) t =
|
|
||||||
error "TODO: graphvizDependencyTree"
|
|
||||||
|
|
||||||
graphvizParseTreeDep :: Maybe Labels -> PGF -> Language -> GraphvizOptions -> Tree -> String
|
graphvizParseTreeDep :: Maybe Labels -> PGF -> Language -> GraphvizOptions -> Tree -> String
|
||||||
graphvizParseTreeDep mbl pgf lang opts tree = graphvizBracketedString opts mbl tree $ bracketedLinearize pgf lang tree
|
graphvizParseTreeDep mbl pgf lang opts tree = graphvizBracketedString opts mbl tree $ bracketedLinearize pgf lang tree
|
||||||
|
|
||||||
|
|||||||
Reference in new issue
Block a user