-
Notifications
You must be signed in to change notification settings - Fork 0
/
parse.py
194 lines (163 loc) · 5.78 KB
/
parse.py
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
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
"""
CMPUT 291 MiniProject 2
"""
import sys, re, os
class ParseMail(object):
# CONSTRUCTOR:
def __init__(self):
self.thisEmail = {}
self.current = 0
self.thisToken = None
self.term = None
## PUBLIC METHODS: ##
def execute(self):
# public entry point to the parser
self.__mail__()
## PRIVATE METHODS: ##
def __mail__(self):
self.__openFiles__()
self.thisEmail = {}
self.thisEmail["xml"] = ""
sys.stdin.readline()
sys.stdin.readline()
self.__getTokens__()
while(self.__match__("^\s*(<mail>)|(<emails type=[\w\"\']*>)\s*")):
self.__emailInfo__()
self.thisEmail.clear()
self.thisEmail["xml"] = ""
self.__getTokens__()
self.__closeFiles__()
def __emailInfo__(self):
while(not self.__match__("^\s*</mail>\s*$", appendXml=False)):
if re.search("^\s*<([\w]+)>([\s\w/!@#$%^&*\.:\-,\?\(\)\"\'\+=\\/\[\]\{\}]+)?</[\w]+>\s*$", self.__tokenPeek__()):
# <tag>data</tag>
# this line contains
tagVal = re.findall("^\s*<([\w]+)>([\s\w/!@#$%^&*\.:\-,\?\(\)\"\'\+=\\/\[\]\{\}]+)?</[\w]+>\s*$", self.__consume__())
self.thisEmail[tagVal[0][0]] = tagVal[0][1]
if re.search("^\s*<([a-zA-z]+)>\s*$", self.__tokenPeek__()):
# <tag>
# next token is data
# open tag with contents as next token
tagname = re.findall("^\s*<([a-zA-z]+)>\s*$", self.__tokenPeek__())
if (tagname[0] != "mail"):
self.__consume__()
if not re.search("^\s*</([a-zA-z]+)>\s*$", self.__tokenPeek__()):
self.thisEmail[tagname[0]] = self.__consume__()
else:
self.thisEmail[tagname[0]] = ""
else:
self.__consume__(appendXml=False)
if re.search("^\s*<([a-zA-z]+)/>\s*$", self.__tokenPeek__()):
# <emptyTag/>
# empty tag
# give email an empty tagname
tagname = re.findall("^\s*<([a-zA-z]+)/>\s*$", self.__consume__())
self.thisEmail[tagname[0]] = ""
if re.search("^\s*</([a-zA-z]+)>\s*$", self.__tokenPeek__()):
# </endTag>
# end of valid open tag
tag = re.findall("^\s*</([a-zA-z]+)>\s*$", self.__tokenPeek__())
if tag[0] == "mail":
self.__consume__(appendXml=False)
break
else:
self.__consume__()
self.__createTerm__()
self.__createEmails__()
self.__createDates__()
self.__createRecs__()
def __getTokens__(self):
self.current = 0
self.tokenList = []
tmp = re.split("(<[\w/]+>)", sys.stdin.readline())
[self.tokenList.append(x) for x in tmp if not str(x).strip() == ""]
def __createTerm__(self):
if self.thisEmail != {}:
# sanitize output and write to term.txt
id = self.thisEmail["row"]
# subject line
for term in re.split(" |[^a-zA-Z0-9\-_]*|&#[\d]+", self.thisEmail["subj"]):
if len(term) > 2:
self.term.write("s-" + term.lower() + ":" + id + "\n")
# body
for term in re.split(" |[^a-zA-Z0-9\-_]*|&#[\d]+", self.thisEmail["body"]):
if len(term) > 2:
self.term.write("b-" + term.lower() + ":" + id + "\n")
def __createEmails__(self):
if self.thisEmail != {}:
id = self.thisEmail["row"]
if self.thisEmail["from"] != "":
self.emails.write("from-" + self.thisEmail["from"] + ":" + id + "\n")
if self.thisEmail["to"] != "":
for emails in self.thisEmail["to"].strip().split(","):
self.emails.write("to-" + emails + ":" + id + "\n")
if self.thisEmail["cc"] != "":
for emails in self.thisEmail["cc"].strip().split(","):
self.emails.write("cc-" + emails + ":" + id + "\n")
if self.thisEmail["bcc"] != "":
for emails in self.thisEmail["bcc"].strip().split(","):
self.emails.write("bcc-" + emails + ":" + id + "\n")
def __createDates__(self):
if self.thisEmail != {}:
id = self.thisEmail["row"]
if self.thisEmail["date"] != []:
self.dates.write(self.thisEmail["date"] + ":" + id + "\n")
def __createRecs__(self):
if self.thisEmail != {}:
if re.search("^<mail>", self.thisEmail["xml"]):
sep = ":"
else:
sep = ":<mail>"
self.recs.write(self.thisEmail["row"] + sep + self.thisEmail["xml"] + "\n")
def __sortFiles__(self):
os.system("sort terms.txt -u -o terms_sorted.txt")
os.system("sort emails.txt -u -o emails_sorted.txt")
os.system("sort dates.txt -u -o dates_sorted.txt")
os.system("sort recs.txt -u -n -o recs_sorted.txt")
## HELPER METHODS: ##
def __openFiles__(self):
self.term = open("terms.txt", mode = "w", encoding = "utf-8")
self.emails = open("emails.txt", mode = "w", encoding = "utf-8")
self.dates = open("dates.txt", mode = "w", encoding = "utf-8")
self.recs = open("recs.txt", mode = "w", encoding = "utf-8")
def __closeFiles__(self):
self.term.close()
self.emails.close()
self.dates.close()
self.recs.close()
def __match__(self, pattern, appendXml=True):
# check if the current token matches the pattern, increment current if so
if not self.thisToken:
if self.current < len(self.tokenList):
self.thisToken = self.tokenList[self.current].strip()
self.current += 1
else:
self.thisToken = ""
self.thisEmail["xml"] += self.thisToken.strip()
if re.search(pattern, self.thisToken):
if self.current < len(self.tokenList):
self.thisToken = self.tokenList[self.current].strip()
self.current += 1
else:
self.thisToken = ""
if appendXml:
self.thisEmail["xml"] += self.thisToken.strip()
return True
return False
def __tokenPeek__(self):
return self.thisToken
def __consume__(self, appendXml=True):
tmp = self.thisToken
if self.current < len(self.tokenList):
self.thisToken = self.tokenList[self.current].strip()
self.current += 1
else:
self.thisToken = ""
if appendXml:
self.thisEmail["xml"] += self.thisToken.strip()
return tmp
def main():
parser = ParseMail()
parser.execute()
if __name__ == "__main__":
main()