-
Notifications
You must be signed in to change notification settings - Fork 0
/
word2vec.py
41 lines (31 loc) · 1.12 KB
/
word2vec.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
#!/usr/bin/env python
import logging
import os.path
import sys
import multiprocessing
from gensim.models import Word2Vec
import re
class MySentences(object):
def __init__(self, filename):
self.filename = filename
def __iter__(self):
for line in open(self.filename, 'r'):
textline = line.lower()
text=re.sub(r'\[(.*?)\|(.*?)\]',r'\1',textline)
text=re.sub(r'[\[\]\(\)\:"\.;\',]',r' ', text)
text=re.sub(r'[0-9]*', r' ', text)
yield text.split()
if __name__ == '__main__':
program = os.path.basename(sys.argv[0])
logger = logging.getLogger(program)
logging.basicConfig(format='%(asctime)s : %(levelname)s : %(message)s')
logging.root.setLevel(level=logging.INFO)
logger.info("running %s" % ' '.join(sys.argv))
if len(sys.argv) < 3:
print (globals()['__doc__'] % locals())
sys.exit(1)
inp, outp = sys.argv[1:3]
model = Word2Vec(MySentences(inp), size = 100, window = 8, min_count = 5,
workers = multiprocessing.cpu_count())
model.init_sims(replace=True)
model.save(outp)