Parse text using CaboCha, then return Tree instance.
332403
def parse_to_tree(text):
    """"""
    xml_text = cabocha.as_xml(text)
    tree = Tree(xml_text)
    return tree
544201
def parsesingle(s, strictmode=True, expansionlimit=None, convertpos=False):
    ''''''
    p = _parser(s, strictmode=strictmode, expansionlimit=expansionlimit)
    tree = p.parse()
    if convertpos:
        ast.posconverter(s).visit(tree)
    return tree
1122221
public function parse(string $text) : \stdClass
    {
        $items = Text::split($text, $this->separator);
        $itemsCount = count($items);
        $itemIndex = 0;
        $names = array_keys($this->fields);
        $namesCount = count($names);
        $nameIndex = 0;
        $index = 0;
        $separatorLength = strlen($this->separator);
        $itemsLeft = false;
        $this->clearValues();
        try {
            while ($itemIndex < $itemsCount && $nameIndex < $namesCount) {
                $syntax = $this->fields[$names[$nameIndex]];
                $this->values[$names[$nameIndex]]->value = $syntax->parse($items[$itemIndex]);
                $this->values[$names[$nameIndex]]->error = null;
                $index += strlen($items[$itemIndex]) + $separatorLength;
                $nameIndex ++;
                $itemIndex ++;
                if ($syntax instanceof OptionalSyntax && !$syntax->success()) {
                    $itemIndex --;
                }
            }
            // if items are left
            if ($itemIndex < $itemsCount)
                $itemsLeft = true;
            // if fields are left
            while ($nameIndex < $namesCount) {
                $syntax = $this->fields[$names[$nameIndex]];
                $this->values[$names[$nameIndex]]->value = $syntax->parse('');
                $this->values[$names[$nameIndex]]->error = null;
                $nameIndex ++;
            }
        } catch (ParseException $e) {
            if ($itemIndex < $itemsCount) {
                $this->values[$names[$nameIndex]]->error = static::INVALID_FIELD;
                $error = "Unable to parse the item '{$items[$itemIndex]}' for field '{$names[$nameIndex]}'";
                $extra = [
                    'type'  => 'invalid-field',
                    'field' => $names[$nameIndex],
                    'text'  => $items[$itemIndex]
                ];
            } else {
                $this->values[$names[$nameIndex]]->error = static::MISSING_FIELD;
                $error = "No item left for field '{$names[$nameIndex]}'";
                $extra = [
                    'type'  => 'missing-field',
                    'field' => $names[$nameIndex],
                    'position' => $e->position()
                ];
            }
            throw new ParseException($this, $text, $index + $e->position(), $error, $extra, $e);
        }

        if ($itemsLeft) {
            $extra = [
                'type'  => 'additional-items',
                'items' => array_slice($items, $itemIndex)
            ];
            throw new ParseException($this, $text, $index - $separatorLength,
                "Additional items with no corresponding fields", $extra);
        }

        return (object) array_map(function(\stdClass $field) {
            return $field->value;
        }, $this->values);
    }
762574
def load_grammar(self, grammar_text, grammar_name='<?>'):
        ""

        try:
            tree = self.canonize_tree.transform( self.parser.parse(grammar_text+'\n') )
        except UnexpectedCharacters as e:
            context = e.get_context(grammar_text)
            raise GrammarError("Unexpected input at line %d column %d in %s: \n\n%s" %
                               (e.line, e.column, grammar_name, context))
        except UnexpectedToken as e:
            context = e.get_context(grammar_text)
            error = e.match_examples(self.parser.parse, {
                'Unclosed parenthesis': ['a: (\n'],
                'Umatched closing parenthesis': ['a: )\n', 'a: [)\n', 'a: (]\n'],
                'Expecting rule or terminal definition (missing colon)': ['a\n', 'a->\n', 'A->\n', 'a A\n'],
                'Alias expects lowercase name': ['a: -> "a"\n'],
                'Unexpected colon': ['a::\n', 'a: b:\n', 'a: B:\n', 'a: "a":\n'],
                'Misplaced operator': ['a: b??', 'a: b(?)', 'a:+\n', 'a:?\n', 'a:*\n', 'a:|*\n'],
                'Expecting option ("|") or a new rule or terminal definition': ['a:a\n()\n'],
                '%import expects a name': ['%import "a"\n'],
                '%ignore expects a value': ['%ignore %import\n'],
            })
            if error:
                raise GrammarError("%s at line %s column %s\n\n%s" % (error, e.line, e.column, context))
            elif 'STRING' in e.expected:
                raise GrammarError("Expecting a value at line %s column %s\n\n%s" % (e.line, e.column, context))
            raise

        tree = PrepareGrammar().transform(tree)

        # Extract grammar items
        defs = classify(tree.children, lambda c: c.data, lambda c: c.children)
        term_defs = defs.pop('term', [])
        rule_defs = defs.pop('rule', [])
        statements = defs.pop('statement', [])
        assert not defs

        term_defs = [td if len(td)==3 else (td[0], 1, td[1]) for td in term_defs]
        term_defs = [(name.value, (t, int(p))) for name, p, t in term_defs]
        rule_defs = [options_from_rule(*x) for x in rule_defs]

        # Execute statements
        ignore, imports = [], {}
        for (stmt,) in statements:
            if stmt.data == 'ignore':
                t ,= stmt.children
                ignore.append(t)
            elif stmt.data == 'import':
                if len(stmt.children) > 1:
                    path_node, arg1 = stmt.children
                else:
                    path_node, = stmt.children
                    arg1 = None

                if isinstance(arg1, Tree):  # Multi import
                    dotted_path = tuple(path_node.children)
                    names = arg1.children
                    aliases = dict(zip(names, names))  # Can't have aliased multi import, so all aliases will be the same as names
                else:  # Single import
                    dotted_path = tuple(path_node.children[:-1])
                    name = path_node.children[-1]  # Get name from dotted path
                    aliases = {name: arg1 or name}  # Aliases if exist

                if path_node.data == 'import_lib':  # Import from library
                    base_paths = []
                else:  # Relative import
                    if grammar_name == '<string>':  # Import relative to script file path if grammar is coded in script
                        try:
                            base_file = os.path.abspath(sys.modules['__main__'].__file__)
                        except AttributeError:
                            base_file = None
                    else:
                        base_file = grammar_name  # Import relative to grammar file path if external grammar file
                    if base_file:
                        base_paths = [os.path.split(base_file)[0]]
                    else:
                        base_paths = [os.path.abspath(os.path.curdir)]

                try:
                    import_base_paths, import_aliases = imports[dotted_path]
                    assert base_paths == import_base_paths, 'Inconsistent base_paths for %s.' % '.'.join(dotted_path)
                    import_aliases.update(aliases)
                except KeyError:
                    imports[dotted_path] = base_paths, aliases

            elif stmt.data == 'declare':
                for t in stmt.children:
                    term_defs.append([t.value, (None, None)])
            else:
                assert False, stmt

        # import grammars
        for dotted_path, (base_paths, aliases) in imports.items():
            grammar_path = os.path.join(*dotted_path) + EXT
            g = import_grammar(grammar_path, base_paths=base_paths)
            new_td, new_rd = import_from_grammar_into_namespace(g, '__'.join(dotted_path), aliases)

            term_defs += new_td
            rule_defs += new_rd

        # Verify correctness 1
        for name, _ in term_defs:
            if name.startswith('__'):
                raise GrammarError('Names starting with double-underscore are reserved (Error at %s)' % name)

        # Handle ignore tokens
        # XXX A slightly hacky solution. Recognition of %ignore TERMINAL as separate comes from the lexer's
        #     inability to handle duplicate terminals (two names, one value)
        ignore_names = []
        for t in ignore:
            if t.data=='expansions' and len(t.children) == 1:
                t2 ,= t.children
                if t2.data=='expansion' and len(t2.children) == 1:
                    item ,= t2.children
                    if item.data == 'value':
                        item ,= item.children
                        if isinstance(item, Token) and item.type == 'TERMINAL':
                            ignore_names.append(item.value)
                            continue

            name = '__IGNORE_%d'% len(ignore_names)
            ignore_names.append(name)
            term_defs.append((name, (t, 1)))

        # Verify correctness 2
        terminal_names = set()
        for name, _ in term_defs:
            if name in terminal_names:
                raise GrammarError("Terminal '%s' defined more than once" % name)
            terminal_names.add(name)

        if set(ignore_names) > terminal_names:
            raise GrammarError("Terminals %s were marked to ignore but were not defined!" % (set(ignore_names) - terminal_names))

        resolve_term_references(term_defs)

        rules = rule_defs

        rule_names = set()
        for name, _x, _o in rules:
            if name.startswith('__'):
                raise GrammarError('Names starting with double-underscore are reserved (Error at %s)' % name)
            if name in rule_names:
                raise GrammarError("Rule '%s' defined more than once" % name)
            rule_names.add(name)

        for name, expansions, _o in rules:
            used_symbols = {t for x in expansions.find_data('expansion')
                              for t in x.scan_values(lambda t: t.type in ('RULE', 'TERMINAL'))}
            for sym in used_symbols:
                if sym.type == 'TERMINAL':
                    if sym not in terminal_names:
                        raise GrammarError("Token '%s' used but not defined (in rule %s)" % (sym, name))
                else:
                    if sym not in rule_names:
                        raise GrammarError("Rule '%s' used but not defined (in rule %s)" % (sym, name))

        return Grammar(rules, term_defs, ignore_names)
738707
protected function parse($text) {
        // Build tree structure
        $in = array();
        for ($ptr=0,$len=strlen($text),$tree=array(),$node=&$tree,
            $stack=array(),$depth=0,$tmp='';$ptr<$len;)
            if (preg_match('/^@(end)?('.$this->tags.')\b(.*)/i', substr($text,$ptr),$match)) {
                $single = substr($match[3], strlen($match[3])-1)==='@';
                !$single || $match[3] = substr($match[3], 0, strlen($match[3])-1);
                if (strlen($tmp))
                    $node[] = $tmp;
                // Element node
                if ($match[1]) {
                    if ($in) {
                        // in element
                        for ($i=count($in)-1; $i > -1 ; --$i)
                            $in[$i][1] = array_pop($node);
                        $node['@in'] = $in;
                        $in = array();
                    }
                    // Find matching start tag
                    $save  = $depth;
                    $found = false;
                    while ($depth>0) {
                        $depth--;
                        foreach ($stack[$depth] as $item)
                            if (is_array($item) && isset($item[$match[2]])) {
                                // Start tag found
                                $found = true;
                                break 2;
                            }
                    }
                    if (!$found)
                        // Unbalanced tag
                        $depth = $save;
                    $node =& $stack[$depth];
                }
                elseif ($this->intag($match[2])) {
                    $in[] = array($match[2], '@attrib'=>$this->parseAttrib($match[3]), '');
                }
                else {
                    // Start tag
                    $stack[$depth]   =& $node;
                    $node            =& $node[][$match[2]];
                    $node['@attrib'] = $this->parseAttrib($match[3]);
                    if ($single)
                        // single
                        $node =& $stack[$depth];
                    else
                        $depth++;
                }
                $tmp = '';
                $ptr += strlen($match[0]);
            }
            else {
                // Text node
                $tmp .= substr($text,$ptr,1);
                $ptr++;
            }
        if (strlen($tmp))
            // Append trailing text
            $node[] = $tmp;
        // Break references
        unset($node, $stack);

        return $tree;
    }
93282
def parse(self, tokenized):  # pylint: disable=invalid-name
        """"""
        table, trees = _parse(tokenized, self.grammar)
        # Check if the parse succeeded.
        if all(r.lhs != self.start for r in table[(0, len(tokenized) - 1)]):
            raise ParseError('Parsing failed.')
        parse = trees[(0, len(tokenized) - 1)][self.start]
        return self._to_tree(revert_cnf(parse))
134597
def _parse(self, html):
        """"""
        if self._has_body_re.search(html):
            tree = lxml.html.document_fromstring(html).find('.//body')
            self.has_body = True
        else:
            tree = lxml.html.fragment_fromstring(html,
                                                 create_parent=self._root_tag)

        if tree.tag != self._root_tag:
            # ensure the root element exists even if not really needed,
            # so the tree has always the same structure
            root = lxml.html.HtmlElement()
            root.tag = self._root_tag
            root.append(tree)
            return root
        return tree
212319
def parse_text(text: str, schema: dict) -> Any:
    
    try:
        data = json.loads(text)
        jsonschema.validate(data, schema)
    except (TypeError, json.decoder.JSONDecodeError):
        raise jsonschema.ValidationError("Could not parse json")

    return data
1221506
def parse(self, text):
        ''''''
        in_comment = False
        result = []
        for s in iter(text.splitlines()):
            self.log.debug('Parsing string: {}'.format(s))
            rsc = self.RE_START_COMMENT.match(s)
            if rsc:
                if in_comment:
                    self.log.debug('Double comment start!')
                in_comment = True
                comment = rsc.group(1)
                jdoc = JDOC('', '', comment)
            if in_comment:
                rcb = self.RE_COMMENT_BODY.match(s)
                rp = self.RE_PARAM.match(s)
                rec = self.RE_END_COMMENT.match(s)
                if rp:
                    if rsc:
                        self.log.debug('Comment start and param on same line!')
                    param_name = rp.group(1)
                    param_desc = rp.group(2)
                    if param_name in ['function', 'trigger', 'procedure']:
                        rso = self.RE_SCHEMA_OBJECT.match(param_desc)
                        jdoc.object_type = param_name
                        if rso:
                            jdoc.schema_name = rso.group(1)
                            if jdoc.schema_name:
                                jdoc.schema_name = jdoc.schema_name.strip(".")
                            if not jdoc.schema_name or jdoc.schema_name == "":
                                jdoc.schema_name = self.default_schema
                            jdoc.object_name = rso.group(2)
                    elif param_name in ['return',
                                        'returns',
                                        'returning',
                                        'result']:
                        jdoc.returns = param_desc
                    else:
                        jdoc.add_param(param_name, param_desc)
                    self.log.debug('Param {} = {}'.format(param_name, param_desc))
                if rcb:
                    comment = comment + ' ' + rcb.group(1)
                    jdoc.comment = comment
                if rec:
                    in_comment = False
                    result.append(copy.deepcopy(jdoc))
                    del jdoc
                    self.log.debug('Comment:' + comment)
        return result
741783
def from_text(cls, text, mapping='mapping'):
        
        graphemes = Counter(grapheme_pattern.findall(text))
        specs = [
            OrderedDict([
                (cls.GRAPHEME_COL, grapheme),
                ('frequency', frequency),
                (mapping, grapheme)])
            for grapheme, frequency in graphemes.most_common()]
        return cls(*specs)
469625
def parse(some_text, **kwargs):
    
    ap = parser.AddressParser(**kwargs)
    return ap.parse(some_text)
751742
def text_to_data(self, text, elt, ps):
        '''
        '''
        if text is None:
            return None
        
        m = self.lex_pattern.match(text)
        if not m:
            raise EvaluateException('Bad Gregorian: %s' %text, ps.Backtrace(elt))
        try:
            retval = _dict_to_tuple(m.groupdict())
        except ValueError, e:
            #raise EvaluateException(str(e))
            raise
        
        if self.pyclass is not None:
            return self.pyclass(retval)
        return retval
420459
public function parse($text, $context = null)
    {
        $parser = $this->getParser($context);

        $this->events->dispatch(new Parsing($parser, $context, $text));

        return $parser->parse($text);
    }
677443
def tree(s, token=[WORD, POS, CHUNK, PNP, REL, LEMMA]):
    
    return Text(s, token)
575034
def get_edu_text(text_subtree):
    """"""
    assert text_subtree.label() == SubtreeType.text
    return u' '.join(word.decode('utf-8') for word in text_subtree.leaves())
35789
def etree_to_text(tree,
                  guess_punct_space=True,
                  guess_layout=True,
                  newline_tags=NEWLINE_TAGS,
                  double_newline_tags=DOUBLE_NEWLINE_TAGS):
    
    chunks = []

    _NEWLINE = object()
    _DOUBLE_NEWLINE = object()

    class Context:
        """ workaround for missing `nonlocal` in Python 2 """
        # _NEWLINE, _DOUBLE_NEWLINE or content of the previous chunk (str)
        prev = _DOUBLE_NEWLINE

    def should_add_space(text, prev):
        """ Return True if extra whitespace should be added before text """
        if prev in {_NEWLINE, _DOUBLE_NEWLINE}:
            return False
        if not _has_trailing_whitespace(prev):
            if _has_punct_after(text) or _has_open_bracket_before(prev):
                return False
        return True

    def get_space_between(text, prev):
        if not text or not guess_punct_space:
            return ' '
        return ' ' if should_add_space(text, prev) else ''

    def add_newlines(tag, context):
        if not guess_layout:
            return
        prev = context.prev
        if prev is _DOUBLE_NEWLINE:  # don't output more than 1 blank line
            return
        if tag in double_newline_tags:
            context.prev = _DOUBLE_NEWLINE
            chunks.append('\n' if prev is _NEWLINE else '\n\n')
        elif tag in newline_tags:
            context.prev = _NEWLINE
            if prev is not _NEWLINE:
                chunks.append('\n')

    def add_text(text_content, context):
        text = _normalize_whitespace(text_content) if text_content else ''
        if not text:
            return
        space = get_space_between(text, context.prev)
        chunks.extend([space, text])
        context.prev = text_content

    def traverse_text_fragments(tree, context, handle_tail=True):
        """ Extract text from the ``tree``: fill ``chunks`` variable """
        add_newlines(tree.tag, context)
        add_text(tree.text, context)
        for child in tree:
            traverse_text_fragments(child, context)
        add_newlines(tree.tag, context)
        if handle_tail:
            add_text(tree.tail, context)

    traverse_text_fragments(tree, context=Context(), handle_tail=False)
    return ''.join(chunks).strip()
767328
def parse(text):
    
    rv = {}
    m = META.match(text)

    while m:
        key = m.group(1)
        value = m.group(2)
        value = INDENTATION.sub('\n', value.strip())
        rv[key] = value
        text = text[len(m.group(0)):]
        m = META.match(text)

    return rv, text