3

我有一个涉及标点符号的上下文无关语法(CFG)。例如 nltk.parse_cfg("""PP-CLR -> IN `` NP-TTL""")

`` 是有效的 Penn Treebank POS 标签。但是 nltk 无法识别它。事实上,nltk.parse_cfg 无法识别除字母数字和破折号以外的任何字符。Penn Treebank POS 标签有几个标点符号,例如 $ # : 。(

那么,我应该在我的数据集中保留标点符号吗?或者无论如何要解析这些字符?

谢谢

4

2 回答 2

3

You might need to specially specify them as terminal notes, for e.g. :

>>> import nltk
>>> grammar = nltk.parse_cfg("""
... S -> NP VP
... VP -> V PUNCT
... PUNCT -> '.'
... V -> 'eat'
... NP -> 'I'
... """)
>>> 
>>> sentence = "I eat .".split()
>>> cp = nltk.ChartParser(grammar)
>>> for tree in cp.nbest_parse(sentence):
...     print tree
... 
(S (NP I) (VP (V eat) (PUNCT .)))
于 2013-09-25T09:09:44.423 回答
0

对于使用当前一代 NLTK 的人,您可以通过手动更新语法对象的产生式集来添加包含特殊字符的非终端。PRP$下面,我添加了包含特殊字符的标签/非终端$

from nltk.grammar import Production
from nltk.grammar import Nonterminal
productions = my_grammar.productions()
productions.extend([Production(Nonterminal('Nom'),[Nonterminal('PRP$')])])

这相当于在我们的 CFG 中添加以下内容:

Nom -> PRP$

改为使用nltk.CFG.fromstring("Nom -> PRP$")会引发错误。

于 2021-10-10T01:25:22.317 回答