def _do_normal(self): b = BaseInput(self.normal_path) print("删除所有普通标签") Normal.delete_all() def callback(info): normal = Normal(**info) normal.save() print(info.get('slug', '')) b.input(callback)
def handle(self, *args, **options): print("Will delete the table of tags and normal tags, Y/n") delete = raw_input() if not delete == "Y": return Tag.delete_all() Normal.delete_all() self.do_init()
def normal_update(self, **kwargs): smart_print(kwargs, 'normal_update') try: slug = kwargs.get('slug', '') if not slug: return {'success': False} Normal.cls_update(**kwargs) except Exception: return {'success': False} else: return {'success': True}
def add_tag(self, tag, force_add=False): name = tag.name if not name: return score = getattr(tag, 'score', 1.0) or 1.0 parents = getattr(tag, 'parents', []) or [] items = getattr(tag, 'items', []) or [] instance_items = [] equal_to = getattr(tag, 'equal_to', '') or '' for item in items: slug = item['slug'] or name if (not force_add) and (self.memory_normal_items.exists(slug) or self.memory_place_items.exists(slug)): continue category = item['class'] if category == 'NORMAL': item_info = Normal.get_by_slug(slug, json_format=True) if not item_info: continue item_instance = NormalItemEntity(tag_name=name, parents=parents, **item_info) self.memory_normal_items.add(slug, item_instance) else: item_info = Place.get_by_slug(slug, json_format=True) if not item_info: continue item_instance = PlaceItemEntity(tag_name=name, **item_info) self.memory_place_items.add(slug, item_instance) instance_items.append(item_instance) tag_instance = TagEntity(name, score=score, equal_to=equal_to, parents=parents, items=instance_items) self.memory_tags.add(name, tag_instance)
def do_init(self): file = open(self.file_path, "r") lines = file.readlines() file.close() for line in lines[1:]: info = line.decode("utf-8")[:-1] tag_name, score, parents_str, equal_to, items_str = info.split("\t") print(tag_name) score = 1.0 items = [] parents = parents_str.split(",") for item in items_str.split(","): name, class_name = item.split("__") if class_name == "NORMAL": print("Save normal tag %s" % name) n = Normal(slug=name, score=score) n.save() items.append({"slug": name, "class": class_name}) tag = Tag(name=tag_name, score=score, parents=parents, equal_to=equal_to, items=items) tag.save()
def do_normal(self): path = os.path.join(BASE, 'output_normal.csv') file = open(path, 'w') for n in Normal.objects(): name = getattr(n, 'slug', '') if not name: return score = getattr(n, 'score', settings.NEW_WORD_DEFAULT_VALUE) info = '%s\t%f\n' % (name, score) print(name) file.write(info.encode('utf-8')) file.close()
def load(self): with open(name=self.path, mode='r') as f: for each_line in f.readlines(): name, parents_str = each_line.decode('utf-8')[:-1].split('\t') parents = parents_str.split(',') print(name, parents) # To normal model exists = Normal.get_by_slug(name) if not exists: item = Normal(slug=name) print("Saving Normal Item %s" % name) item.save() for parent in parents: exists = Normal.get_by_slug(parent) if not exists: parent_item = Normal(slug=parent) print("Saving Normal Item %s" % parent) parent_item.save() # To tag model exists = Tag.get_by_name(name) if not exists: tag = Tag(name=name, parents=parents, score=self.score, items=[{'slug': name, 'class': 'NORMAL'},], proxy='NORMAL') print("Tag Item, %s" % name) tag.save() for parent in parents: exists = Tag.get_by_name(parent) if not exists: tag = Tag(name=parent, score=self.score, items=[{'slug': parent, 'class': 'NORMAL'},], proxy='NORMAL') print("Tag item %s " % parent) tag.save()
def callback(info): normal = Normal(**info) normal.save() print(info.get('slug', ''))