-
Notifications
You must be signed in to change notification settings - Fork 0
/
anonymize-eng.py
executable file
·146 lines (130 loc) · 4.35 KB
/
anonymize-eng.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
#!/usr/bin/python3 -W all
# anonymize-eng.py: remove personal information from English text file
# usage: anonymize-eng.py < file.ner
# note: expects line input in ner format: word SPACE pos SPACE ner-tag
# 20181016 erikt(at)xs4all.nl
import random
import re
import sys
COMMAND = sys.argv.pop(0)
NAMESDIR = "/home/erikt/projects/e-mental-health/usb/tmp"
NAMEFILE = NAMESDIR+"/names-eng.txt"
NEOTHER = "O"
PER = "PERSON"
LOC = "LOCATION"
ORG = "ORGANIZATION"
NUM = "NUM"
DAY = "DAY"
DATE = "DATE"
MONTH = "MONTH"
MAIL = "MAIL"
NETAGS = {PER,LOC,ORG,NUM,DAY,DATE,MONTH,MAIL}
DATETAGS = {NUM,DAY,DATE,MONTH}
TAGNUM = "CD"
DOMAINS = "(com|net|nl|org)"
MONTHS = [ "january","february","march","april","may","june","july", \
"august","september","october","november","december", \
"January","February","March","April","May","June","July", \
"August","September","October","November","December" ]
WEEKDAYS = [ "sunday","monday","tuesday","wednesday","thursday", \
"friday","saturday", \
"Sunday","Monday","Tuesday","Wednesday","Thursday", \
"Friday","Saturday", \
"Sundays","Mondays","Tuesdays","Wednesdays","Thursdays", \
"Fridays","Saturdays",
"Sun","Mon","Tue","Wed","Thu","Fri","Sat","Sun" ]
def addName(name,myClass):
global names
global newNames
names[name] = myClass
newNames[name] = myClass
return()
def storeNewNames():
global newNames
if len(newNames) > 0:
outFile = open(NAMEFILE,"a")
keys = list(newNames.keys())
random.shuffle(keys)
for key in keys:
print(key,newNames[key],file=outFile)
outFile.close()
return()
def compressNE(tokens):
i = 0
while i < len(tokens):
if tokens[i] in NETAGS:
while i < len(tokens)-1 and tokens[i+1] == tokens[i]:
tokens = tokens[:i]+tokens[i+1:]
if tokens[i] in DATETAGS:
while i < len(tokens) and tokens[i+1] in DATETAGS:
tokens = tokens[:i-1]+[DATE]+tokens[i+2:]
i += 1
return(tokens)
def anonymize(tokens,pos,ner):
global names
for i in range(0,len(tokens)):
if tokens[i] in names.keys():
if names[tokens[i]] != NEOTHER:
tokens[i] = names[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"\."+DOMAINS+"$",tokens[i],re.IGNORECASE):
tokens[i] = ORG
elif ner[i] != NEOTHER:
addName(tokens[i],ner[i])
tokens[i] = ner[i]
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 = compressNE(tokens)
line = " ".join(tokens)
return(line)
def readKnownNames():
names = {}
try:
inFile = open(NAMEFILE,"r")
for line in inFile:
line = line.rstrip()
try: token,ner = line.split()
except: sys.exit(COMMAND+": unexpected line in name file: "+line)
names[token] = ner
inFile.close()
except: pass
return(names,{})
def posTag2base(posTag):
return(re.sub(r"\(.*$","",posTag))
def neTag2base(neTag):
return(re.sub(r"^.-","",neTag))
def readSentence():
tokens,pos,ner = [[],[],[]]
for line in sys.stdin:
try:
line = line.rstrip()
token,posTag,neTag = line.split()
posTag = posTag2base(posTag)
neTag = neTag2base(neTag)
tokens.append(token)
pos.append(posTag)
ner.append(neTag)
except Exception as e:
if line != "":
sys.exit(COMMAND+": unexpected line: "+line+" "+str(e))
if len(tokens) > 0:
return(tokens,pos,ner)
return(tokens,pos,ner)
def main(argv):
global names,newNames
names,newNames = readKnownNames()
tokens,pos,ner = readSentence()
while len(tokens) > 0:
print(anonymize(tokens,pos,ner))
tokens,pos,ner = readSentence()
storeNewNames()
if __name__ == "__main__":
sys.exit(main(sys.argv))