<p>雨果迈尔霍特的<a href="https://stackoverflow.com/a/32895132/395857">answer</a>很棒。我将为那些希望在两个单词之间找到最短依赖路径的用户(而HugoMailhot的答案依赖于<a href="https://github.com/biplab-iitb/practNLPTools" rel="noreferrer">practNLPTools</a>)编写类似的东西。</p>
<p>句子:</p>
<blockquote>
<p>Robots in popular culture are there to remind us of the awesomeness of
unbound human agency.</p>
</blockquote>
<p>有<a href="https://demos.explosion.ai/displacy/?text=Robots%20in%20popular%20culture%20are%20there%20to%20remind%20us%20of%20the%20awesomeness%20of%20unbound%20human%20agency.&model=en&cpu=1&cph=1" rel="noreferrer">following dependency tree</a>:</p>
<p><a href="https://i.stack.imgur.com/UNoPf.png" rel="noreferrer"><img src="https://i.stack.imgur.com/UNoPf.png" alt="enter image description here"/></a></p>
<p>下面是查找两个单词之间最短依赖路径的代码:</p>
<pre><code>import networkx as nx
import spacy
nlp = spacy.load('en')
# https://spacy.io/docs/usage/processing-text
document = nlp(u'Robots in popular culture are there to remind us of the awesomeness of unbound human agency.', parse=True)
print('document: {0}'.format(document))
# Load spacy's dependency tree into a networkx graph
edges = []
for token in document:
# FYI https://spacy.io/docs/api/token
for child in token.children:
edges.append(('{0}-{1}'.format(token.lower_,token.i),
'{0}-{1}'.format(child.lower_,child.i)))
graph = nx.Graph(edges)
# https://networkx.github.io/documentation/networkx-1.10/reference/algorithms.shortest_paths.html
print(nx.shortest_path_length(graph, source='robots-0', target='awesomeness-11'))
print(nx.shortest_path(graph, source='robots-0', target='awesomeness-11'))
print(nx.shortest_path(graph, source='robots-0', target='agency-15'))
</code></pre>
<p>输出:</p>
<pre><code>4
['robots-0', 'are-4', 'remind-7', 'of-9', 'awesomeness-11']
['robots-0', 'are-4', 'remind-7', 'of-9', 'awesomeness-11', 'of-12', 'agency-15']
</code></pre>
<p>要安装spacy和networkx:</p>
<pre><code>sudo pip install networkx
sudo pip install spacy
sudo python -m spacy.en.download parser # will take 0.5 GB
</code></pre>
<p>关于spacy依赖解析的一些基准:<a href="https://spacy.io/docs/api/" rel="noreferrer">https://spacy.io/docs/api/</a></p>
<p><a href="https://i.stack.imgur.com/IxZH2.png" rel="noreferrer"><img src="https://i.stack.imgur.com/IxZH2.png" alt="enter image description here"/></a></p>