python - nltk.grammar.is_terminal('str') always returns true? -
how define grammar used nltk.grammar.is_terminal()
? no matter on object cast method, true
return. instead wanna have checked whether list called wordlist
contains productions defined in context-free-grammar safed under grammar.cfg
.
check out code on https://github.com/nltk/nltk/blob/develop/nltk/grammar.py
def is_nonterminal(item): """ :return: true if item ``nonterminal``. :rtype: bool """ homecoming isinstance(item, nonterminal) def is_terminal(item): """ homecoming true if item terminal, if hashable , not ``nonterminal``. :rtype: bool """ homecoming hasattr(item, '__hash__') , not isinstance(item, nonterminal)
although i'm not sure how functions should used, default value is_terminal()
string input true
.
because, firstly, strings contains __hash__
attribute, it's function hash string, see https://docs.python.org/2/reference/datamodel.html#object.hash
>>> astring = 'foo bar' >>> astring.__hash__ <method-wrapper '__hash__' of str object @ 0x7f06bb0cbcc0> >>> astring.__hash__() 8194924035431162904
secondly, string certainly not nonterminal
object in nltk
because class nonterminal
is:
class nonterminal(object): """ non-terminal symbol context free grammar. ``nonterminal`` wrapper class node values; used ``production`` objects distinguish node values leaf values. node value wrapped ``nonterminal`` known "symbol". symbols typically strings representing phrasal categories (such ``"np"`` or ``"vp"``). however, more complex symbol types used (e.g., lexicalized grammars). since symbols node values, must immutable , hashable. 2 ``nonterminals`` considered equal if symbols equal. :see: ``cfg``, ``production`` :type _symbol: :ivar _symbol: node value corresponding ``nonterminal``. value must immutable , hashable. """
so string goes through both criteria of (1) having __hash__
attribute , (2) not nonterminal
object. nltk.grammar.is_terminal()
returns true strings.
then how create homecoming false, when load grammar , read nonterminal object in grammar, perchance when object created or casted nonterminal, e.g. http://www.nltk.org/_modules/nltk/parse/pchart.html
python list terminal grammar nltk
No comments:
Post a Comment