# -*- coding: utf-8 -*-
import gensim
import jieba
import pandas as pd
from tqdm import tqdm
from datetime import datetime


class yjiang(object):
    def __init__(self):
        self.idx2id = None
        self.raw_sentence = None
        self.preprocess_sentence = None
        self.vecs = None
        self.alternative_words = set()

    def load_raw(self, file_path):
        data = pd.read_csv(file_path)
        self.idx2id = data["productid"].to_dict()
        self.raw_sentence = data[u"commentcontent"].map(str).tolist()

    def preprocess(self):
        cut_sentence = [list(jieba.cut(s)) for s in tqdm(self.raw_sentence)]
        self.preprocess_sentence = cut_sentence  # 可能这里还需要去掉停用词

    def train_wordvec(self, param="default"):
        if param == "default":
            param = {
                "size": 100,  # Word vector dimensionality
                "min_count": 5,  # Minimum word count
                "workers": 4,  # Number of threads to run in parallel
                "window": 10,  # Context window size
                "sample": 1e-3  # Downsample setting for frequent words
            }
        print "\n{} Training wordvector ...".format(datetime.now())
        model = gensim.models.Word2Vec(self.preprocess_sentence, seed=1, **param)
        model.init_sims(replace=True)
        print "\n{} Saving wordvector ...".format(datetime.now())
        model.save("{}features_{}mincount_{}windowsize".format(param["size"], param["min_count"], param["window"]))

    def load_wordvec(self, filename, test=False):
        self.vecs = gensim.models.Word2Vec.load(filename)
        if test:
            print self.vecs.most_similar(u"不错")
            print "Successfully!"

    def find_alternative_words(self, threshold=0.9, k=5):
        print "{} Finding alternative words ...".format(datetime.now())
        for w in tqdm(self.vecs.vocab):
            t = self.vecs.similar_by_vector(w, N)
            i = 0
            while i < k:
                if t[i][1] >= threshold:
                    self.alternative_words.add((w, t[i][0]) if w > t[i][0] else (t[i][0], w))
                else:
                    break
                i += 1

    def run(self, file_path):
        self.load_raw(file_path)
        self.preprocess()
        self.train_wordvec()
        self.load_wordvec('100features_5mincount_10windowsize')
        self.find_alternative_words()
        print "\n", self.alternative_words.__len__()


if __name__ == "__main__":
    tt = yjiang()
    tt.run("text.csv")
