-
Notifications
You must be signed in to change notification settings - Fork 1
/
charity.py
241 lines (192 loc) · 6.5 KB
/
charity.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
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
import csv
import dataclasses
import json
import os
import re
import nltk
import numpy as np
import pandas as pd
import torch
from nltk.tokenize import sent_tokenize
from tqdm import tqdm
from bert import calculate_next_sentence_probability
from bert import calculate_similarities
from bert import embed_sentences
from utils import batch
nltk.download('punkt')
def _get_sentence_embeddings(description, embed_batch_size=10):
embeddings_list = []
sentences = [
re.sub('\[\d+\]', '', sent).strip().lower()
for sent in sent_tokenize(description)
]
for sentence_batch in batch(sentences, embed_batch_size):
embeddings = embed_sentences(sentence_batch)
embeddings_list.append(embeddings)
return torch.cat(embeddings_list)
@dataclasses.dataclass
class Charity:
name: str
description: str
url: str
@dataclasses.dataclass
class CharitySearchResult:
name: str
description: str
url: str
score: float
class CharityIndex:
_CHARITY_FILENAME = 'charities.json'
_EMBED_FILENAME = 'embeddings.npy'
_INDEX_FILENAME = 'index.json'
def __init__(self, charities, embeddings, embeddings_charity_index):
self._charities = charities
self._embeddings = embeddings
self._embeddings_charity_index = embeddings_charity_index
self._embeddings_normalized = (
self._embeddings /
torch.norm(self._embeddings, p=2, dim=0)
)
@classmethod
def build(cls, charities):
embeddings_charity_index = []
embeddings_list = []
with tqdm(total=len(charities)) as progress_bar:
for charity_index, charity in enumerate(charities):
embeddings = _get_sentence_embeddings(
charity.description,
)
embeddings_charity_index += (
[charity_index] * len(embeddings)
)
embeddings_list.append(embeddings)
progress_bar.update(1)
embeddings = torch.cat(embeddings_list)
return cls(
charities,
embeddings,
embeddings_charity_index,
)
@classmethod
def build_from_tsv(cls, tsv_path):
"""expects columns name, description, and url"""
with open(tsv_path, 'r') as tsv_file:
reader = csv.DictReader(tsv_file, dialect='excel-tab')
charities = [
Charity(**row)
for row in reader
]
return cls.build(charities)
@classmethod
def load(cls, path):
charity_path = cls._get_charity_path(path)
embeddings_path = cls._get_embeddings_path(path)
index_path = cls._get_index_path(path)
with open(charity_path, 'r') as charity_file:
charity_data = json.load(charity_file)
charities = [
Charity(**charity_entry)
for charity_entry in charity_data
]
with open(index_path, 'r') as index_file:
embeddings_charity_index = json.load(index_file)
embeddings = torch.tensor(np.load(embeddings_path))
return cls(
charities,
embeddings,
embeddings_charity_index,
)
def save(self, path):
charity_path = self._get_charity_path(path)
embeddings_path = self._get_embeddings_path(path)
index_path = self._get_index_path(path)
with open(charity_path, 'w') as charity_file:
charity_data = [
dataclasses.asdict(charity)
for charity in self._charities
]
json.dump(charity_data, charity_file)
with open(index_path, 'w') as index_file:
json.dump(
self._embeddings_charity_index,
index_file,
)
np.save(embeddings_path, self._embeddings.cpu().numpy())
def search(
self,
query,
top_n=5,
use_top_n_sentences=20,
rank_with_next_sentence_prediction=True,
):
query_embedding = embed_sentences([query]).cpu()
similarities = calculate_similarities(
query_embedding,
self._embeddings,
)
charity_similarities = pd.DataFrame({
'charity': self._embeddings_charity_index,
'similarity': similarities,
})
best_match_charities = (charity_similarities
.sort_values('similarity', ascending=False)
.groupby('charity')
.head(use_top_n_sentences)
.groupby('charity')
.mean()
.sort_values('similarity', ascending=False)
.head(top_n))
best_match_indices = best_match_charities.index.tolist()
matched_charities = [
self._charities[i]
for i in best_match_indices
]
if rank_with_next_sentence_prediction:
descriptions = [
charity.description
for charity in matched_charities
]
probabilities = calculate_next_sentence_probability(
query,
descriptions,
)
rank_indices = torch.argsort(probabilities).numpy()[::-1]
charities = [
matched_charities[i]
for i in rank_indices
]
return [
CharitySearchResult(
name=charity.name,
url=charity.url,
description=charity.description,
score=score,
)
for (charity, score) in zip(
charities,
probabilities.tolist(),
)
]
else:
matched_similarities = best_match_charities['similarity'].tolist()
return [
CharitySearchResult(
name=charity.name,
url=charity.url,
description=charity.description,
score=score,
)
for (charity, score) in zip(
matched_charities,
matched_similarities,
)
]
@classmethod
def _get_charity_path(cls, path):
return os.path.join(path, cls._CHARITY_FILENAME)
@classmethod
def _get_embeddings_path(cls, path):
return os.path.join(path, cls._EMBED_FILENAME)
@classmethod
def _get_index_path(cls, path):
return os.path.join(path, cls._INDEX_FILENAME)