-
Notifications
You must be signed in to change notification settings - Fork 0
/
maildedup2.py
executable file
·153 lines (132 loc) · 4.12 KB
/
maildedup2.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
#! /usr/bin/python3
import io
import os
import sys
import pickle
import traceback
from eml2token import eml2str,tokenize,eprint
try:
wordmap=pickle.load(open("model/model.wordmap-py3", "rb"))
except:
wordmap=pickle.load(open("model/model.wordmap-py2", "rb"))
###########################################################################################################
################################################### DEDUP #################################################
###########################################################################################################
wmem=bytearray(2*65536*65536) # 8GB ram!!!
maxhash=8*len(wmem)-1
# hl=mozgo ablak merete amit hashel. nn=maximum egyezesek szama
def dedup(tokens,hl,nn):
# fuzzy search
ok=0
n=len(tokens)-(hl-1)
for i in range(n):
w=" ".join(tokens[i:i+hl])
wh=hash(w)
wh^=(wh>>40)
wh&=maxhash
# print(wh)
# try:
if wmem[wh>>3] & (1<<(wh&7)):
ok+=1
if ok>nn:
return 0
# break
# except:
# print(type(wh))
if ok<=nn:
# if ok:
# o=" ".join(tokens)
# print(o)
for i in range(n):
w=" ".join(tokens[i:i+hl])
#wh=hash(w) & maxhash
wh=hash(w)
wh^=(wh>>40)
wh&=maxhash
wmem[wh>>3]|=(1<<(wh&7))
return 1
# print(o.encode("utf-8"))
# print str(label)+" "+" ".join(tokens)
return 0
def dedup1(tokens):
w=" ".join(tokens)
wh=hash(w)
wh^=(wh>>40)
wh&=maxhash
# print(wh)
# try:
if wmem[wh>>3] & (1<<(wh&7)):
return 0
wmem[wh>>3]|=(1<<(wh&7))
return 1
#################################################################################################################################################################
def do_eml(eml,out_txt):
# print("Parsing email (%d bytes)"%(len(eml)))
vtokens=[]
tokens=[]
for text in eml2str(eml):
# print(text.encode("utf-8"))
if text.find('pam detection software, running on the system')>=0:
continue
t=" ".join(text.replace('"',' ').split())
# print(t.encode("utf-8"))
if(len(t)>10):
# print(str(label)+" "+t.encode("utf-8"))
# print(t.encode("utf-8"))
try:
vtok,tok=tokenize(t,wordmap)
if len(vtok)>len(vtokens):
vtokens=vtok
tokens=tok
except:
eprint(traceback.format_exc())
# print("NUM of tokens: %d / %d"%(len(vtokens),len(tokens)))
# print("%d / %d"%(bestnn,len(tokens)))
if len(vtokens)<10:
return 0
# ok=dedup(vtokens,5,(len(vtokens)-10)/3)
ok=dedup(vtokens,7,(len(vtokens)-10)*4/5)
# ok=dedup1(vtokens)
# print(ok)
if ok:
# print(" ".join(tokens)+"\n")
out_txt.write(" ".join(tokens)+"\n")
return ok
########## MAIN ##############
#for line in open("vocab.txt"):
# w,c=line.strip().split(" ")
# vocab[w]=int(c)
#input_stream = io.TextIOWrapper(sys.stdin.buffer, encoding='iso-8859-2',errors='ignore')
#input_stream = io.TextIOWrapper(sys.stdin.buffer, encoding='utf-8',errors='ignore')
#output_stream= open("maildedup.mbox","wt",encoding="utf-8",errors='ignore'):
#input_stream = sys.stdin
#output_stream = sys.stdout
#output_txt = open("maildedup.txt","wt",encoding="utf-8",errors='ignore')
input_stream= open(sys.argv[1],"rb")
output_stream= open(sys.argv[1]+".out","wb")
output_txt = open(sys.argv[1]+".txt","w")
in_hdr=0
eml=None
for line in input_stream:
if in_hdr:
# eml+=line
if len(line.rstrip())==0:
in_hdr=0
elif line[0:5]==b'From ':
# print(line)
if eml:
res=do_eml(eml,output_txt)
if res:
output_stream.write(eml)
eml=None
in_hdr=1
if eml:
eml+=line
else:
eml=line
if eml:
res=do_eml(eml,output_txt)
if res:
output_stream.write(eml)
output_stream.close()
output_txt.close()