本文介绍了加载和编辑cfg文件以进行语法分析的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在执行此处提到的步骤- http://www.nltk.org/book /ch10.html 使用cfg文件加载和解析数据.当我使用下面的代码时,我不会遇到任何问题.

I am following the steps mentioned here - http://www.nltk.org/book/ch10.htmlto load and parse data using a cfg file. When I use the code below I don't face any issue.

cp = load_parser('grammars/book_grammars/sql0.fcfg')
query = 'What cities are located in China'
trees = list(cp.parse(query.split()))
answer = trees[0].label()['SEM']
answer = [s for s in answer if s]
q = ' '.join(answer)
print(q)

我想做的是取出sql0.fcfg,对其进行更改,然后再次将其加载到解析器中,以使用我自己的语句对其进行测试.我就是在这里遇到问题的.

What I wish to do is take out the sql0.fcfg, make changes to it and load it into the parser again to test it with my own sentences. It is here that I run into issues.

我将sql0.fcg文件的内容复制到一个txt文件中,该文件存储在本地系统中,并将其重命名为.cfg,但是当我像下面那样对其进行解析时,出现错误,提示nltk.download('C:' ).

I copied the contents of the sql0.fcg file into a txt file, stored in my local system, renamed it as .cfg but when I am parsing it like below I get an error saying nltk.download('C:').

cp = load_parser('C:/Users/212757677/Desktop/mygrammar.fcfg')

我尝试的第二种方法是从fcfg文件复制语法,并尝试以以下方式加载它.在这里,我收到一条错误消息:无法解析第2行.期望的箭头"

The second method I tried was to copy the grammar from the fcfg file and try to load it in the following manner. Here I get an error saying 'Unable to parse line 2. Expected arrow'

import nltk
groucho_grammar = nltk.CFG.fromstring("""
S[SEM=(?np + WHERE + ?vp)] -> NP[SEM=?np] VP[SEM=?vp]
VP[SEM=(?v + ?pp)] -> IV[SEM=?v] PP[SEM=?pp]
VP[SEM=(?v + ?ap)] -> IV[SEM=?v] AP[SEM=?ap]
NP[SEM=(?det + ?n)] -> Det[SEM=?det] N[SEM=?n]
PP[SEM=(?p + ?np)] -> P[SEM=?p] NP[SEM=?np]
AP[SEM=?pp] -> A[SEM=?a] PP[SEM=?pp]
NP[SEM='Country="greece"'] -> 'Greece'
NP[SEM='Country="china"'] -> 'China'
Det[SEM='SELECT'] -> 'Which' | 'What'
N[SEM='City FROM city_table'] -> 'cities'
IV[SEM=''] -> 'are'
A[SEM=''] -> 'located'
P[SEM=''] -> 'in'
""")
cp = load_parser(groucho_grammar)
query = 'What cities are located in China'
trees = list(cp.parse(query.split()))
answer = trees[0].label()['SEM']
answer = [s for s in answer if s]
q = ' '.join(answer)
print(q)

ValueError: Unable to parse line 2: S[SEM=(?np + WHERE + ?vp)] -> NP[SEM=?np] VP[SEM=?vp]
Expected an arrow

我只想在sql0.fcfg中编辑现有语法并解析它.有人可以建议如何解决这个问题吗?

I just want to edit the existing grammar in sql0.fcfg and parse it. Can someone suggest how to go about this ?

推荐答案

nltk.load_parser的原型是

请注意,第一个参数是一个"URL",而不仅仅是文件路径(请参见数据模块文档以获取非常简短的说明). nltk URL以协议开头,后跟冒号,因此它将C:解释为协议.您可能应该明确:file:C:/Users/212757677/Desktop/mygrammar.fcfg. (或者也许是file:///C:/Users/212757677/Desktop/mygrammar.fcfg -我没有Windows计算机可以对其进行测试.)

Note that the first argument is a "url", not just a file path (See the data Module documentation for a very brief explanation). An nltk URL starts with a protocol followed by a colon, so it will interpret C: as a protocol. You should probably be explicit: file:C:/Users/212757677/Desktop/mygrammar.fcfg. (Or perhaps it's file:///C:/Users/212757677/Desktop/mygrammar.fcfg -- I don't have a Windows machine to test it on.)

nltk.load_parser根据文件扩展名猜测语法格式.在这种情况下,您正在加载要素语法(.fcfg),而不是简单的CFG.如果要手动创建解析器,则应遵循 NLTK操作方法功能中的示例语法解析.

nltk.load_parser guesses the grammar format based on the filename extension. In this case, you're loading a feature grammar (.fcfg), not a simple CFG. If you want to create the parser manually, you should follow the example in the NLTK how-to on feature grammar parsing.

这篇关于加载和编辑cfg文件以进行语法分析的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-17 09:24