在 tikz 依赖树上方绘制具有相同叶子的 tikz 树

在 tikz 依赖树上方绘制具有相同叶子的 tikz 树

我想实现以下目标:

我有两种不同的树想要合并:

\documentclass{article}
\usepackage{color,graphicx}
\usepackage{tikz}
\usepackage{tikz-dependency}
\usetikzlibrary{%
  shapes,%
  arrows,%
  positioning,%
  calc,%
  automata%
}
\definecolor{pf7}{RGB}{166, 118, 29}

\begin{document}
\begin{tikzpicture}[
    level/.style={sibling distance=2cm,
    level distance = 1cm}
]
    \tikzset{level 1/.style={sibling distance=4cm}}
    \node {S}
    child{
        node (np) {\textcolor{red}{\textbf{NP}}}
        child {
            node {D} child{node {The}}
        }
        child[red] {
            node {\textcolor{red}{\textbf{N}}} child[black]{node {cat}}
        }
    }
child{
    node (vp) {\textcolor{blue}{\textbf{VP}}}
    child[blue] {
        node {\textcolor{blue}{\textbf{V}}} child[black]{node {sat}}
    }
    child[blue] {
        node {\textcolor{blue}{\textbf{PP}}}
        child {
            node {\textcolor{blue}{\textbf{P}}} child[black]{node {on}}
        }
        child[black] {
            node {NP}
            child {
                node {D} child{node {the}}
            }
            child {
                node {N} child{node {mat}}
            }
        }
    }
}
;
\end{tikzpicture}
\end{document}

组成树

以及一个依赖树:

\documentclass{article}
\usepackage{color,graphicx}
\usepackage{tikz}
\usepackage{tikz-dependency}
\usetikzlibrary{%
  shapes,%
  arrows,%
  positioning,%
  calc,%
  automata%
}
\definecolor{pf7}{RGB}{166, 118, 29}

\begin{document}
\begin{dependency}
        \begin{deptext}[column sep=3em]
            \textbf{The} \& \textbf{cat} \& \textbf{sat} \& \textbf{on} \& \textbf{the} \& \textbf{mat} \\
            \textbf{1}    \& \textbf{2}     \& \textbf{3} \& \textbf{4}    \& \textbf{5}     \& \textbf{6} \\
        \end{deptext}
        \depedge[edge below]{3}{2}{\bf\textcolor{pf7}{nsubj}}
        \depedge[edge below]{4}{6}{\bf\textcolor{pf7}{pobj}}
        \depedge[edge below]{3}{4}{\bf\textcolor{pf7}{prep}}
        \depedge[edge below]{2}{1}{\bf\textcolor{pf7}{det}}
        \depedge[edge below]{6}{5}{\bf\textcolor{pf7}{det}}
    \end{dependency}
\end{document}

依赖树

到目前为止我已经设法得到了这个,但是 tikz 上层树没有对齐(就像以前一样)而且我不知道如何实现这一点。

\begin{dependency}
    \begin{deptext}[column sep=3em]
        \textbf{The} \& \textbf{cat} \& \textbf{sat} \& \textbf{on} \& \textbf{the} \& \textbf{mat} \\
        \textbf{1}    \& \textbf{2}     \& \textbf{3} \& \textbf{4}    \& \textbf{5}     \& \textbf{6} \\
    \end{deptext}

    \node[yshift=7cm] {S}
    child{
        node (np) {\textcolor{red}{\textbf{NP}}}
        child {
            node {D} child{node at ($(\wordref{1}{1})$) {}}
        }
        child[red] {
            node {\textcolor{red}{\textbf{N}}}
            child[black]{node at($(\wordref{1}{2})$) {}}
        }
    }
    child{
        node (vp) {\textcolor{blue}{\textbf{VP}}}
        child[blue] {
            node {\textcolor{blue}{\textbf{V}}}
            child[black]{node at($(\wordref{1}{3})$) {}}
        }
        child[blue] {
            node {\textcolor{blue}{\textbf{PP}}}
            child {
                node {\textcolor{blue}{\textbf{P}}}
                child[black]{node at($(\wordref{1}{4})$) {}}
            }
            child[black] {
                node {NP}
                child {
                    node {D} child{node at($(\wordref{1}{5})$) {}}
                }
                child {
                    node {N} child{node at($(\wordref{1}{6})$) {}}
                }
            }
        }
    }
    ;


    \depedge[edge below]{3}{2}{\bf\textcolor{pf7}{nsubj}}
    \depedge[edge below]{4}{6}{\bf\textcolor{pf7}{pobj}}
    \depedge[edge below]{3}{4}{\bf\textcolor{pf7}{prep}}
    \depedge[edge below]{2}{1}{\bf\textcolor{pf7}{det}}
    \depedge[edge below]{6}{5}{\bf\textcolor{pf7}{det}}
\end{dependency}

合并树

如果有人能帮助我,那就太好了,

祝你今天过得愉快。

答案1

这应该可以帮助你入门。

\documentclass{article}
\usepackage{color,graphicx}
\usepackage{tikz}
\usepackage{tikz-dependency}
\usetikzlibrary{%
  shapes,%
  arrows,%
  positioning,%
  calc,%
  automata%
}
\definecolor{pf7}{RGB}{166, 118, 29}

\begin{document}
\begin{tikzpicture}[
    level/.style={sibling distance=2cm,
    level distance = 1cm}
]
    \tikzset{level 1/.style={sibling distance=4cm}}
    \node {S}
    child{
        node (np) {\textcolor{red}{\textbf{NP}}}
        child {
            node (d1) {D}
        }
        child[red] {
            node (n1) {\textcolor{red}{\textbf{N}}}
        }
    }
child{
    node (vp) {\textcolor{blue}{\textbf{VP}}}
    child[blue] {
        node (v) {\textcolor{blue}{\textbf{V}}}
    }
    child[blue] {
        node {\textcolor{blue}{\textbf{PP}}}
        child {
            node (p) {\textcolor{blue}{\textbf{P}}}
        }
        child[black] {
            node {NP}
            child {
                node (d2) {D}
            }
            child {
                node (n2) {N}
            }
        }
    }
}
;
\begin{scope}[shift={(1cm,-2.2in)}]
\begin{deptext}[column sep=3em]
            \textbf{The} \& \textbf{cat} \& \textbf{sat} \& \textbf{on} \& \textbf{the} \& \textbf{mat} \\
            \textbf{1}    \& \textbf{2}     \& \textbf{3} \& \textbf{4}    \& \textbf{5}     \& \textbf{6} \\
        \end{deptext}
        \depedge[edge below]{3}{2}{\bf\textcolor{pf7}{nsubj}}
        \depedge[edge below]{4}{6}{\bf\textcolor{pf7}{pobj}}
        \depedge[edge below]{3}{4}{\bf\textcolor{pf7}{prep}}
        \depedge[edge below]{2}{1}{\bf\textcolor{pf7}{det}}
        \depedge[edge below]{6}{5}{\bf\textcolor{pf7}{det}}
\end{scope}
 \draw [->, very thick, red] (d1) -- (\wordref{1}{1});
 \draw [->, very thick, red] (n1) -- (\wordref{1}{2});
 \draw [->, very thick, red] (v) -- (\wordref{1}{3});
 \draw [->, very thick, red] (p) -- (\wordref{1}{4});
 \draw [->, very thick, red] (d2) -- (\wordref{1}{5});
 \draw [->, very thick, red] (n2) -- (\wordref{1}{6});
\end{tikzpicture}


\end{document}

在此处输入图片描述

我删除了最后一个children,并为 s 添加了一个标签node,然后使用\wordrefif 依赖关系绘制了边缘。可以手动对齐它们。

您的代码可以像这样修改

\documentclass{article}
\usepackage{color,graphicx}
\usepackage{tikz}
\usepackage{tikz-dependency}
\usetikzlibrary{%
  shapes,%
  arrows,%
  positioning,%
  calc,%
  automata%
}
\definecolor{pf7}{RGB}{166, 118, 29}

\begin{document}
\begin{tikzpicture}[
    level/.style={sibling distance=2cm,
    level distance = 1cm},
    level 1/.style={sibling distance=4cm}
]
    \begin{scope}[shift={(1cm,0in)}]
    \begin{deptext}[column sep=3em]
        \textbf{The} \& \textbf{cat} \& \textbf{sat} \& \textbf{on} \& \textbf{the} \& \textbf{mat} \\
        \textbf{1}    \& \textbf{2}     \& \textbf{3} \& \textbf{4}    \& \textbf{5}     \& \textbf{6} \\
    \end{deptext}
    \end{scope}

    \node[yshift=5cm] {S}
    child{
        node (np) {\textcolor{red}{\textbf{NP}}}
        child {
            node {D} child{node at ($(\wordref{1}{1})$) {}}
        }
        child[red] {
            node {\textcolor{red}{\textbf{N}}}
            child[black]{node at($(\wordref{1}{2})$) {}}
        }
    }
    child{
        node (vp) {\textcolor{blue}{\textbf{VP}}}
        child[blue] {
            node {\textcolor{blue}{\textbf{V}}}
            child[black]{node at($(\wordref{1}{3})$) {}}
        }
        child[blue] {
            node {\textcolor{blue}{\textbf{PP}}}
            child {
                node {\textcolor{blue}{\textbf{P}}}
                child[black]{node at($(\wordref{1}{4})$) {}}
            }
            child[black] {
                node {NP}
                child {
                    node {D} child{node at($(\wordref{1}{5})$) {}}
                }
                child {
                    node {N} child{node at($(\wordref{1}{6})$) {}}
                }
            }
        }
    }
    ;


    \depedge[edge below]{3}{2}{\bf\textcolor{pf7}{nsubj}}
    \depedge[edge below]{4}{6}{\bf\textcolor{pf7}{pobj}}
    \depedge[edge below]{3}{4}{\bf\textcolor{pf7}{prep}}
    \depedge[edge below]{2}{1}{\bf\textcolor{pf7}{det}}
    \depedge[edge below]{6}{5}{\bf\textcolor{pf7}{det}}
\end{tikzpicture}
\end{document}

在此处输入图片描述

答案2

这是一种forest我不应该花时间去玩的方法(即使是星期六!);)。

我喜欢这种方法的一点是它可以自动放置单词并使其与父节点对齐。但是,绘制下面的依赖关系相当笨重,而且可能很脆弱。

基本上,当你到达语法树的最后一个节点时,你会说出word={}{}第一个参数是单词,第二个参数是唯一标签。(如果同一个句子中有相同的单词,这是必要的。)

在树完成后,但在结束之前forest,您可以使用pic depedge={}{}{}{}绘制依赖关系。这需要 4 个参数:起始节点、终止节点、是否要从单词向下一层或两层,以及中间标签的内容应该是什么。

\documentclass[border=5pt,tikz]{standalone}
\usepackage{forest}
\usetikzlibrary{calc}
\begin{document}
  \newcounter{word}
  \forestset{
    colour group/.style={#1, edge=#1, for parent={#1}},
    word/.style n args=2{
      append={
          [{#1\\\stepcounter{word}\theword},
            name=#2,
            tier=word,
            font=\bfseries,
            align=center,
            inner ysep=1pt,
            for descendants={l=-2pt},
            [, name=#2-dep1, tier=dep1, dep node, edge path={\relax}
              [, name=#2-dep2, tier=dep2, dep node, edge path={\relax}
              ]
            ]
          ]
        },
    },
  }
  \tikzset{
    dep node/.style={shape=coordinate},
    dep type/.style={midway, fill=white, font=\scriptsize, inner sep=.75pt, text=orange!75!black, outer sep=0pt, draw, minimum height=8pt},
    pics/depedge/.style n args={4}{
      code={
        \ifnum#3=1
          \draw [->, rounded corners=2pt, pic actions] ({$(#1)!.075!(#2)$} |- #1.south) -- ($(#1-dep#3)!.125!(#2-dep#3)$) -- node [dep type] {#4} ($(#2-dep#3)!.125!(#1-dep#3)$) -- ({$(#2)!.075!(#1)$} |- #1.south);
        \else\ifnum#3=2
          \draw [->, rounded corners=2pt, pic actions] (#1.south) -- ($(#1-dep#3)!.075!(#2-dep#3)$) -- node [dep type] {#4} ($(#2-dep#3)!.075!(#1-dep#3)$) -- (#2.south);
        \fi\fi
      },
    },
  }

  \begin{forest}
    for tree={
      s sep+=10pt,
    }
    [S
      [NP
        [D, word={The}{the1}
        ]
        [N, colour group=red, word={cat}{cat}
        ]
      ]
      [VP
        [V, colour group=blue, word={sat}{sat}
        ]
        [PP, colour group=blue
          [P, colour group=blue, word={on}{on}
          ]
          [NP
            [D, word={the}{the2}
            ]
            [N, word={mat}{mat}
            ]
          ]
        ]
      ]
    ]
    \pic {depedge={cat}{the1}{1}{det}};
    \pic {depedge={mat}{the2}{1}{det}};
    \pic {depedge={sat}{cat}{1}{nsubj}};
    \pic {depedge={sat}{on}{1}{prep}};
    \pic {depedge={on}{mat}{2}{pobj}};
  \end{forest}
\end{document}

森林依赖关系

相关内容