- android - 多次调用 OnPrimaryClipChangedListener
- android - 无法更新 RecyclerView 中的 TextView 字段
- android.database.CursorIndexOutOfBoundsException : Index 0 requested, 光标大小为 0
- android - 使用 AppCompat 时,我们是否需要明确指定其 UI 组件(Spinner、EditText)颜色
如何定义 nltk.grammar.is_terminal()
使用的语法?无论我在什么对象上使用此方法,我总是得到 A true
作为返回值。但相反,我想检查一个名为 wordlist
的列表是否包含在 grammar.cfg
下安全的上下文无关语法中定义的作品。
最佳答案
查看 https://github.com/nltk/nltk/blob/develop/nltk/grammar.py 上的代码
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)
虽然我不确定应该如何使用这些函数,但对于任何字符串输入,is_terminal()
的默认值始终是 True
。
因为,首先,所有字符串都包含__hash__
属性,它是一个散列字符串的函数,参见https://docs.python.org/2/reference/datamodel.html#object.hash
>>> astring = 'foo bar'
>>> astring.__hash__
<method-wrapper '__hash__' of str object at 0x7f06bb0cbcc0>
>>> astring.__hash__()
8194924035431162904
其次,所有字符串肯定不是 NLTK
中的 Nonterminal
对象,因为类 Nonterminal
是:
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.
"""
因此,字符串符合以下两个条件:(1) 具有 __hash__
属性,并且 (2) 不是 Nonterminal
对象。因此,nltk.grammar.is_terminal()
总是为所有字符串返回 True。
然后如何让它返回 False,仅当您加载语法然后读取语法中的非终端对象时,可能仅当对象被专门创建或转换为非终端时,例如http://www.nltk.org/_modules/nltk/parse/pchart.html
关于python - nltk.grammar.is_terminal ('str' ) 总是返回 true?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26917726/
如何定义 nltk.grammar.is_terminal() 使用的语法?无论我在什么对象上使用此方法,我总是得到 A true 作为返回值。但相反,我想检查一个名为 wordlist 的列表是否包
我是一名优秀的程序员,十分优秀!