match model factory
| 24 | ''' |
| 25 | |
| 26 | class ModelFactory(object): |
| 27 | '''match model factory |
| 28 | ''' |
| 29 | def __init__(self, match_models=['bow', 'tfidf', 'ngram_tfidf'], |
| 30 | bow_model = Bow, |
| 31 | tf_idf_model=TfIdf, |
| 32 | ngram_tf_idf_model=NgramTfIdf, |
| 33 | w2v_model=Word2Vec, |
| 34 | #bert_embedding_model=BertEmbedding, |
| 35 | albert_embedding_model=ALBertEmbedding, |
| 36 | ): |
| 37 | self.model = {} |
| 38 | for match_model in match_models: |
| 39 | if match_model == 'bow': |
| 40 | model = bow_model( dic_path=const.BOW_DIC_PATH, |
| 41 | bow_index_path=const.BOW_INDEX_PARH, ) |
| 42 | self.model[match_model] = model |
| 43 | elif match_model == 'tfidf': |
| 44 | model = tf_idf_model( dic_path=const.TFIDF_DIC_PATH, |
| 45 | tfidf_model_path=const.TFIDF_MODEL_PATH, |
| 46 | tfidf_index_path=const.TFIDF_INDEX_PATH, ) |
| 47 | self.model[match_model] = model |
| 48 | elif match_model == 'ngram_tfidf': |
| 49 | model = ngram_tf_idf_model( dic_path=const.NGRAM_TFIDF_DIC_PATH, |
| 50 | tfidf_model_path=const.NGRAM_TFIDF_MODEL_PATH, |
| 51 | tfidf_index_path=const.NGRAM_TFIDF_INDEX_PATH, ) |
| 52 | self.model[match_model] = model |
| 53 | elif match_model == 'w2v': |
| 54 | model = w2v_model( w2v_model_file=const.W2V_MODEL_FILE, |
| 55 | stop_word=StopWords(stopwords_file=const.STOPWORDS_FILE) ) |
| 56 | self.model[match_model] = model |
| 57 | elif match_model == 'bert': |
| 58 | model = bert_embedding_model( config_path=const.BERT_CONFIG_PATH, |
| 59 | checkpoint_path = const.BERT_CHECKPOINT_PATH, |
| 60 | dict_path = const.BERT_DICT_PATH) |
| 61 | self.model[match_model] = model |
| 62 | elif match_model == 'albert': |
| 63 | model = albert_embedding_model( |
| 64 | config_path=const.ALBERT_CONFIG_PATH, |
| 65 | albert_checkpoint_path = const.ALBERT_CHECKPOINT_PATH, |
| 66 | dict_path = const.ALBERT_DICT_PATH, ) |
| 67 | #albert_checkpoint_path = const.ALCHECKPOINT_PATH) |
| 68 | self.model[match_model] = model |
| 69 | else: |
| 70 | logging.error( "[ModelFactory] match_model not existed,please select from ['bow', 'tfidf', 'ngram_tfidf', 'w2v', 'bert', 'albert'] " ) |
| 71 | continue |
| 72 | |
| 73 | def init(self, words_dict=None, update=False): |
| 74 | if words_dict != None: |
| 75 | self.id_lists, self.words_list = self._dic2list(words_dict) |
| 76 | else: |
| 77 | self.id_lists, self.words_list = None, None |
| 78 | for key, model in self.model.items(): |
| 79 | self.model[key] = model.init(self.words_list, update) |
| 80 | |
| 81 | # id list / words list |
| 82 | def _dic2list(self, words_dict): |
| 83 | return list( words_dict.keys() ) , list( words_dict.values() ) |
no outgoing calls
no test coverage detected