-
Notifications
You must be signed in to change notification settings - Fork 12
/
fasttext_imdb.py
32 lines (25 loc) · 998 Bytes
/
fasttext_imdb.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
import fasttext
import prenlp
from prenlp.data import Normalizer
from prenlp.tokenizer import NLTKMosesTokenizer
normalizer = Normalizer(emoji_repl=None)
# Data preparation
imdb_train, imdb_test = prenlp.data.IMDB()
# Preprocessing
tokenizer = NLTKMosesTokenizer()
for dataset in [imdb_train, imdb_test]:
for i, (text, label) in enumerate(dataset):
dataset[i][0] = ' '.join(tokenizer(normalizer.normalize(text.strip()))) # both
# dataset[i][0] = text.strip() # original
# dataset[i][0] = normalizer.normalize(text.strip()) # only normalization
# dataset[i][0] = ' '.join(tokenizer(text.strip())) # only tokenization
prenlp.data.fasttext_transform(imdb_train, 'imdb.train')
prenlp.data.fasttext_transform(imdb_test, 'imdb.test')
# Train
model = fasttext.train_supervised(input='imdb.train', epoch=25)
# Evaluate
print(model.test('imdb.train'))
print(model.test('imdb.test'))
# Inference
print(imdb_test[0][0])
print(model.predict(imdb_test[0][0]))