-
Notifications
You must be signed in to change notification settings - Fork 2
/
email_reply_parser_test.go
419 lines (362 loc) · 9.84 KB
/
email_reply_parser_test.go
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
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
package email_reply_parser //nolint:stylecheck,golint
import (
"fmt"
"io/ioutil"
"os"
"path/filepath"
"strings"
"testing"
)
func TestIsQuotedEmailStart(t *testing.T) {
shouldReturnTrue := []string{
"On Monday, November 4, 2013 4:29 PM, John Smith <john.smith@example.org> wrote:",
"2013/11/1 John Smith <john@smith.org>",
"On Monday, November 4, 2013 4:29 PM, John Smith <john.smith@example.org> wrote:",
"on mon, aug 26, 2019 at 4:37 pm the hiring engine <a-really-long-automated-email+1234556@humanresources.com> wrote:",
}
for _, should := range shouldReturnTrue {
if isQuotedEmailStart(strings.ToLower(should)) != true {
t.Errorf("Should return true: %v", should)
}
}
shouldReturnFalse := []string{
"since on Monday, November 4, John Smith wrote me this message",
"You see this this the problem",
}
for _, should := range shouldReturnFalse {
if isQuotedEmailStart(strings.ToLower(should)) != false {
t.Errorf("Should return false: %v", should)
}
}
}
func TestIsName(t *testing.T) {
shouldReturnTrue := []string{
"Richard Lindhout",
"Karen The Green",
"Jan de Smit",
"Richard Lindhout | Software Engineer",
"Richard Lindhout, Software Engineer",
}
for _, should := range shouldReturnTrue {
if isName(should) != true {
t.Errorf("Should return true: %v", should)
}
}
shouldReturnFalse := []string{
"Hi",
"Ok",
"tel 01666666 ",
"email 01666666 ",
"kvk 01666666 ",
"btw 01666666 ",
"TEL 01666666 ",
"Email 01666666 ",
"KvK 01666666 ",
"BTW 01666666 ",
"Street 2, City, Zeeland, 4694EG, NL",
"You see this this the problem",
}
for _, should := range shouldReturnFalse {
if isName(should) != false {
t.Errorf("Should return false: %v", should)
}
}
}
func TestPossibleSignature(t *testing.T) {
shouldReturnTrue := []string{
"WEB webRidge.nl <https://webridge.nl/>",
"IBAN NL93 BUNQ 0000 1111 22",
"BTW NL0000000AA0",
"Richard Lindhout",
"Karen The Green",
"Graphic Designer",
"Jan de Smit",
"Tel: +44423423423423",
"Fax: +44234234234234",
"Richard Lindhout | Software Engineer",
"Richard Lindhout, Software Engineer",
"tel 01666666",
"email 01666666",
"kvk 01666666",
"btw 01666666",
"TEL 01666666",
"Email 01666666",
"KvK 01666666",
"BTW 01666666",
"karen@webby.com",
"www.thing.com",
"thing.com",
"------",
"______",
"-Abhishek Kona",
"riak-users@lists.basho.com",
"http://lists.basho.com/mailman/listinfo/riak-users_lists.basho.com",
// TODO: address lines
}
for _, should := range shouldReturnTrue {
if isPossibleSignatureLine(should) != true {
t.Errorf("Should return true: %v", should)
}
}
shouldReturnFalse := []string{
"Hi",
"Ok",
"Lorem Ipsum is simply dummy text of the printing and typesetting industry.",
"Haha dit is supper grappig!",
"Ok!",
"Haha ok",
"That's not true",
"Her email address is karen@webby.com",
"Her website is facebook.com",
"You see this this the problem",
}
for _, should := range shouldReturnFalse {
if isPossibleSignatureLine(should) != false {
t.Errorf("Should return false: %v", should)
}
}
}
func TestGreetings(t *testing.T) {
shouldReturnTrue := []string{
"Met vriendelijke groeten,",
"best regards",
"groeten,",
"groeten",
}
for _, should := range shouldReturnTrue {
if detectGreetings(should) != true {
t.Errorf("Should return true: %v", should)
}
}
shouldReturnFalse := []string{
"hij zei nog dat je de groeten kreeg",
"de groeten van Jan",
"You see this this the problem",
}
for _, should := range shouldReturnFalse {
if detectGreetings(should) != false {
t.Errorf("Should return false: %v", should)
}
}
}
func TestRemoveSpacesBetweenNumbers(t *testing.T) {
before := "Mijn nummer is 0166 66 42 42 45 67"
after := "Mijn nummer is 01666642424567"
result := removeSpacesBetweenNumbers(before)
if result != after {
t.Errorf("is %v but should be %v", result, after)
}
}
func TestKarenSignature(t *testing.T) {
content := Parse(karenMail)
expected := "Hi this is my email"
if content != expected {
t.Errorf("expected: %v but is %v", expected, content)
}
}
const karenMail = `
Hi this is my email
Karen The Green
Graphic Designer
Office
Tel: +44423423423423
Fax: +44234234234234
karen@webby.com
Street 2, City, Zeeland, 4694EG, NL
www.thing.com
Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged.
> Steps 0-2 are in prod. Gonna let them sit for a bit then start cleaning up
> the old code with 3 & 4.
>
>
`
func TestRichardSignature(t *testing.T) {
if isQuoteOnTop(richardMail) {
t.Errorf("quote starts on bottom")
}
content := Parse(richardMail)
expected := ":+1:"
if content != expected {
t.Errorf("expected: `%v` but is `%v`", expected, content)
}
}
const richardMail = `
:+1:
*Richard Lindhout* | *Eigenaar*
*Bel mij +31 6 22 22 22 22* <+31622222222>
[image: logo webridge]
Beatrixlaan 2, 4694EG Scherpenisse
*KVK 50000000*
*BTW NL0000000AA0*
*IBAN NL93 BUNQ 0000 1111 22*
*WEB webRidge.nl <https://webridge.nl/>*
`
func TestRichardReverseSignature(t *testing.T) {
if !isQuoteOnTop(richardReverseMail) {
t.Errorf("quote starts on top")
}
content := Parse(richardReverseMail)
expected := ":+1:"
if content != expected {
t.Errorf("expected: `%v` but is `%v`", expected, content)
}
}
func TestMultilineOrSingleLine(t *testing.T) {
lines := plainMailToLines(multilineStartQuoteReply)
multi, single := detectQuotedEmailStart(0, lines[0], lines)
if !multi {
t.Errorf("Should be multi")
}
if single {
t.Errorf("single should be false")
}
}
const multilineStartQuoteReply = `Op za 8 mei 2021 om 12:09 schreef
Richard Lindhout <richardlindhout96@gmail.com>:`
const richardReverseMail = `
On Mon, Aug 26, 2019 at 4:37 PM The Hiring Engine <
a-really-long-automated-email+1234556@humanresources.com> wrote:
> dd
> sldfj
> slfjlsdf
> slkfjlksfj
:+1:
*Richard Lindhout* | *Eigenaar*
*Bel mij +31 6 22 22 22 22* <+31622222222>
[image: logo webridge]
Beatrixlaan 2, 4694EG Scherpenisse
*KVK 50000000*
*BTW NL0000000AA0*
*IBAN NL93 BUNQ 0000 1111 22*
*WEB webRidge.nl <https://webridge.nl/>*
`
func TestMissingQuotedReply(t *testing.T) {
content := Parse(quotedReplyMissingContent)
expected := "Some content"
if content != expected {
t.Errorf("expected: `%v` but is `%v`", expected, content)
}
}
const quotedReplyMissingContent = `
Some content
On Monday, November 4, 2013 4:29 PM, John Smith <john.smith@example.org> wrote:`
func TestAbhishekSignature(t *testing.T) {
content := Parse(abishhekMail)
expected := "Hi"
if content != expected {
t.Errorf("expected: `%v` but is `%v`", expected, content)
}
}
const abishhekMail = `
Hi
-Abhishek Kona
_______________________________________________
riak-users mailing list
riak-users@lists.basho.com
http://lists.basho.com/mailman/listinfo/riak-users_lists.basho.com
On Mon, Aug 26, 2019 at 4:37 PM The Hiring Engine <
a-really-long-automated-email+1234556@humanresources.com> wrote:
> dd
> sldfj
> slfjlsdf
> slkfjlksfj
`
func TestAllKindOfCombinations(t *testing.T) {
var howManyCombinations int
var howManySuccess int
mailErr := filepath.Walk("./dataset/basemail/",
func(mailPath string, mailFile os.FileInfo, err error) error {
if err != nil {
return err
}
if mailFile.IsDir() {
return nil
}
fmt.Println(mailPath)
//
mailContent, err := ioutil.ReadFile(filepath.Join(mailPath))
if err != nil {
return err
}
signatureErr := filepath.Walk("./dataset/signatures/",
func(signaturePath string, signatureFile os.FileInfo, err error) error {
if err != nil {
return err
}
if signatureFile.IsDir() {
return nil
}
signatureContent, err := ioutil.ReadFile(signaturePath)
if err != nil {
return err
}
quotedReplyErr := filepath.Walk("./dataset/quoted_reply/",
func(quotedReplyPath string, quotedReplyFile os.FileInfo, err error) error {
if err != nil {
return err
}
if quotedReplyFile.IsDir() {
return nil
}
howManyCombinations += 2
quotedReplyContent, err := ioutil.ReadFile(quotedReplyPath)
if err != nil {
return err
}
expectedContent := string(mailContent)
parsedQuotedReplyBottom := Parse(
expectedContent + "\n\n" +
string(signatureContent) + "\n\n" +
string(quotedReplyContent),
)
logLine := `mail: %v, reply: %v, signature: %v: expected
"%v"
but got
"%v"
`
if parsedQuotedReplyBottom != removeWhiteSpaceBeforeAndAfter(expectedContent) {
t.Errorf(
logLine,
mailFile.Name(),
quotedReplyFile.Name(),
signatureFile.Name(),
expectedContent,
parsedQuotedReplyBottom,
)
} else {
howManySuccess++
}
parsedQuotedReplyTop := Parse(
string(quotedReplyContent) + "\n\n" +
expectedContent + "\n\n" +
string(signatureContent) + "\n\n",
)
if parsedQuotedReplyTop != removeWhiteSpaceBeforeAndAfter(expectedContent) {
t.Errorf(
logLine,
mailFile.Name(),
quotedReplyFile.Name(),
signatureFile.Name(),
expectedContent,
parsedQuotedReplyTop,
)
} else {
howManySuccess++
}
return nil
})
if quotedReplyErr != nil {
return quotedReplyErr
}
return nil
})
if signatureErr != nil {
return signatureErr
}
return err
})
if mailErr != nil {
t.Error(mailErr)
}
t.Logf("%v/%v were successfully parsed", howManySuccess, howManyCombinations)
}