![](/img/trans.png)
[英]Using NLTK to parse with context free grammar but issues with Adjectives
[英]How to parse the special character in Context Free Grammar?
我有一個涉及標點符號的上下文無關語法(CFG)。 例如 nltk.parse_cfg("""PP-CLR -> IN `` NP-TTL""")
`` 是有效的 Penn Treebank POS 標簽。 但是nltk無法識別。 事實上,nltk.parse_cfg 無法識別字母數字和破折號以外的任何字符。 而 Penn Treebank POS 標簽有幾個標點符號,例如 $ #: 。 (
那么,我應該在我的數據集中保留標點符號嗎? 或者有沒有辦法解析這些字符?
謝謝
您可能需要特別指定它們作為終端注釋,例如:
>>> 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 .)))
對於使用當前一代 NLTK 的人,您可以通過手動更新語法 object 的產生式集來添加包含特殊字符的非終端。下面,我添加了包含特殊字符$
的標記/非終端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$")
會引發錯誤。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.