Python nltk.grammar.u终端(';str';)是否总是返回true?
如何定义Python nltk.grammar.u终端(';str';)是否总是返回true?,python,list,terminal,grammar,nltk,Python,List,Terminal,Grammar,Nltk,如何定义nltk.grammar.is_terminal()使用的语法?不管我在什么对象上使用这个方法,我总是得到一个true作为返回。但是我想检查名为wordlist的列表是否包含在grammar.cfg下安全的上下文无关语法中定义的产品,请查看 虽然我不确定这些函数应该如何使用,但是对于任何字符串输入,的默认值总是True 因为,首先,所有字符串都包含\uuuuuuuuuuuuuuuuuuuuuuuuuuu散列属性,所以它是散列字符串的函数,请参阅 因此,一个字符串需要满足以下两个条件:(1
nltk.grammar.is_terminal()
使用的语法?不管我在什么对象上使用这个方法,我总是得到一个true
作为返回。但是我想检查名为wordlist
的列表是否包含在grammar.cfg
下安全的上下文无关语法中定义的产品,请查看
虽然我不确定这些函数应该如何使用,但是对于任何字符串输入,的默认值总是True
因为,首先,所有字符串都包含\uuuuuuuuuuuuuuuuuuuuuuuuuuu散列
属性,所以它是散列字符串的函数,请参阅
因此,一个字符串需要满足以下两个条件:(1)具有\uuuuuuuuuuuuuuuuuuuuuuuuuuuu
属性,以及(2)不是非终端对象。因此,nltk.grammar.is_terminal()
总是为所有字符串返回True
那么,只有当加载语法并读取语法中的非终结符对象时,可能只有当对象被专门创建或强制转换为非终结符时,我如何使其返回False,例如
def is_nonterminal(item):
"""
:return: True if the item is a ``Nonterminal``.
:rtype: bool
"""
return isinstance(item, Nonterminal)
def is_terminal(item):
"""
Return True if the item is a terminal, which currently is
if it is hashable and not a ``Nonterminal``.
:rtype: bool
"""
return hasattr(item, '__hash__') and not isinstance(item, Nonterminal)
>>> astring = 'foo bar'
>>> astring.__hash__
<method-wrapper '__hash__' of str object at 0x7f06bb0cbcc0>
>>> astring.__hash__()
8194924035431162904
class Nonterminal(object):
"""
A non-terminal symbol for a context free grammar. ``Nonterminal``
is a wrapper class for node values; it is used by ``Production``
objects to distinguish node values from leaf values.
The node value that is wrapped by a ``Nonterminal`` is known as its
"symbol". Symbols are typically strings representing phrasal
categories (such as ``"NP"`` or ``"VP"``). However, more complex
symbol types are sometimes used (e.g., for lexicalized grammars).
Since symbols are node values, they must be immutable and
hashable. Two ``Nonterminals`` are considered equal if their
symbols are equal.
:see: ``CFG``, ``Production``
:type _symbol: any
:ivar _symbol: The node value corresponding to this
``Nonterminal``. This value must be immutable and hashable.
"""