NLTK WordNet错误,单词使用synsets查找

时间:2017-06-14 13:49:03

标签: python nltk wordnet

我正在使用带有NLTK 3.2.3的Python 3.6,并且我得到了一个" WordNetError"仅用于"护送"。我不会因为任何其他词语而出错。这里的成绩单显示了成功的单词" dog"和使用“" escort":

这个词的错误
Python 3.6.0 |Anaconda custom (64-bit)| (default, Dec 23 2016, 12:22:00) 
[GCC 4.4.7 20120313 (Red Hat 4.4.7-1)] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> from nltk.corpus import wordnet
>>> wordnet.synsets('dog')
[Synset('dog.n.01'), Synset('frump.n.01'), Synset('dog.n.03'), Synset('cad.n.01'), Synset('frank.n.02'), Synset('pawl.n.01'), Synset('andiron.n.01'), Synset('chase.v.01')]
>>> wordnet.synsets('escort')
Traceback (most recent call last):
  File "/home/user1/.conda/envs/ca/lib/python3.6/site-packages/nltk/corpus/reader/wordnet.py", line 1403, in _synset_from_pos_and_line
    offset = int(_next_token())
ValueError: invalid literal for int() with base 10: '02026433\x00v'

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/home/user1/.conda/envs/ca/lib/python3.6/site-packages/nltk/corpus/reader/wordnet.py", line 1491, in synsets
    for p in pos
  File "/home/user1/.conda/envs/ca/lib/python3.6/site-packages/nltk/corpus/reader/wordnet.py", line 1493, in <listcomp>
    for offset in index[form].get(p, [])]
  File "/home/user1/.conda/envs/ca/lib/python3.6/site-packages/nltk/corpus/reader/wordnet.py", line 1335, in synset_from_pos_and_offset
    synset = self._synset_from_pos_and_line(pos, data_file_line)
  File "/home/user1/.conda/envs/ca/lib/python3.6/site-packages/nltk/corpus/reader/wordnet.py", line 1448, in _synset_from_pos_and_line
    raise WordNetError('line %r: %s' % (data_file_line, e))
nltk.corpus.reader.wordnet.WordNetError: line '02025829 38 v 01 escort 0 006 @ 02025550 v 0000 + 09992538 n 0102 ~ 02026203 v 0000 ~ 02026327 v 0000 ~ 02026433\x00v 0000 ~ 02026712 v 0000 04 + 08 00 + 09 00 + 20 00 + 21 00 | accompany as an escort; "She asked her older brother to escort her to the ball"  \n': invalid literal for int() with base 10: '02026433\x00v'

但是,当我在http://wordnetweb.princeton.edu/perl/webwn使用在线WordNet搜索工具时,它会按预期执行查找。使用nltk.download()下载了最新的WordNet语料库。

当期望找到整数值时,该错误似乎引用了WordB定义中的十六进制值。

有什么想法吗?如果您遇到类似的情况,请告知。

1 个答案:

答案 0 :(得分:0)

仔细检查“动词”字典文件后,我发现它实际上已损坏。这就是像“vi”这样的编辑器中的样子:

02025829 38 v 01 escort 0 006 @ 02025550 v 0000 + 09992538 n 0102 ~ 02026203 v 0000 ~ 02026327 v 0000 ~ 02026433^@v 0000 ~ 02026712 v 0000 04 +

我用空格替换了“^ @”,问题就消失了。我想更重要的问题是它首先是如何被破坏的。

问题解决了!