<p>你的问题很容易被理解为一个图问题,在图中我们必须找到两个节点之间的最短路径。</p>
<p>要在图中转换依赖项解析,我们首先必须处理它作为字符串出现的事实。你想得到这个:</p>
<pre><code>'nsubj(are-5, Robots-1)\nxsubj(remind-8, Robots-1)\namod(culture-4, popular-3)\nprep_in(Robots-1, culture-4)\nroot(ROOT-0, are-5)\nadvmod(are-5, there-6)\naux(remind-8, to-7)\nxcomp(are-5, remind-8)\ndobj(remind-8, us-9)\ndet(awesomeness-12, the-11)\nprep_of(remind-8, awesomeness-12)\namod(agency-16, unbound-14)\namod(agency-16, human-15)\nprep_of(awesomeness-12, agency-16)'
</code></pre>
<p>像这样:</p>
<pre><code>[('are-5', 'Robots-1'), ('remind-8', 'Robots-1'), ('culture-4', 'popular-3'), ('Robots-1', 'culture-4'), ('ROOT-0', 'are-5'), ('are-5', 'there-6'), ('remind-8', 'to-7'), ('are-5', 'remind-8'), ('remind-8', 'us-9'), ('awesomeness-12', 'the-11'), ('remind-8', 'awesomeness-12'), ('agency-16', 'unbound-14'), ('agency-16', 'human-15'), ('awesomeness-12', 'agency-16')]
</code></pre>
<p>通过这种方式,您可以将元组列表从<a href="http://networkx.github.io/documentation/latest/overview.html">networkx</a>模块馈送到一个图形构造函数,该模块将分析该列表并为您构建一个图,另外还提供一个简洁的方法,该方法为您提供两个给定节点之间最短路径的长度。</p>
<p><strong>必要的进口</strong></p>
<pre><code>import re
import networkx as nx
from practnlptools.tools import Annotator
</code></pre>
<p><strong>如何以所需的元组列表格式获取字符串</strong></p>
<pre><code>annotator = Annotator()
text = """Robots in popular culture are there to remind us of the awesomeness of unbound human agency."""
dep_parse = annotator.getAnnotations(text, dep_parse=True)['dep_parse']
dp_list = dep_parse.split('\n')
pattern = re.compile(r'.+?\((.+?), (.+?)\)')
edges = []
for dep in dp_list:
m = pattern.search(dep)
edges.append((m.group(1), m.group(2)))
</code></pre>
<p><strong>如何构建图表</p>
<pre><code>graph = nx.Graph(edges) # Well that was easy
</code></pre>
<p><strong>如何计算最短路径长度</strong></p>
<pre><code>print(nx.shortest_path_length(graph, source='Robots-1', target='awesomeness-12'))
</code></pre>
<p>此脚本将揭示给定依赖项解析的最短路径实际上是长度2,因为您可以通过<code>remind-8</code>从<code>Robots-1</code>到<code>awesomeness-12</code></p>
<pre><code>1. xsubj(remind-8, Robots-1)
2. prep_of(remind-8, awesomeness-12)
</code></pre>
<p>如果您不喜欢这个结果,您可能需要考虑过滤一些依赖项,在这种情况下不允许将<code>xsubj</code>依赖项添加到图中。</p>