9Dome commited on
Commit
f6e45ae
Β·
verified Β·
1 Parent(s): aed394c

Create utils.py

Browse files
Files changed (1) hide show
  1. utils.py +161 -0
utils.py ADDED
@@ -0,0 +1,161 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ """
2
+ utils.py - Utility functions for the project.
3
+ """
4
+ import logging
5
+ import re
6
+
7
+
8
+ def postprocess(text: str):
9
+ """
10
+ postprocess - remove common values in scraped dataset
11
+ Args:
12
+ text (str): the text to postprocess
13
+ """
14
+
15
+ replacements = {
16
+ "ENA": "COMPANY",
17
+ "Enron": "COMPANY",
18
+ "Enron Corporation": "COMPANY",
19
+ "Sony Pictures Entertainment": "COMPANY",
20
+ "Columbia Pictures": "COMPANY",
21
+ "Sony": "COMPANY",
22
+ "Columbia": "COMPANY",
23
+ "Hillary": "Jane",
24
+ "Clinton": "Smith",
25
+ "Amy": "Jane",
26
+ "Sara": "Jane",
27
+ "Harambe": "Jane",
28
+ "Pascal": "PERSON",
29
+ }
30
+
31
+ # replace common values, also check lowercase
32
+ for k, v in replacements.items():
33
+ text = text.replace(k, v)
34
+ text = text.replace(k.lower(), v)
35
+
36
+ return text
37
+
38
+
39
+ def clear(text, verbose=False, **kwargs):
40
+ """for use with buttons"""
41
+ if verbose:
42
+ logging.info(f"Clearing text: {text}")
43
+ return ""
44
+
45
+
46
+ def make_email_link(
47
+ subject: str = "Email subject - This was generated by Postbot",
48
+ link_text: str = "click to open in your email client",
49
+ body: str = None,
50
+ tag_placeholder: str = "PLACEHOLDER",
51
+ ):
52
+ """
53
+ email_link - generate an email link
54
+ Args:
55
+ subject (str, optional): the subject of the email. Defaults to "Email subject - This was generated by Postbot".
56
+ link_text (str, optional): the text of the link. Defaults to "click to open in your email client".
57
+ body (str, optional): the body of the email. Defaults to None.
58
+ tag_placeholder (str, optional): the placeholder for the tag. Defaults to "PLACEHOLDER".
59
+ Returns:
60
+ str: the email link, in the form of an html link
61
+ """
62
+
63
+ if body is None:
64
+ body = "hmm - no body. replace me"
65
+
66
+ # strip brackets and other HTML-tag characters from body with regex
67
+ body = re.sub(r"<[^>]*>", tag_placeholder, body)
68
+
69
+ # replace all newline chars with a whitespace
70
+ body = body.replace("\n", " ")
71
+
72
+ nice_html_button = f"""<!DOCTYPE html>
73
+ <html>
74
+ <head>
75
+ <title>Generated Email</title>
76
+ <style>
77
+ body {{
78
+ font-family: sans-serif;
79
+ font-size: 1.2em;
80
+ }}
81
+ .button {{
82
+ background-color: #6CCEC6;
83
+ border: none;
84
+ color: white;
85
+ padding: 15px 32px;
86
+ text-align: center;
87
+ text-decoration: none;
88
+ display: inline-block;
89
+ font-size: 16px;
90
+ margin: 4px 2px;
91
+ cursor: pointer;
92
+ value: "Send Email";
93
+ }}
94
+ </style>
95
+ <button class="button" onclick="window.location.href='mailto:?subject={subject}&body={body}'">{link_text} value="Open in Email client"</button>
96
+ </html>"""
97
+
98
+ # return f'<a href="mailto:%20?subject={subject}&body={body}">{link_text}</a>'
99
+ return nice_html_button
100
+
101
+
102
+ def make_mailto_form(
103
+ body: str = None,
104
+ subject: str = "This email was generated by Postbot! tiny.cc/postbot-demo",
105
+ cc_email: str = "",
106
+ ):
107
+ """Returns a mailto link with the given parameters"""
108
+
109
+ if body is None:
110
+ body = "hmm - no body. Replace me or try rerunning the model."
111
+
112
+ template = f"""<!DOCTYPE html>
113
+ <html>
114
+ <head>
115
+ <title>Generated Email</title>
116
+ <style>
117
+ body {{
118
+ font-family: sans-serif;
119
+ font-size: 1.2em;
120
+ }}
121
+ .button {{
122
+ background-color: #6CCEC6;
123
+ border: none;
124
+ color: white;
125
+ padding: 15px 32px;
126
+ text-align: center;
127
+ text-decoration: none;
128
+ display: inline-block;
129
+ font-size: 16px;
130
+ margin: 4px 2px;
131
+ cursor: pointer;
132
+ value: "Send Email";
133
+ }}
134
+ </style>
135
+ </head>
136
+ <body>
137
+ <h1>Adjust and Open in your mail client:</h1>
138
+ <form action="mailto:" method="get" enctype="text/plain">
139
+ <div>
140
+ <label for="cc">CC Email:
141
+ <input type="text" name="cc" id="cc" value="{cc_email}"/>
142
+ </label>
143
+ </div>
144
+ <div>
145
+ <label for="subject">Subject:
146
+ <input type="text" name="subject" id="subject" value="{subject}"/>
147
+ </label>
148
+ </div>
149
+ <div>
150
+ <label>Email Body:</label>
151
+ <br />
152
+ <textarea name="body" id="body" rows="12" cols="35">{body}</textarea>
153
+ </div>
154
+ <div>
155
+ <input type="submit" name="submit" value="Open in Email App" class="button"/>
156
+ </div>
157
+ </form>
158
+ </body>
159
+ </html>"""
160
+
161
+ return template