【问题标题】:Python NLTK parsing error? 'str' object has no attribute 'check_coverage'Python NLTK 解析错误? “str”对象没有属性“check_coverage”
【发布时间】:2017-08-18 19:28:04
【问题描述】:

我正在尝试使用 NLTK 来确定一个句子是否有效。 我加载了语法,但是每当我尝试获取解析器时,它都不起作用,并且出现错误“AttributeError:'str'对象没有属性'check_coverage'” 这是我的代码:

sentence = ['show', 'me', 'northwest', 'flights', 'to', 'detroit', '.']
grammar = nltk.data.load('grammars/large_grammars/atis.cfg', 'text')
parser =  nltk.parse.BottomUpChartParser(grammar)
chart = parser.chart_parse(sentence)

这是完整的回溯: Traceback(最近一次调用最后一次):

 File "<ipython-input-448-852d3bb24984>", line 1, in <module>
 chart = parser.chart_parse(sentence)

 File "C:\Users\Class2016\Anaconda3\lib\site-packages\nltk\parse\chart.py", 
 line 1310, in chart_parse
  self._grammar.check_coverage(tokens)

AttributeError: 'str' object has no attribute 'check_coverage'

我从下面列出的示例中获得这部分代码,在 LARGE 上下文无关语法的单元测试下:http://www.nltk.org/howto/parse.html

任何关于为什么会发生这种情况或如何纠正这种情况的信息将不胜感激。

谢谢!

【问题讨论】:

  • 完整的追溯是什么?该异常并非直接来自这 3 行...
  • @JonClements 我刚刚添加了它
  • @VinnyChase 我的回答能解决问题吗?
  • @cᴏʟᴅsᴘᴇᴇᴅ 是的,谢谢!

标签: python python-3.x parsing nltk


【解决方案1】:

通过this 链接,您可能希望首先使用nltk.parse_cfg 解析这些规则:

rules = nltk.data.load('grammars/large_grammars/atis.cfg', 'text')
grammar = nltk.parse_cfg(rules)
parser =  nltk.parse.BottomUpChartParser(parsed_grammar)

【讨论】:

  • 在我的 nltk/python 版本中,我实际上不得不使用 CFG.fromstring() 代替,这在这个版本中应该是等效的,但效果很好
猜你喜欢
  • 2016-01-29
  • 1970-01-01
  • 2016-04-07
  • 2023-03-05
  • 2015-04-19
  • 2020-09-28
  • 1970-01-01
  • 1970-01-01
  • 2022-01-16
相关资源
最近更新 更多