-
Notifications
You must be signed in to change notification settings - Fork 0
/
train_word2vec_model.py
60 lines (42 loc) · 1.78 KB
/
train_word2vec_model.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
import warnings
warnings.filterwarnings(action='ignore', category=UserWarning, module='gensim')# 忽略警告
import logging
import os.path
import sys
import argparse
import multiprocessing
from gensim.corpora import WikiCorpus
from gensim.models import Word2Vec
from gensim.models.word2vec import LineSentence
if __name__ == '__main__':
# create parser object
parser = argparse.ArgumentParser(description="A word2vec model training script!")
# defining arguments for parser object
parser.add_argument("-i", "--input", type=str, nargs=1,
metavar="filename", default=None,
help="Opens the specified text file.")
parser.add_argument("-o", "--output", type=str, nargs=1,
metavar="filename", default=None,
help="Output file name")
parser.add_argument("-d", "--dim", type=int, nargs=1,
metavar="dimension", default=None,
help="word vector dimension")
# parse the arguments from standard input
args = parser.parse_args()
program = os.path.basename(sys.argv[0])
logger = logging.getLogger(program)
logging.basicConfig(format='%(asctime)s: %(levelname)s: %(message)s',level=logging.INFO)
logger.info("running %s" % ' '.join(sys.argv))
dimension = args.dim[0]
inp = args.input[0]
print(args.input)
if args.output != None:
outp1 = args.output[0]+'.model'
outp2 = args.output[0]+'.txt'
else:
outp1 = args.input[0] + '.model'
outp2 = args.input[0] + '.txt'
model = Word2Vec(LineSentence(inp), size=dimension, window=5, min_count=5,
workers=multiprocessing.cpu_count())
model.save(outp1)
model.wv.save_word2vec_format(outp2, binary=False)