擅长:python、mysql、java
<p><strong>有关NTLK 3.2.3或更新版本,请参阅donners45的答案。</strong></p>
<p><strong>对于旧版本的NLTK:</strong></p>
<p>NLTK中没有内置方法,但可以使用以下方法:</p>
<pre><code>from nltk.corpus import wordnet
syns = list(wordnet.all_synsets())
offsets_list = [(s.offset(), s) for s in syns]
offsets_dict = dict(offsets_list)
offsets_dict[14204095]
>>> Synset('heatstroke.n.01')
</code></pre>
<p>然后你就可以在需要的时候把字典装进去。</p>
<p>对于3.0之前的NLTK版本,请替换行</p>
<pre><code>offsets_list = [(s.offset(), s) for s in syns]
</code></pre>
<p>与</p>
<pre><code>offsets_list = [(s.offset, s) for s in syns]
</code></pre>
<p>因为在NLTK 3.0之前,<code>offset</code>是一个属性而不是一个方法。</p>