-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathparser.py
More file actions
87 lines (71 loc) · 1.99 KB
/
parser.py
File metadata and controls
87 lines (71 loc) · 1.99 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
# !/usr/bin/env python2
# -*- coding:utf-8 -*-
#
# Author: Flyaway - flyaway1217@gmail.com
# Blog: zhouyichu.com
#
# Python release: 3.4.1
#
# Date: 2016-10-12 09:29:56
# Last modified: 2016-10-15 17:16:55
"""
Create tree structures from string.
1. Penn Tree Bank:
The format should be:
(IP (NP (NP (NR 上海) (NR 浦东)) (NP (NN 开发) (CC 与)
(NN 法制) (NN 建设))) (VP (VV 同步)))
"""
from __future__ import unicode_literals
import collections
from .tree import Node
from .tree import Tree
class ParsingError(Exception):
"""Exception class of parser.
"""
def __init__(self, errormessage):
self.errormessage = 'Parsing Error:'
self.errormessage += errormessage
def create_from_bracket_string(line):
"""Create tree structure from a given string.
"""
stack = collections.deque()
if len(line.strip()) == 0:
raise ParsingError('Empty String !')
# Clean the input string
sentence = line.replace('(', ' ( ')
sentence = sentence.replace(')', ' ) ')
sentence = sentence.split()
for char in sentence:
if char != ')':
stack.append(char)
else:
if type(stack[-1]) == unicode and stack[-2] == '(':
stack.append(char)
else:
_stack_operation(stack)
root = stack.pop()
return Tree(root)
def _stack_operation(stack):
"""The standard operation on the stack.
Pop items and insert a new item.
Args:
stack
"""
if len(stack) == 0:
return
children = []
# Special case for '(' or ')' as the content of sentence.
value = stack.pop()
if type(value) == unicode:
value = Node(value)
children.append(value)
while len(stack) != 0 and stack[-1] != '(':
value = stack.pop()
if type(value) == unicode:
value = Node(value)
children.append(value)
parent = children.pop()
children.reverse()
parent.children = children
stack.pop()
stack.append(parent)