我们从Python开源项目中,提取了以下40个代码示例,用于说明如何使用nltk.nonterminals()。
def nonterminals(symbols): """ Given a string containing a list of symbol names, return a list of ``Nonterminals`` constructed from those symbols. :param symbols: The symbol name string. This string can be delimited by either spaces or commas. :type symbols: str :return: A list of ``Nonterminals`` constructed from the symbol names given in ``symbols``. The ``Nonterminals`` are sorted in the same order as the symbols names. :rtype: list(Nonterminal) """ if ',' in symbols: symbol_list = symbols.split(',') else: symbol_list = symbols.split() return [Nonterminal(s.strip()) for s in symbol_list]
def leftcorners(self, cat): """ Return the set of all nonterminals that the given nonterminal can start with, including itself. This is the reflexive, transitive closure of the immediate leftcorner relation: (A > B) iff (A -> B beta) :param cat: the parent of the leftcorners :type cat: Nonterminal :return: the set of all leftcorners :rtype: set(Nonterminal) """ return self._leftcorners.get(cat, set([cat]))
def leftcorner_parents(self, cat): """ Return the set of all nonterminals for which the given category is a left corner. This is the inverse of the leftcorner relation. :param cat: the suggested leftcorner :type cat: Nonterminal :return: the set of all parents to the leftcorner :rtype: set(Nonterminal) """ return self._leftcorner_parents.get(cat, set([cat]))
def __init__(self, start, productions): """ Create a new feature-based grammar, from the given start state and set of ``Productions``. :param start: The start symbol :type start: FeatStructNonterminal :param productions: The list of productions that defines the grammar :type productions: list(Production) """ CFG.__init__(self, start, productions) # The difference with CFG is that the productions are # indexed on the TYPE feature of the nonterminals. # This is calculated by the method _get_type_if_possible().
def read_grammar(input, nonterm_parser, probabilistic=False, encoding=None): """ Return a pair consisting of a starting category and a list of ``Productions``. :param input: a grammar, either in the form of a string or else as a list of strings. :param nonterm_parser: a function for parsing nonterminals. It should take a ``(string, position)`` as argument and return a ``(nonterminal, position)`` as result. :param probabilistic: are the grammar rules probabilistic? :type probabilistic: bool :param encoding: the encoding of the grammar, if it is a binary string :type encoding: str """ if encoding is not None: input = input.decode(encoding) if isinstance(input, string_types): lines = input.split('\n') else: lines = input start = None productions = [] continue_line = '' for linenum, line in enumerate(lines): line = continue_line + line.strip() if line.startswith('#') or line=='': continue if line.endswith('\\'): continue_line = line[:-1].rstrip()+' ' continue continue_line = '' try: if line[0] == '%': directive, args = line[1:].split(None, 1) if directive == 'start': start, pos = nonterm_parser(args, 0) if pos != len(args): raise ValueError('Bad argument to start directive') else: raise ValueError('Bad directive') else: # expand out the disjunctions on the RHS productions += _read_production(line, nonterm_parser, probabilistic) except ValueError as e: raise ValueError('Unable to parse line %s: %s\n%s' % (linenum+1, line, e)) if not productions: raise ValueError('No productions found!') if not start: start = productions[0].lhs() return (start, productions)