-
Notifications
You must be signed in to change notification settings - Fork 0
/
wiki.py
51 lines (36 loc) · 1.46 KB
/
wiki.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
import sys
import logging
import os
from gensim.models import Word2Vec
from gensim.models.word2vec import LineSentence
from utils.utils import parse_args, slice
if __name__ == '__main__':
program = os.path.basename(sys.argv[0])
logger = logging.getLogger(program)
logging.basicConfig(
filename='wiki_log.txt',
format='%(asctime)s: %(levelname)s: %(message)s',
level=logging.INFO)
logger.info("running %s" % ' '.join(sys.argv))
# check and process input arguments
args = parse_args(sys.argv[1:])
for arg in ['input', 'output']:
if not arg in args:
logger.error('Argument ' + arg + ' is missing')
sys.exit(1)
limit, workers, min_count, size, input, output = args['limit'], args['workers'], args['min_count'], \
args['size'], args['input'], args['output']
# log arguments
logger.info('Training with: ' + ' '.join([k + " : " + str(v) for k, v in args.iteritems()]))
# import sentences
sentences = slice(LineSentence(input), limit);
try:
# train model
model = Word2Vec(sentences, min_count=min_count, workers=workers, size=size)
# Save model
logger.info('Saving model to file')
model.save(output + "_" + str(limit))
logger.info('Model has been saved.')
except Exception as exc:
logger.exception('Exception in model training: ' + str(exc))
logger.info('Done!')