def iterparse(self, lines: Union[Iterable[str], str]) -> Iterator[Tree]: """ Yield trees parsed from *lines*. Args: lines: a string or open file with PENMAN-serialized graphs Returns: The :class:`Tree` object described in *lines*. """ tokens = lex(lines, pattern=PENMAN_RE) while tokens and tokens.peek().type in ('COMMENT', 'LPAREN'): yield self._parse(tokens)
def parse(self, s: str) -> Tree: """ Parse PENMAN-notation string *s* into its tree structure. Args: s: a string containing a single PENMAN-serialized graph Returns: The tree structure described by *s*. Example: >>> codec = PENMANCodec() >>> codec.parse('(b / bark-01 :ARG0 (d / dog))') # noqa Tree(('b', [('/', 'bark-01'), ('ARG0', ('d', [('/', 'dog')]))])) """ tokens = lex(s, pattern=PENMAN_RE) return self._parse(tokens)
def _lex(s): return [tok.type for tok in lexer.lex(s)]
def parse_triples(self, s: str) -> List[BasicTriple]: """ Parse a triple conjunction from *s*.""" tokens = lex(s, pattern=TRIPLE_RE) return self._parse_triples(tokens)
def _lex(s): return [tok.type for tok in lexer.lex(s, pattern=lexer.TRIPLE_RE)]