-
Notifications
You must be signed in to change notification settings - Fork 0
/
embed.py
69 lines (60 loc) · 2.32 KB
/
embed.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
# -*- encoding: utf-8 -*-
'''
@File : embed.py
@Time : 2020/04/09 17:05:35
@Author : Cao Shuai
@Version : 1.0
@Contact : caoshuai@stu.scu.edu.cn
@License : (C)Copyright 2019-2020, MILAB_SCU
@Desc : None
'''
import csv
import numpy as np
import logging
import os
import fire
from tqdm import tqdm
from sentence_transformers import SentenceTransformer
logging.basicConfig(format='%(asctime)s - %(levelname)s - %(name)s - %(message)s',
datefmt='%m/%d/%Y %H:%M:%S',
level=logging.INFO)
logger = logging.getLogger(__name__)
def convert(csv_file, model, ignore_header=False):
pairs = []
with open(csv_file, 'r', encoding='utf-8') as f:
reader = csv.reader(f, delimiter='\t')
for idx, line in enumerate(reader):
if ignore_header and idx == 0: continue
embedding = model.encode(line[:-1])
pairs.append(embedding)
array = np.vstack((pairs[0][0], pairs[0][1]))
for pair in tqdm(pairs[1:], desc="convert to one array"):
array = np.vstack((array, pair[0]))
array = np.vstack((array, pair[1]))
return array
def main(data_dir, train=True, dev=True, test=True, model_name='bert-base-nli-mean-tokens'):
model = SentenceTransformer(model_name)
if train:
logger.info("Start convert train.tsv")
train_csv = os.path.join(data_dir, 'train.tsv')
assert os.path.exists(train_csv), "File path not exists."
train_array = convert(train_csv, model)
train_npy = os.path.join(data_dir, "train_sentence.npy")
np.save(train_npy, train_array)
if dev:
logger.info("Start convert dev.tsv")
dev_csv = os.path.join(data_dir, 'dev.tsv')
assert os.path.exists(dev_csv), "File path not exists."
dev_array = convert(dev_csv, model)
dev_npy = os.path.join(data_dir, "dev_sentence.npy")
np.save(dev_npy, dev_array)
if test:
logger.info("Start convert test.tsv")
test_csv = os.path.join(data_dir, "test.tsv")
assert os.path.exists(test_csv), "File path not exists."
test_array = convert(test_csv, model)
test_npy = os.path.join(data_dir, "test_sentence.npy")
np.save(test_npy, test_array)
logger.info("Successfully converted npy.")
if __name__ == "__main__":
fire.Fire(main)