-
Notifications
You must be signed in to change notification settings - Fork 12
/
Copy pathutils.py
182 lines (140 loc) · 5.83 KB
/
utils.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
import os
import re
import random
import logging
from collections import Counter
import gdown
import torch
import numpy as np
from seqeval.metrics import precision_score, recall_score, f1_score, classification_report
logger = logging.getLogger(__name__)
def download_vgg_features(args):
""" Download vgg features"""
vgg_path = os.path.join(args.data_dir, args.img_feature_file)
if not os.path.exists(vgg_path):
logger.info("Downloading vgg img features...")
gdown.download("https://drive.google.com/uc?id=1q9CRm5gCuU9EVEA6Y4xYp6naskTL0bs4", vgg_path, quiet=False)
def init_logger():
logging.basicConfig(format='%(asctime)s - %(levelname)s - %(name)s - %(message)s',
datefmt='%m/%d/%Y %H:%M:%S',
level=logging.INFO)
def preprocess_word(word):
"""
- Do lowercase
- Regular expression (number, url, hashtag, user)
- https://nlp.stanford.edu/projects/glove/preprocess-twitter.rb
:param word: str
:return: word: str
"""
number_re = r"[-+]?[.\d]*[\d]+[:,.\d]*"
url_re = r"https?:\/\/\S+\b|www\.(\w+\.)+\S*"
hashtag_re = r"#\S+"
user_re = r"@\w+"
if re.compile(number_re).match(word):
word = '<NUMBER>'
elif re.compile(url_re).match(word):
word = '<URL>'
elif re.compile(hashtag_re).match(word):
word = word[1:] # only erase `#` at the front
elif re.compile(user_re).match(word):
word = word[1:] # only erase `@` at the front
word = word.lower()
return word
def build_vocab(args):
"""
Build vocab from train, dev and test set
Write all the tokens in vocab. When loading the vocab, limit the size of vocab at that time
"""
# Read all the files
words, chars = [], []
for filename in [args.train_file, args.dev_file, args.test_file]:
with open(os.path.join(args.data_dir, filename), 'r', encoding='utf-8') as f:
for line in f:
line = line.strip()
if line != "":
if not line.startswith('IMGID:'):
word = line.split('\t')[0]
word = preprocess_word(word)
words.append(word)
for char in word:
chars.append(char)
if not os.path.exists(args.vocab_dir):
os.mkdir(args.vocab_dir)
word_vocab, char_vocab = [], []
word_vocab_path = os.path.join(args.vocab_dir, "word_vocab")
char_vocab_path = os.path.join(args.vocab_dir, "char_vocab")
word_counts = Counter(words)
word_vocab.append("[pad]")
word_vocab.append("[unk]")
word_vocab.extend([x[0] for x in word_counts.most_common()])
logger.info("Total word vocabulary size: {}".format(len(word_vocab)))
with open(word_vocab_path, 'w', encoding='utf-8') as f:
for word in word_vocab:
f.write(word + "\n")
char_counts = Counter(chars)
char_vocab.append("[pad]")
char_vocab.append("[unk]")
char_vocab.extend([x[0] for x in char_counts.most_common()])
logger.info("Total char vocabulary size: {}".format(len(char_vocab)))
with open(char_vocab_path, 'w', encoding='utf-8') as f:
for char in char_vocab:
f.write(char + "\n")
# Set the exact vocab size
# If the original vocab size is smaller than args.vocab_size, then set args.vocab_size to original one
with open(word_vocab_path, 'r', encoding='utf-8') as f:
word_lines = f.readlines()
args.word_vocab_size = min(len(word_lines), args.word_vocab_size)
with open(char_vocab_path, 'r', encoding='utf-8') as f:
char_lines = f.readlines()
args.char_vocab_size = min(len(char_lines), args.char_vocab_size)
logger.info("args.word_vocab_size: {}".format(args.word_vocab_size))
logger.info("args.char_vocab_size: {}".format(args.char_vocab_size))
def load_vocab(args):
word_vocab_path = os.path.join(args.vocab_dir, "word_vocab")
char_vocab_path = os.path.join(args.vocab_dir, "char_vocab")
if not os.path.exists(word_vocab_path):
logger.warning("Please build word vocab first!")
return
if not os.path.exists(char_vocab_path):
logger.warning("Please build char vocab first!")
return
word_vocab = dict()
char_vocab = dict()
word_ids_to_tokens = []
char_ids_to_tokens = []
# Load word vocab
with open(word_vocab_path, "r", encoding="utf-8") as f:
# Set the exact vocab size
# If the original vocab size is smaller than args.vocab_size, then set args.vocab_size to original one
word_lines = f.readlines()
args.word_vocab_size = min(len(word_lines), args.word_vocab_size)
for idx, line in enumerate(word_lines[:args.word_vocab_size]):
line = line.strip()
word_vocab[line] = idx
word_ids_to_tokens.append(line)
# Load char vocab
with open(char_vocab_path, "r", encoding="utf-8") as f:
char_lines = f.readlines()
args.char_vocab_size = min(len(char_lines), args.char_vocab_size)
for idx, line in enumerate(char_lines[:args.char_vocab_size]):
line = line.strip()
char_vocab[line] = idx
char_ids_to_tokens.append(line)
return word_vocab, char_vocab, word_ids_to_tokens, char_ids_to_tokens
def set_seed(args):
random.seed(args.seed)
np.random.seed(args.seed)
torch.manual_seed(args.seed)
if not args.no_cuda and torch.cuda.is_available():
torch.cuda.manual_seed_all(args.seed)
def compute_metrics(labels, preds):
assert len(labels) == len(preds)
return f1_pre_rec(labels, preds)
def f1_pre_rec(labels, preds):
return {
"precision": precision_score(labels, preds),
"recall": recall_score(labels, preds),
"f1": f1_score(labels, preds),
}
def report(labels, preds):
return classification_report(labels, preds)