-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathpreW2V.py
49 lines (38 loc) · 934 Bytes
/
preW2V.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
# -*- coding: utf-8 -*-
#
# 输入doc,生成分词后的语料
#
#
import re
import logging
import os.path
import sys
import multiprocessing
import jieba
from gensim.models import Word2Vec
from gensim.models.word2vec import LineSentence
reload(sys)
sys.setdefaultencoding('utf8')
output = "./output/"
def saveObject(filename,obj):
f=open(filename,'wb')
pickle.dump(obj,f)
f.close()
return True
etlregex = re.compile(ur"[^\u4e00-\u9f5a0-9]")
def etl(content):
content = etlregex.sub('',content)
return content
#原始语料集合
train_set=[]
docinfos = []
#读取文本,进行切词操作
f=open("./data/all.txt")
lines=f.readlines()
for line in lines:
content = (line.lower()).split("\t")[2] + (line.lower()).split("\t")[1]
word_list = filter(lambda x: len(x)>0,map(etl,jieba.cut(content,cut_all=False)))
for w in word_list:
print w + " ",
print ""
f.close()