text: import gensim
  import re
  
  
  class TrainWordVector(object):
      _embedding_len = 200  # 词向量长度
      _min_count = 10  # 模型训练最小词数量
      _window = 3  # 词窗口大小
      _iter = 1  # 迭代次数
  
      @classmethod
      def load_exists_vector(cls, filename, cnt=100000):
          """ 加载已有的词向量 """
          model = gensim.models.Word2Vec(
              size=cls._embedding_len,
              window=cls._window,
              min_count=cls._min_count,
              iter=cls._iter
          )
          print("load existing vectors ...")
          corpus = []
          with open(filename, "r", encoding="utf-8") as f:
              for i, line in enumerate(f):
                  if i > cnt:
                      break
                  line = line.split()
                  if len(line) == cls._embedding_len + 1:
                      corpus.append([line[0]] * cls._min_count)
          print("vector count: {}".format(len(corpus)))
          print("start building ...")
          model.build_vocab(corpus)
          print("start intersecting word2vec ...")
          model.intersect_word2vec_format(filename, lockf=1.0, binary=False)
          print("load finished !")
          return model
  
      @classmethod
      def incremental_training(cls, model, corpus):
          """ 增量训练 """
          model.build_vocab(corpus, keep_raw_vocab=True, update=True)
          model.train(corpus, total_examples=model.corpus_count, epochs=model.iter)
          return model
  
      @classmethod
      def load_model(cls, model_path):
          """ 加载模型 """
          return gensim.models.Word2Vec.load(model_path)
  
      @classmethod
      def save_model(cls, model, model_path):
          """ 保存模型 """
          return model.save(model_path)
  
  
  class Common(object):
      @classmethod
      def get_corpus(cls, filename, cnt=100000):
          corpus = []
          with open(filename, "r", encoding="utf-8") as f:
              for i, line in enumerate(f):
                  if i > cnt:
                      break
                  line = line.split()
                  corpus.append(cls.filter_corpus(line))
          return corpus
  
      @classmethod
      def filter_corpus(cls, word_list):
          filtered_word_list = []
          for word in word_list:
              word = cls._text_filter(word)
              if word:
                  filtered_word_list.append(word)
          return filtered_word_list
  
      @classmethod
      def _text_filter(cls, text):
          rule = re.compile(u"[^a-zA-Z0-9\u4e00-\u9fa5]")
          text = rule.sub("", text)
          return text
  
  
  if __name__ == "__main__":
      # # 加载已有词向量
      # word_vector_model = TrainWordVector.load_exists_vector("../../files/Tencent_AILab_ChineseEmbedding.txt")
      #
      # # 保存模型
      # TrainWordVector.save_model(word_vector_model, "./output/models/word_vector/v1/word_vector.model")
  
      # 加载模型
      word_vector_model = TrainWordVector.load_model("./output/models/word_vector/v1/word_vector.model")
  
      # 原始模型相似词测试
      ans = word_vector_model.most_similar("分手")
      print(ans)
  
      # 增量训练
      incremental_corpus = Common.get_corpus("./data/netease_comment.txt", 3000000)
      word_vector_model = TrainWordVector.incremental_training(word_vector_model, incremental_corpus)
  
      # 增量训练后相似词测试
      ans = word_vector_model.most_similar("分手")
      print(ans)
  