y=(((len(pos),dtype=int),
(len(neg),dtype=int)))
returncombined,y
#定义函数,对句子进行分词,并去掉换行符
deftokenizer(text):
text=[(('\n',''))fordocument
intext]
returntext
#创建词典,并返回每个词语的索引、词向量以及每个句子所对应的词语索引
defcreate_dictionaries(model=None,
combined=None):
if(combinedisnotNone)and(modelisnotNone):
}bl
} gensim_dict=Dictionary()
gensim_dict.doc2bow((),allow_updat
e=True)
#所有频数超过10的词语的索引
w2indx={v:k+1fork,vingensim_dict.items()}
#所有频数超过10的词语的词向量
w2vec={word:model[word] ()}
defparse_dataset(combined):
data=[]
forsentenceincombined:
new_txt=[]
forwordinsentence:
try:
new_txt.append(w2indx[word])
except:
new_txt.append(0)
(new_txt)
returndata
combined=parse_dataset(combined)
#每个句子所含词语对应的索引
combined=sequence.pad_sequences(combined,maxlen=maxlen)
returnw2indx,w2vec,combined
else:
print("Nodataprovided...")
#定义词向量函数,读取训练好的词向量模型。调用创建词典的函数
#返回每个词语的索引、词向量以及每个句子所对应的词语索引
defword2vec_train(combined):
