-
Notifications
You must be signed in to change notification settings - Fork 0
/
anonymize.py
executable file
·169 lines (153 loc) · 5.78 KB
/
anonymize.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
#!/usr/bin/python3 -W all
# anonymize.py: remove personal information from any text file
# usage: anonymize.py [-i] < nerfile
# note: might miss words with punctuation attached to them
# 20180604 erikt(at)xs4all.nl
import getopt
import re
import sys
COMMAND = sys.argv.pop(0)
NAMESDIR = "/home/erikt/projects/e-mental-health/usb/OVK/data/eriktks/names"
POSITIVENAMEFILE = NAMESDIR+"/positiveNames.txt"
NEGATIVENAMEFILE = NAMESDIR+"/negativeNames.txt"
NEOTHER = "O"
PER = "PER"
STOP = "x"
TAGNUM = "TW"
NAMEWORDS = [ "van","de","der","vande","vander","Van","De","Vande","Vander" ]
SKIP = [ "EFrom","EDate","ETo","Verzonden","Van","Aan","From","To","Date","Sent" ]
MONTHS = [ "januari","februari","maart","april","mei","juni","juli", \
"augustus","september","oktober","november","december", \
"Januari","Februari","Maart","April","Mei","Juni","Juli", \
"Augustus","September","Oktober","November","December" ]
WEEKDAYS = [ "zondag","maandag","dinsdag","woensdag","donderdag", \
"vrijdag","zaterdag", \
"Zondag","Maandag","Dinsdag","Woensdag","Donderdag", \
"Vrijdag","Zaterdag", \
"zondags","maandags","dinsdags","woensdags","donderdags", \
"vrijdags","zaterdags" ]
def check(name,ner):
print("? ("+ner+") "+name+" ",end="")
sys.stdout.flush()
response = sys.stdin.readline().rstrip()
if response == STOP: sys.exit(COMMAND+": stopped")
if response == "1": response = ner
return(response)
def addNegative(name):
global negativeNames
negativeNames[name] = False
outFile = open(NEGATIVENAMEFILE,"a")
print(name,file=outFile)
outFile.close()
return()
def addPositive(name,myClass):
global positiveNames
positiveNames[name] = myClass
outFile = open(POSITIVENAMEFILE,"a")
print(name+" "+myClass,file=outFile)
outFile.close()
return()
def compressPER(tokens):
counter = 0
while counter < len(tokens):
if tokens[counter] != PER: counter += 1
else:
nextCounter = counter
allChecked = False
while not allChecked:
if nextCounter < len(tokens)-1 and \
tokens[nextCounter+1] == PER:
nextCounter += 1
elif nextCounter < len(tokens)-2 and \
tokens[nextCounter+1] in NAMEWORDS and \
tokens[nextCounter+2] == PER:
nextCounter += 2
elif nextCounter < len(tokens)-2 and \
tokens[nextCounter+1] in NAMEWORDS and \
tokens[nextCounter+2] in NAMEWORDS and \
tokens[nextCounter+3] == PER:
nextCounter += 2
else: allChecked = True
tokens = tokens[:counter]+["PER"]+tokens[nextCounter+1:]
counter += 1
return(tokens)
def anonymize(tokens,pos,ner,interactive):
global positiveNames,negativeNames
if len(tokens) > 1 and tokens[0] in SKIP and tokens[1] == ":":
return(" ".join(tokens))
for i in range(0,len(tokens)):
if tokens[i] in positiveNames.keys():
tokens[i] = positiveNames[tokens[i]]
elif pos[i] == TAGNUM or re.search(r"^\d",tokens[i]):
tokens[i] = "NUM"
elif tokens[i] in MONTHS:
tokens[i] = "MONTH"
elif tokens[i] in WEEKDAYS:
tokens[i] = "DAY"
elif re.search(r"@",tokens[i]):
tokens[i] = "MAIL"
elif re.search(r"^www\.",tokens[i],re.IGNORECASE) or \
re.search(r"\.nl$",tokens[i],re.IGNORECASE):
tokens[i] = "ORG"
elif (ner[i] != NEOTHER or pos[i] == "SPEC") and \
not tokens[i] in negativeNames.keys():
if interactive:
checkOutput = check(tokens[i],ner[i])
if not checkOutput: addNegative(tokens[i])
else:
addPositive(tokens[i],checkOutput)
tokens[i] = checkOutput
else:
addPositive(tokens[i],ner[i])
tokens[i] = checkOutput
tokens[i] = re.sub(r"^0\d\d\b","PHONE",tokens[i])
tokens[i] = re.sub(r"\d\d\d\d\d\d*","PHONE",tokens[i])
tokens = compressPER(tokens)
line = " ".join(tokens)
return(line)
def readPositiveNames():
data = {}
inFile = open(POSITIVENAMEFILE,"r")
for line in inFile:
line = line.rstrip()
try: token,ner = line.split()
except: sys.exit(COMMAND+": unexpected line in positive file: "+line)
data[token] = ner
inFile.close()
return(data)
def readNegativeNames():
data = {}
inFile = open(NEGATIVENAMEFILE,"r")
for line in inFile:
line = line.rstrip()
try: [token] = line.split()
except: sys.exit(COMMAND+": unexpected line in negative file: "+line)
data[token] = False
inFile.close()
return(data)
positiveNames = readPositiveNames()
negativeNames = readNegativeNames()
def main(argv):
try: optionList, files = getopt.getopt(argv,"i",[])
except: sys.exit("usage: "+COMMAND+" [-i] < nerfile ")
interactive = "i" in optionList
ner,pos,tokens = [[],[],[]]
for line in sys.stdin:
try:
line = line.rstrip()
token,tag,ne = line.split()
tag = re.sub(r"\(.*$","",tag)
ne = re.sub(r"\(.*$","",ne)
ne = re.sub(r"^.-","",ne)
tokens.append(token)
pos.append(tag)
ner.append(ne)
except:
if line != "": sys.exit(COMMAND+": unexpected line: "+line)
elif len(tokens) > 0:
print(anonymize(tokens,pos,ner,interactive))
tokens,pos,ner = ([],[],[])
if len(tokens) > 0:
print(anonymize(tokens,pos,ner,interactive))
if __name__ == "__main__":
sys.exit(main(sys.argv))