Python-WordNet NLTK KeyError [英] Python - WordNet NLTK KeyError

查看:147
本文介绍了Python-WordNet NLTK KeyError的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我真的不明白.我正在使用WordNet,但遇到麻烦了.我用这样的字符串调用以下方法:

I really don`t get it. I´m using WordNet and I get in trouble. I call the following method with strings like that:

F2F-已启动过程,创建计划数据,创建收货,已创建收货

F2F - Process started, Create planning data, Create a goods receipt, Goods receipt created

def lemmatise(word, pos=NOUN):
        return WordNetLemmatizer().lemmatize(word, pos)

这个调用下一​​个方法:

This one calls the next method:

def lemmatize(self, word, pos=NOUN):
    lemmas = wordnet._morphy(word, pos)
    return min(lemmas, key=len) if lemmas else word

并且方法morphy()是错误指出问题出处的地方(请参阅错误消息).

And the method morphy() is where the error says that the problem comes from (see error message).

def _morphy(self, form, pos):
    # from jordanbg:
    # Given an original string x
    # 1. Apply rules once to the input to get y1, y2, y3, etc.
    # 2. Return all that are in the database
    # 3. If there are no matches, keep applying rules until you either
    #    find a match or you can't go any further
L1687   exceptions = self._exception_map[pos]
        substitutions = self.MORPHOLOGICAL_SUBSTITUTIONS[pos]

错误消息

[18/Nov/2016 15:02:57] "GET / HTTP/1.1" 200 4364
[18/Nov/2016 15:02:57] "GET /static/website/js/animation.js HTTP/1.1" 200 180
[18/Nov/2016 15:02:57] "GET /static/website/css/animation.css HTTP/1.1" 200 1069
[18/Nov/2016 15:02:57] "GET /static/website/img/mamegra_pro_thumbnail.png HTTP/1.1" 200 3588
[18/Nov/2016 15:02:57] "GET /static/website/css/bootstrap.min.css HTTP/1.1" 200 121260
[18/Nov/2016 15:02:59] "GET /syntactic_matching_final/ HTTP/1.1" 200 5571
[18/Nov/2016 15:03:02] "GET /semantic_matching_final/ HTTP/1.1" 200 5571
Internal Server Error: /semantic_matching_final/
Traceback (most recent call last):
  File "C:\Users\Bebop\AppData\Local\Programs\Python\Python35\lib\site-packages\django\core\handlers\exception.py", line 39, in inner
    response = get_response(request)
  File "C:\Users\Bebop\AppData\Local\Programs\Python\Python35\lib\site-packages\django\core\handlers\base.py", line 249, in _legacy_get_response
    response = self._get_response(request)
  File "C:\Users\Bebop\AppData\Local\Programs\Python\Python35\lib\site-packages\django\core\handlers\base.py", line 187, in _get_response
    response = self.process_exception_by_middleware(e, request)
  File "C:\Users\Bebop\AppData\Local\Programs\Python\Python35\lib\site-packages\django\core\handlers\base.py", line 185, in _get_response
    response = wrapped_callback(request, *callback_args, **callback_kwargs)
  File "C:\Users\Bebop\OneDrive\Masterarbeit\MamegraPro\MamegraProApp\views.py", line 418, in semantic_matching_final
    matches = matching.match_optimization('semantic_final', enable_infocontent) #perform syntactic matching and save matches in variable
  File "C:\Users\Bebop\OneDrive\Masterarbeit\MamegraPro\MamegraProApp\classes\class_matching.py", line 55, in match_optimization
    scores = {(n1, n2): self.semantic_score_final(n1.name, n2.name, enable_infocontent) if self.semantic_score_final(n1.name, n2.name, enable_infocontent) >= self.minimum_ratio else 0 for n1 in nodes1 for n2 in nodes2}
  File "C:\Users\Bebop\OneDrive\Masterarbeit\MamegraPro\MamegraProApp\classes\class_matching.py", line 55, in <dictcomp>
    scores = {(n1, n2): self.semantic_score_final(n1.name, n2.name, enable_infocontent) if self.semantic_score_final(n1.name, n2.name, enable_infocontent) >= self.minimum_ratio else 0 for n1 in nodes1 for n2 in nodes2}
  File "C:\Users\Bebop\OneDrive\Masterarbeit\MamegraPro\MamegraProApp\classes\class_matching.py", line 557, in semantic_score_final
    lemmas = self.corpusBase.lemmatise(w1)
  File "C:\Users\Bebop\OneDrive\Masterarbeit\MamegraPro\MamegraProApp\classes\class_wordnetWrapper.py", line 8, in lemmatise
    return WordNetLemmatizer().lemmatize(word, pos)
  File "C:\Users\Bebop\AppData\Local\Programs\Python\Python35\lib\site-packages\nltk\stem\wordnet.py", line 40, in lemmatize
    lemmas = wordnet._morphy(word, pos)
  File "C:\Users\Bebop\AppData\Local\Programs\Python\Python35\lib\site-packages\nltk\corpus\reader\wordnet.py", line 1687, in _morphy
    exceptions = self._exception_map[pos]
KeyError: 'Created'
[18/Nov/2016 15:03:11] "POST /semantic_matching_final/ HTTP/1.1" 500 122256
Not Found: /favicon.ico
[18/Nov/2016 15:03:12] "GET /favicon.ico HTTP/1.1" 404 4238

奇怪的是,即使输入相同,它也会在出现错误的地方随机切换单词.请帮帮我!

The strange thing is that it randomly switches the word where it throws the error even with the same input. Please help me!

推荐答案

检查以确保传递的是一个字母而不是更长的字符串.这些可以在源nltk.corpus.reader.wordnet的常量部分中找到. {词性常数ADJ,ADJ_SAT,ADV,NOUN,VERB ='a','s','r','n','v'#}

Check to insure that you are passing in one letter and not the longer string. These are found in the constants section of source nltk.corpus.reader.wordnet { Part-of-speech constants ADJ, ADJ_SAT, ADV, NOUN, VERB = 'a', 's', 'r', 'n', 'v' #}

这篇关于Python-WordNet NLTK KeyError的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆