-
Notifications
You must be signed in to change notification settings - Fork 16
/
generate_cache.py
151 lines (129 loc) · 6.2 KB
/
generate_cache.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
import os
import pickle
import argparse
import hashlib
import pandas as pd
from tqdm import tqdm
from pprint import pprint
import gensim.downloader as api
from gensim.models import KeyedVectors
###
# Parsing Arguments
###
parser = argparse.ArgumentParser(description='Zero-Shot Topic Extraction')
parser.add_argument("-cnp", "--conceptnet_assertions_path", type=str, help="Path to CSV file containing ConceptNet assertions dump", default='conceptnet-assertions-5.7.0.csv')
parser.add_argument("-nbp", "--conceptnet_numberbatch_path", type=str, help="Path to W2V file for ConceptNet Numberbatch", default='numberbatch-en-19.08.txt')
parser.add_argument("-zcp", "--zeste_cache_path", type=str, help="Path to the repository where the generated files will be saved", default='zeste_cache/')
args = parser.parse_args()
###
# Loading & Preprocessing Data
###
# wget https://s3.amazonaws.com/conceptnet/downloads/2019/edges/conceptnet-assertions-5.7.0.csv.gz
# gzip -d conceptnet-assertions-5.7.0.csv.gz
# wc -l conceptnet-assertions-5.7.0.csv
# wget https://conceptnet.s3.amazonaws.com/downloads/2019/numberbatch/numberbatch-19.08.txt.gz
if not os.path.exists(args.zeste_cache_path):
print('Caching folder (', args.zeste_cache_path,') not found.. creating it now.')
os.makedirs(args.zeste_cache_path, exist_ok=True)
data = []
print('Reading ConceptNet assertions..')
with open(args.conceptnet_assertions_path, 'r') as f:
for line in f:
triplet, rel, sub, obj, info = line.split('\t')
data.append((sub, rel, obj))
# if len(data) == 30000: break
cn = pd.DataFrame(data=data, columns=['subject', 'relation', 'object'])
# cn.to_csv('conceptnet_5.7.0.csv')
print('Loading ConceptNet assertions..')
data_en = []
for i, triplet in tqdm(cn.iterrows(), total=len(cn)):
lang = triplet.subject.split('/')[2]
if lang == 'en':
sub = triplet.subject.split('/')[3]
obj = triplet.object.split('/')[3]
rel = '/'.join([w.lower() for w in triplet.relation.split('/')[2:]])
data_en.append((sub, rel, obj))
# cn_en = pd.DataFrame(data=data_en, columns=['subject', 'relation', 'object'])
print('Loading Numberbatch embeddings (may take some time)..')
numberbatch = KeyedVectors.load_word2vec_format(args.conceptnet_numberbatch_path)
numberbatch_cache_path = os.path.join(args.zeste_cache_path, args.conceptnet_numberbatch_path.split('/')[-1].replace('.txt', '') +'.pickle')
pickle.dump(numberbatch, open(numberbatch_cache_path, 'wb'))
print('Saving the pickled Numberbatch into', numberbatch_cache_path)
reverse_rels = { 'antonym': 'antonym',
'atlocation': 'locatedat',
'capableof': 'doableby',
'causes': 'iscausedby',
'causesdesire': 'desires',
'createdby': 'created',
'definedas': 'isdefinionof',
'derivedfrom': 'derives',
'desires': 'causesdesire',
'distinctfrom': 'distinctfrom',
'entails': 'requires',
'etymologicallyderivedfrom': 'etymologicallyderiving',
'etymologicallyrelatedto': 'etymologicallyrelatedto',
'formof': 'originalformof',
'hasa': 'ispartof',
'hascontext': 'incontextof',
'hasfirstsubevent': 'isfirstsubevent',
'haslastsubevent': 'islastsubeventof',
'hasprerequisite': 'isprequisite',
'hasproperty': 'ispropertyof',
'hassubevent': 'issubeventfor',
'instanceof': 'type',
'isa': 'isa',
'locatednear': 'locatednear',
'madeof': 'ismatterof',
'mannerof': 'ofmanner',
'motivatedbygoal': 'motivates',
'notcapableof': 'isimpossiblefor',
'notdesires': 'notdesiredby',
'nothasproperty': 'notpropertyof',
'partof': 'hasa',
'receivesaction': 'actson',
'relatedto': 'relatedto',
'similarto': 'similarto',
'symbolof': 'symbolizedby',
'synonym': 'synonym',
'usedfor': 'uses',
'dbpedia/capital': 'dbpedia/capital',
'dbpedia/field': 'dbpedia/field',
'dbpedia/genre': 'dbpedia/genre',
'dbpedia/genus': 'dbpedia/genus',
'dbpedia/influencedby': 'dbpedia/influencedby',
'dbpedia/knownfor': 'dbpedia/knownfor',
'dbpedia/language': 'dbpedia/language',
'dbpedia/leader': 'dbpedia/leader',
'dbpedia/occupation': 'dbpedia/occupation',
'dbpedia/product': 'dbpedia/product'}
data_rev = set()
print('Adding reverse relations to the graph if absent..')
for s, r, o in tqdm(data_en):
if r == 'externalurl':
continue
data_rev.add((s, r, o))
data_rev.add((o, reverse_rels[r], s))
cn_en_all = pd.DataFrame(data=sorted(data_rev, key=lambda x: x[0]), columns=['subject', 'relation', 'object'])
current = '0'
neighbors = {current: {'rels':['sameas'], 'sim': 1., 'from': [current]}}
for i, e in tqdm(cn_en_all.iterrows(), total=len(cn_en_all)):
s, r, o = e['subject'], e['relation'], e['object']
assert(type(s) == str and type(s) == str)
if s != current:
try:
filename = current + '.pickle'
filepath = os.path.join(args.zeste_cache_path, hashlib.md5(filename.encode('utf-8')).hexdigest()[:2])
os.makedirs(filepath, exist_ok=True)
pickle.dump(neighbors, open(os.path.join(filepath, filename), 'wb'))
except Exception as e:
print(f'Exception at word "{current}":', str(e))
# print(current, "'s neighborhood:")
# pprint(neighbors)
current = s
neighbors = {current: {'rels':['sameas'], 'sim': 1., 'from': [current]}}
if o not in neighbors: # adding a new neighbor to the neighborhood
neighbors[o] = {'rels':[r], 'sim': 0., 'from': [s]}
if s in numberbatch and o in numberbatch:
neighbors[o]['sim'] = numberbatch.similarity(s, o)
else: # already encountered this neighnor from a previous relation
neighbors[o]['rels'].append(r)