-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathloan_parser.py
More file actions
175 lines (134 loc) · 5.11 KB
/
loan_parser.py
File metadata and controls
175 lines (134 loc) · 5.11 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
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
# -*- coding: utf-8 -*-
import sys
import re
from datetime import datetime
from datetime import date
from parser import DocumentParser
class LoanParser(DocumentParser):
SELECTION_MARKER = r".*(Loan Facility|Loan Agreement|Financial Agreement).*"
ESCAPE_MARKERS = r".*(the date on which notice of the motion is given|Date:|Venue:|met on ).*"
def __init__(self, content):
super(LoanParser, self).__init__(content)
@classmethod
def filter_out_parties_to_agreement(self,line_content):
line_content = line_content.replace(',',' ')
all_parties1 = line_content.split('between',1)[-1]
if len(all_parties1)>1:
all_parties = all_parties1.split('and',1)
party_1 = all_parties[0]
if len(all_parties)>1:
party_2 = all_parties[1].split('for an amount')[0].split('—',1)[0]
else:
party_2 = ''
return (party_1 , party_2)
else:
return ( '', '' )
@classmethod
def filter_out_loan_purpose(self,line_content):
line_content = line_content.replace(',',' ')
loan_details = line_content.split('to finance',1)
if len(loan_details)>1:
return loan_details[1]
else:
#loan_details[0]
return ''
@classmethod
def filter_out_loan_purpose_via_party_2(self,line_content):
line_content = line_content.replace(',',' ')
all_parties1 = line_content.split('between',1)[-1]
if len(all_parties1)>1:
all_parties = all_parties1.split('and',1)
print all_parties
if len(all_parties)>1:
purpose = all_parties[1].split('for an amount')[0].split('—',1)[1]
#party_2 = all_parties[1].split('for an amount')[0].split('—',1)[0]
else:
purpose = ''
return purpose
else:
return ''
@classmethod
def filter_out_loan_amount(self,line_content):
line_content = line_content.replace(',',' ')
loan_details = line_content.split('amounting to',1)
if len(loan_details)>1:
amount = loan_details[1].split('between',1)[0]
return amount
else:
loan_details1 = line_content.split('an amount of',1)
if len(loan_details1)>1:
loan_details2 =loan_details1[1]
amount = loan_details2.split('to finance',1)[0]
else:
amount = ''
#loan_details[0]
return amount
@classmethod
def parse_loan_summary(cls, lines):
thekind, line, match = None, None, None
loans = []
valid = False
party_1 = ''
party_2 = ''
purpose = ''
amount = '';
while len(lines):
thekind, line, match = lines.pop(0)
line_content = line.strip()
if (thekind == cls.BLANK ):
continue
matchObjectForLoan = re.match( cls.SELECTION_MARKER , line_content)
matchObjectForEscapeMarkers = re.match( cls.ESCAPE_MARKERS , line_content)
if matchObjectForEscapeMarkers:
# START PARSING SECTION
valid = False
continue
if matchObjectForLoan:
# START PARSING SECTION
valid = True
pass
if valid:
if matchObjectForLoan:
all_parties = cls.filter_out_parties_to_agreement(line_content)
party_1 = all_parties[0]
party_2 = all_parties[1]
purpose = cls.filter_out_loan_purpose(line_content)
amount = cls.filter_out_loan_amount(line_content)
if (thekind == cls.LINE or thekind == cls.BLANK or thekind == cls.PAGE_HEADER):
continue
else:
# fallout
continue
if len(loans)==0:
continue
if party_1!='' and party_2!='' and amount!='' and purpose!='':
loans.append(dict(party_1=party_1,
party_2=party_2,
amount=amount,
purpose=purpose
))
return loans
@classmethod
def parse_body(cls, lines):
entries = []
loan_all = cls.parse_loan_summary( list(lines) )
for loan in loan_all:
if len(loan)>2:
entries.append(loan)
return entries
def main(argv):
print 'Party 1 | Party 2 | Amount | Purpose '
for filename in sys.stdin:
handle = open(filename.strip(), 'r')
content = handle.read()
handle.close()
p = LoanParser(content)
p.parse()
data = p.output()
for row in data:
print '%s|%s|%s|%s' % (row['party_1'],
row['party_2'],
row['amount'],
row['purpose'])
if __name__ == "__main__":
main(sys.argv)