This is my code:
class EntityRetrieval(object):
def __init__(self,
entity_kb_path: str,
save_after_init=True):
"""
Args
entity_kb_path: path of entity knowledge base (string) which is json format
"""
try:
entity_kb = json.load(open(entity_kb_path, 'r'))
except Exception as e:
logging.error(e)
raise e
# Don't use entity type
entity_dict = {}
for entity_type, entities in entity_kb.items():
for entity_id, list_entity_strings in entities.items():
entity_dict[entity_id] = list_entity_strings
all_entity_strings = []
all_entity_ids = []
for entity_id, list_entity_strings in entity_dict.items():
all_entity_strings.extend(list_entity_strings)
all_entity_ids.extend([entity_id] * len(list_entity_strings))
self.char_tfidf = TfidfVectorizer(analyzer='char',
lowercase=True,
ngram_range=(2, 5))
self.char_tfidf.fit(all_entity_strings)
self.entity_kb_features = self.char_tfidf.transform(all_entity_strings)
self.all_entity_ids = all_entity_ids
@staticmethod
def init_from_pretrained(pretrained_entity_kb_path: str) -> EntityRetrieval:
# Error here, cannot define EntityRetrieval
As I want to wrap it in class and do not want to use a separated function. I guess it’s more professional. So I create a static method. init_from_pretrained
but it gave me an error which is not define Entity Retrieval. And the pretrained_entity_kb
is binary file which is saved by pickle
lib
So in best practice, how to do it?
1