crbpfx commited on
Commit
b9ed663
·
1 Parent(s): cc6d95f

Upload benchmark.ipynb

Browse files
Files changed (1) hide show
  1. benchmark.ipynb +364 -0
benchmark.ipynb ADDED
@@ -0,0 +1,364 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ {
2
+ "cells": [
3
+ {
4
+ "cell_type": "code",
5
+ "execution_count": null,
6
+ "id": "e30bf074",
7
+ "metadata": {
8
+ "ExecuteTime": {
9
+ "end_time": "2023-06-14T17:16:17.863326Z",
10
+ "start_time": "2023-06-14T17:16:17.806871Z"
11
+ }
12
+ },
13
+ "outputs": [],
14
+ "source": [
15
+ "import json\n",
16
+ "import jellyfish\n",
17
+ "import nltk\n",
18
+ "import statistics\n",
19
+ "from collections import Counter\n",
20
+ "from tqdm import tqdm"
21
+ ]
22
+ },
23
+ {
24
+ "cell_type": "markdown",
25
+ "id": "711902a2",
26
+ "metadata": {},
27
+ "source": [
28
+ "# Load KWX"
29
+ ]
30
+ },
31
+ {
32
+ "cell_type": "code",
33
+ "execution_count": null,
34
+ "id": "2bba73c8",
35
+ "metadata": {
36
+ "ExecuteTime": {
37
+ "end_time": "2023-06-14T17:12:46.170356Z",
38
+ "start_time": "2023-06-14T17:12:44.665181Z"
39
+ }
40
+ },
41
+ "outputs": [],
42
+ "source": [
43
+ "kwx = json.load(open('./data.json','r'))\n",
44
+ "train_size = 16000"
45
+ ]
46
+ },
47
+ {
48
+ "cell_type": "markdown",
49
+ "id": "ed39b665",
50
+ "metadata": {},
51
+ "source": [
52
+ "# Metrics"
53
+ ]
54
+ },
55
+ {
56
+ "cell_type": "code",
57
+ "execution_count": null,
58
+ "id": "b7df88f3",
59
+ "metadata": {
60
+ "ExecuteTime": {
61
+ "end_time": "2023-06-14T17:19:12.956834Z",
62
+ "start_time": "2023-06-14T17:19:12.949734Z"
63
+ }
64
+ },
65
+ "outputs": [],
66
+ "source": [
67
+ "def compare_two_words(w1,w2):\n",
68
+ " return jellyfish.jaro_distance(w1, w2)\n",
69
+ "\n",
70
+ "def is_related_in_list(word,ys):\n",
71
+ " for y in ys:\n",
72
+ " if compare_two_words(word.lower(),y.lower()) >= 0.9:\n",
73
+ " return True\n",
74
+ " return False \n",
75
+ "\n",
76
+ "def kws_precision(ys,ys_true):\n",
77
+ " if len(ys) >= 1:\n",
78
+ " count = 0 \n",
79
+ " for y in ys:\n",
80
+ " if is_related_in_list(y,ys_true):\n",
81
+ " count += 1\n",
82
+ " return count/len(ys)\n",
83
+ " else:\n",
84
+ " return 0\n",
85
+ "\n",
86
+ "def kws_recall(ys,ys_true):\n",
87
+ " if not ys_true:\n",
88
+ " return 0\n",
89
+ " if len(ys) >= 1:\n",
90
+ " count = 0 \n",
91
+ " for y in ys:\n",
92
+ " if is_related_in_list(y,ys_true):\n",
93
+ " count += 1\n",
94
+ " if count > len(ys_true):\n",
95
+ " return 1\n",
96
+ " return count/len(ys_true)\n",
97
+ " else:\n",
98
+ " return 0 \n",
99
+ " \n",
100
+ "def evaluate_kws(ys,ys_true):\n",
101
+ " res = {}\n",
102
+ " res['precision'] = kws_precision(ys,ys_true)\n",
103
+ " res['recall'] = kws_recall(ys,ys_true)\n",
104
+ " if res['precision'] or res['recall']:\n",
105
+ " res['f1'] = (2*res['precision']*res['recall'])/(res['precision']+res['recall'])\n",
106
+ " else:\n",
107
+ " res['f1'] = 0\n",
108
+ " return res \n",
109
+ "\n",
110
+ "\n",
111
+ "def macro_pr(yt_pairs):\n",
112
+ " f1 = []\n",
113
+ " p = []\n",
114
+ " r = []\n",
115
+ " for pair in yt_pairs:\n",
116
+ " evaluate = evaluate_kws(pair[0],pair[1])\n",
117
+ " f1.append(evaluate['f1'])\n",
118
+ " p.append(evaluate['precision'])\n",
119
+ " r.append(evaluate['recall'])\n",
120
+ " return {'f1':statistics.mean(f1),\n",
121
+ " 'precision':statistics.mean(p),\n",
122
+ " 'recall':statistics.mean(r)}\n",
123
+ "\n",
124
+ "def evaluate_extractor(docs_with_kws, extractor, top=None):\n",
125
+ " yt_pairs = []\n",
126
+ " for i in tqdm(list(docs_with_kws.keys())[train_size:]):\n",
127
+ " pair = []\n",
128
+ " pair.append(extractor(docs_with_kws[i]['abstract'],top))\n",
129
+ " ys_true = docs_with_kws[i]['keywords']\n",
130
+ " if 'keywords_extra' in docs_with_kws[i]:\n",
131
+ " ys_true += docs_with_kws[i]['keywords_extra']\n",
132
+ " pair.append(ys_true)\n",
133
+ " yt_pairs.append(pair)\n",
134
+ " print(len(yt_pairs))\n",
135
+ " print(macro_pr(yt_pairs))"
136
+ ]
137
+ },
138
+ {
139
+ "cell_type": "markdown",
140
+ "id": "949cd440",
141
+ "metadata": {},
142
+ "source": [
143
+ "# Keyword Extraction"
144
+ ]
145
+ },
146
+ {
147
+ "cell_type": "code",
148
+ "execution_count": null,
149
+ "id": "30cd3512",
150
+ "metadata": {
151
+ "ExecuteTime": {
152
+ "end_time": "2023-06-14T17:12:51.826420Z",
153
+ "start_time": "2023-06-14T17:12:51.821688Z"
154
+ }
155
+ },
156
+ "outputs": [],
157
+ "source": [
158
+ "kw_start = [\n",
159
+ " 'NN',\n",
160
+ " 'NNP',\n",
161
+ " 'JJ',\n",
162
+ " 'NNS',\n",
163
+ " 'VBG',\n",
164
+ " 'RB',\n",
165
+ " 'VBN'\n",
166
+ "]\n",
167
+ "\n",
168
+ "kw_end = [\n",
169
+ " 'NN',\n",
170
+ " 'NNP',\n",
171
+ " 'NNS',\n",
172
+ " 'VBG',\n",
173
+ " 'JJ'\n",
174
+ "]\n",
175
+ "\n",
176
+ "kw_split = [\n",
177
+ " '.',\n",
178
+ " ','\n",
179
+ "]\n",
180
+ "def keywords_selector(text):\n",
181
+ " tokens = nltk.word_tokenize(text)\n",
182
+ " tags = nltk.pos_tag(tokens)\n",
183
+ " res = set()\n",
184
+ " for i in range(len(tags)):\n",
185
+ " if tags[i][1] in kw_start:\n",
186
+ " end = i+4 if i+4 <= len(tags) - 1 else len(tags) - 1\n",
187
+ " cut = tags[i:end]\n",
188
+ " for j in range(len(cut)):\n",
189
+ " if cut[j][0] in kw_split:\n",
190
+ " cut = cut[:j]\n",
191
+ " break \n",
192
+ " for k in range(len(cut)):\n",
193
+ " if cut[k][1] in kw_end:\n",
194
+ " res.add(' '.join([m[0] for m in cut][:k+1]))\n",
195
+ " return res \n",
196
+ " "
197
+ ]
198
+ },
199
+ {
200
+ "cell_type": "code",
201
+ "execution_count": null,
202
+ "id": "00bf7e61",
203
+ "metadata": {
204
+ "ExecuteTime": {
205
+ "end_time": "2023-06-14T17:14:54.344161Z",
206
+ "start_time": "2023-06-14T17:13:30.555540Z"
207
+ }
208
+ },
209
+ "outputs": [],
210
+ "source": [
211
+ "# scoreboard to record all combinations.\n",
212
+ "history_score = {}\n",
213
+ "# global dictionary to record all links between present and absent keywords\n",
214
+ "global_kwx_index = {}\n",
215
+ "for i in tqdm(list(kwx.keys())[:train_size]):\n",
216
+ " ys = kwx[i]['keywords']\n",
217
+ " candidates = keywords_selector(kwx[i]['abstract'])\n",
218
+ " for c in candidates:\n",
219
+ " if not c in history_score:\n",
220
+ " history_score[c] = 0\n",
221
+ " if is_related_in_list(c,ys):\n",
222
+ " history_score[c] += 100\n",
223
+ " else:\n",
224
+ " history_score[c] -= 1\n",
225
+ " for y in ys:\n",
226
+ " if y in global_kwx_index:\n",
227
+ " global_kwx_index[y].update(ys)\n",
228
+ " else:\n",
229
+ " global_kwx_index[y] = Counter(ys)\n",
230
+ "print(len(history_score))"
231
+ ]
232
+ },
233
+ {
234
+ "cell_type": "code",
235
+ "execution_count": null,
236
+ "id": "cf0bc909",
237
+ "metadata": {
238
+ "ExecuteTime": {
239
+ "end_time": "2023-06-14T17:20:03.965152Z",
240
+ "start_time": "2023-06-14T17:20:03.960665Z"
241
+ }
242
+ },
243
+ "outputs": [],
244
+ "source": [
245
+ "def kwx_kws(text,top=None):\n",
246
+ " res = []\n",
247
+ " score = []\n",
248
+ " candidates = keywords_selector(text)\n",
249
+ " for c in candidates:\n",
250
+ " if c not in history_score:\n",
251
+ " res.append(c)\n",
252
+ " score.append(1)\n",
253
+ " elif history_score[c]>0:\n",
254
+ " res.append(c)\n",
255
+ " score.append(history_score[c])\n",
256
+ " if c in global_kwx_index:\n",
257
+ " for r in global_kwx_index[c]:\n",
258
+ " if r not in candidates:\n",
259
+ " res.append(r)\n",
260
+ " score.append(global_kwx_index[c][r])\n",
261
+ " if not res:\n",
262
+ " res = ['None']\n",
263
+ " sorted_res = [x for _, x in sorted(zip(score, res))][::-1]\n",
264
+ " if top and len(sorted_res)>top:\n",
265
+ " return sorted_res[:top]\n",
266
+ " else:\n",
267
+ " return sorted_res"
268
+ ]
269
+ },
270
+ {
271
+ "cell_type": "code",
272
+ "execution_count": null,
273
+ "id": "86f377c6",
274
+ "metadata": {
275
+ "ExecuteTime": {
276
+ "end_time": "2023-06-14T17:17:18.438467Z",
277
+ "start_time": "2023-06-14T17:16:21.507730Z"
278
+ }
279
+ },
280
+ "outputs": [],
281
+ "source": [
282
+ "evaluate_extractor(kwx, kwx_kws)"
283
+ ]
284
+ },
285
+ {
286
+ "cell_type": "code",
287
+ "execution_count": null,
288
+ "id": "cdfec2e8",
289
+ "metadata": {
290
+ "ExecuteTime": {
291
+ "end_time": "2023-06-14T17:20:24.682626Z",
292
+ "start_time": "2023-06-14T17:20:06.517802Z"
293
+ }
294
+ },
295
+ "outputs": [],
296
+ "source": [
297
+ "evaluate_extractor(kwx, kwx_kws, 10)"
298
+ ]
299
+ }
300
+ ],
301
+ "metadata": {
302
+ "kernelspec": {
303
+ "display_name": "Python 3 (ipykernel)",
304
+ "language": "python",
305
+ "name": "python3"
306
+ },
307
+ "language_info": {
308
+ "codemirror_mode": {
309
+ "name": "ipython",
310
+ "version": 3
311
+ },
312
+ "file_extension": ".py",
313
+ "mimetype": "text/x-python",
314
+ "name": "python",
315
+ "nbconvert_exporter": "python",
316
+ "pygments_lexer": "ipython3",
317
+ "version": "3.9.12"
318
+ },
319
+ "toc": {
320
+ "base_numbering": 1,
321
+ "nav_menu": {},
322
+ "number_sections": true,
323
+ "sideBar": true,
324
+ "skip_h1_title": false,
325
+ "title_cell": "Table of Contents",
326
+ "title_sidebar": "Contents",
327
+ "toc_cell": false,
328
+ "toc_position": {},
329
+ "toc_section_display": true,
330
+ "toc_window_display": false
331
+ },
332
+ "varInspector": {
333
+ "cols": {
334
+ "lenName": 16,
335
+ "lenType": 16,
336
+ "lenVar": 40
337
+ },
338
+ "kernels_config": {
339
+ "python": {
340
+ "delete_cmd_postfix": "",
341
+ "delete_cmd_prefix": "del ",
342
+ "library": "var_list.py",
343
+ "varRefreshCmd": "print(var_dic_list())"
344
+ },
345
+ "r": {
346
+ "delete_cmd_postfix": ") ",
347
+ "delete_cmd_prefix": "rm(",
348
+ "library": "var_list.r",
349
+ "varRefreshCmd": "cat(var_dic_list()) "
350
+ }
351
+ },
352
+ "types_to_exclude": [
353
+ "module",
354
+ "function",
355
+ "builtin_function_or_method",
356
+ "instance",
357
+ "_Feature"
358
+ ],
359
+ "window_display": false
360
+ }
361
+ },
362
+ "nbformat": 4,
363
+ "nbformat_minor": 5
364
+ }