-
Notifications
You must be signed in to change notification settings - Fork 0
/
data_processing.py
191 lines (173 loc) · 7.15 KB
/
data_processing.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
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
import json
from collections import defaultdict
def el_append_properties(el_file, dbpedia_file, output_file):
"""Append entity properties in DBpedia.
Keep the following relations:
- purpose
- office
- background
- meaning
- orderInOffice
- seniority
- title
- role
:param el_file: Path to the TagMe result file.
:param dbpedia_file: Path to DBpedia's mappingbased_literals_en.ttl file.
:param output_file: Path to the output file.
"""
import re
titles = set()
relations = {'purpose', 'office', 'background', 'meaning', 'orderInOffice',
'seniority', 'title', 'role'}
with open(el_file, 'r', encoding='utf-8') as r:
for line in r:
tid, result = line.rstrip().split('\t')
result = json.loads(result)
for anno in result['annotations']:
if 'title' in anno:
titles.add(anno['title'])
print('#title:', len(titles))
title_properties = defaultdict(list)
with open(dbpedia_file, 'r', encoding='utf-8') as r:
for line in r:
if line.startswith('#'):
continue
segs = line[:-2].split(' ', 2)
if not segs[1].startswith('<http://dbpedia.org/ontology/'):
continue
title = segs[0][29:-1].replace('_', ' ')
relation = segs[1][29:-1]
if title in titles and relation in relations:
property = segs[2][segs[2].find('"') + 1:segs[2].rfind('"')]
property = re.sub(r'(\d+th)', r'\1 ', property)
property = re.sub(r'(\d+nd)', r'\1 ', property)
property = re.sub(r'(\d+st)', r'\1 ', property)
property = re.sub(r'([a-z])([A-Z])', r'\1 \2', property)
property = re.sub(r'\s+', r' ', property)
title_properties[title].append(property)
with open(el_file, 'r', encoding='utf-8') as r, \
open(output_file, 'w', encoding='utf-8') as w:
for line in r:
tid, result = line.rstrip().split('\t')
result = json.loads(result)
annotations = []
for anno in result['annotations']:
if 'title' in anno:
title = anno['title']
if title in title_properties:
props = '. '.join(title_properties[title])
anno['abstract'] += ' ' + props
annotations.append(anno)
result['annotations'] = annotations
w.write('{}\t{}\n'.format(tid, json.dumps(result)))
def el_clean_by_type(el_file, output_file, dbpedia_file, type_file):
"""Clean the TagMe result by type.
:param el_file: Path to the TagMe result file.
:param output_file: Path to the output file.
:param dbpedia_file: Path to DBpedia's instance_types_en.ttl file.
:param type_file: Path to a file containing types to keep. Each line a type.
"""
# Load involved entities
seen_titles = set()
with open(el_file, 'r', encoding='utf-8') as r:
for line in r:
tid, result = line.rstrip().split('\t')
result = json.loads(result)
annotations = result['annotations']
for anno in annotations:
if 'title' in anno:
title = anno['title'].replace(',', '')
seen_titles.add(title)
# Load categories
title_type = {}
with open(dbpedia_file, 'r', encoding='utf-8') as r:
for line in r:
if line.startswith('#'):
continue
segs = line.rstrip().split(' ')
if len(segs) != 4:
continue
title = segs[0][1:-1]
if not title.startswith('http://dbpedia.org/resource/'):
continue
title = title[28:].replace('_', ' ')
if title in seen_titles:
entity_type = segs[2]
if not entity_type.startswith('<http://dbpedia.org/ontology/'):
continue
title_type[title] = entity_type[29:-1]
print(len(seen_titles))
print(len(title_type))
# Load removed categories
kept = set()
with open(type_file, 'r', encoding='utf-8') as r:
for line in r:
kept.add(line.split('\t')[0])
print(kept)
with open(el_file, 'r', encoding='utf-8') as r, \
open(output_file, 'w', encoding='utf-8') as w:
for line in r:
tid, result = line.rstrip().split('\t')
result = json.loads(result)
annotations = result['annotations']
filtered_annotations = []
for anno in annotations:
if 'title' in anno:
title = anno['title'].replace(',', '')
if title in title_type:
category = title_type[title]
if category in kept:
filtered_annotations.append(anno)
else:
print('removed', category)
else:
pass
result['annotations'] = filtered_annotations
w.write('{}\t{}\n'.format(tid, json.dumps(result)))
def el_clean_by_pos(tweet_file, el_file, output_file, skip_first=False):
"""Clean the entity linking result by POS.
Keep: NN, NNS, NNPS, NNP, JJ
:param tweet_file: Path to the TSV format tweet data set.
:param el_file: Path to the TagMe result file.
:param output_file: Path to the output file.
:param skip_first: Skip the first line of <tweet_file> (default=false).
"""
from nltk import pos_tag, TweetTokenizer
import nltk
nltk.download('averaged_perceptron_tagger')
tokenizer = TweetTokenizer()
keep_set = {'NN', 'NNS', 'NNPS', 'NNP', 'JJ'}
# Load tweets
tid_tokens = {}
tag_set = set()
with open(tweet_file, 'r', encoding='utf-8') as r:
if skip_first:
next(r)
for line in r:
tid, tweet, _ = line.rstrip().split('\t')
tokens = tokenizer.tokenize(tweet)
tokens = pos_tag(tokens)
tid_tokens[tid] = tokens
for t, p in tokens:
tag_set.add(p)
with open(el_file, 'r', encoding='utf-8') as r, \
open(output_file, 'w', encoding='utf-8') as w:
for line in r:
tid, result = line.rstrip().split('\t')
result = json.loads(result)
tokens = tid_tokens[tid]
keep_tokens = set()
for token, pos in tokens:
if pos in keep_set:
keep_tokens.add(token.lower().replace('#', ''))
filtered_annotations = []
if 'annotations' in result:
for anno in result['annotations']:
if 'title' in anno and 'spot' in anno:
spot = anno['spot']
if len(spot) == 1:
continue
if ' ' in spot or spot.lower() in keep_tokens:
filtered_annotations.append(anno)
result['annotations'] = filtered_annotations
w.write('{}\t{}\n'.format(tid, json.dumps(result)))