-
Notifications
You must be signed in to change notification settings - Fork 5
/
create_embeddings.py
90 lines (67 loc) · 1.94 KB
/
create_embeddings.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
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
r"""
Copyright 2018, 2019, 2020 Rui Antunes, Sérgio Matos
https://github.com/ruiantunes/biocreative-vi-track-5-chemprot
BioCreative VI - Track 5 (CHEMPROT).
This script creates part-of-speech (POS), and dependency parsing (DEP)
embeddings from the full ChemProt dataset. The `gensim` framework [1]_
is used to create `KeyedVectors` models.
References
----------
.. [1] https://radimrehurek.com/gensim/models/word2vec.html
"""
# built-in modules (sys.builtin_module_names)
from itertools import product
# third-party modules
from gensim.models import Word2Vec
import os
# own modules
from support import DATA
from support import LABEL2INDEX
from support import load_data_from_zips
from utils import create_directory
# input arguments
# training groups
GROUPS = ['training', 'development', 'test_gs']
# vector sizes
SIZES = [20, 50, 100]
# window sizes
WINDOWS = [3, 5, 10]
# iterations
ITERS = [10, 50, 100]
# other contants
# output directory
OUT = 'word2vec'
create_directory(OUT)
ZIPS = [
os.path.join(
DATA,
'chemprot_{}'.format(group),
'support',
'processed_corpus.zip',
) for group in GROUPS
]
# load dataset
dataset = load_data_from_zips(zips=ZIPS, label2index=LABEL2INDEX)
# get sentences (list of lists of tokens)
sentences = {k: [] for k in ('pos', 'dep')}
for features in dataset['data']:
for wrd, pos, dep in features:
sentences['pos'].append(pos)
sentences['dep'].append(dep)
for key, size, window, it in product(sentences.keys(), SIZES, WINDOWS, ITERS):
fp = '{}-size{}-window{}-iter{}.kv'.format(key, size, window, it)
print('Generating KeyedVectors model...\n{}\n'.format(repr(fp)))
w2v = Word2Vec(
sentences=sentences[key],
size=size,
window=window,
min_count=0,
workers=4,
sg=0,
hs=0,
iter=it,
sorted_vocab=1,
)
w2v.wv.save(os.path.join(OUT, fp))