class Entity:
    """
    DOCSTRING
    """
    def __init__(self, username=None, password=None, version=None):
        self.conversation = Conversation(username=username,
                                         password=password,
                                         version=version)

    def get_all_entities(self,
                         workspace_id=None,
                         export=None,
                         page_limit=None,
                         include_count=None,
                         sort=None,
                         cursor=None):
        response = self.conversation.list_entities(workspace_id=workspace_id,
                                                   export=export,
                                                   page_limit=page_limit,
                                                   include_count=include_count,
                                                   sort=sort,
                                                   cursor=cursor)
        print(json.dumps(response, indent=2))

    def create_entity(self,
                      workspace_id=None,
                      entity=None,
                      description=None,
                      metadata=None,
                      values=None,
                      fuzzy_match=False):
        response = self.conversation.create_entity(workspace_id=workspace_id,
                                                   entity=entity,
                                                   description=description,
                                                   metadata=metadata,
                                                   values=values,
                                                   fuzzy_match=fuzzy_match)
        print(json.dumps(response, indent=2))

    def delete_entity(self, workspace_id=None, entity=None):
        self.conversation.delete_entity(workspace_id=workspace_id,
                                        entity=entity)
        print("[!]Deleted Entity {}[!]".format(entity))

    def get_entity(self, workspace_id=None, entity=None, export=None):
        response = self.conversation.get_entity(workspace_id=workspace_id,
                                                entity=entity,
                                                export=export)
        print(json.dumps(response, indent=2))

    def update_entity(self,
                      workspace_id=None,
                      entity=None,
                      new_entity=None,
                      new_description=None,
                      new_metadata=None,
                      new_fuzzy_match=None,
                      new_value=None):
        response = self.conversation.update_entity(
            workspace_id=workspace_id,
            entity=entity,
            new_entity=new_entity,
            new_description=new_description,
            new_metadata=new_metadata,
            new_fuzzy_match=new_fuzzy_match,
            new_values=new_value)
        print(json.dumps(response, indent=2))
Example #2
0
def _load_entity_data(conversation: ConversationV1 = None,
                      workspace_id: str = None,
                      entity_data: List[dict] = None,
                      config_data: dict = None):
    """ Add all the entity data to the target workspace

    parameters:
    conversation: instance of Conversation from WDC SDK
    workspace_id: target workspace id
    entity_data: List of entity dict objects
    config_data: Dict of configuration options
        clear_existing: will clear existing examples from target
    """
    clear_existing = config_data['clear_existing'] \
        if config_data is not None and \
        'clear_existing' in config_data.keys() else False
    if entity_data is None:
        return

    existing_entities = conversation.list_entities(workspace_id, export=True)


    for entity in entity_data:
        # check if there is an existing entity
        existing = _get_entity_from_export(
            entity['entity'],
            existing_entities
        )
        if existing is None:
            # entity does not exist in target, so free to create
            conversation.create_entity(
                workspace_id=workspace_id,
                entity=entity['entity'],
                values=entity['values'],
                description=entity['description']
                if 'description' in entity.keys()
                else None,
                fuzzy_match=entity['fuzzy_match']
                if 'fuzzy_match' in entity.keys()
                else False)
        else:
            if clear_existing:
                # entity exists in target, but it should be overwritten
                conversation.update_entity(
                    workspace_id=workspace_id,
                    entity=existing['entity'],
                    new_values=entity['values'],
                    new_description=entity['description']
                    if 'description' in entity.keys()
                    else None,
                    new_fuzzy_match=entity['fuzzy_match']
                    if 'fuzzy_match' in entity.keys()
                    else False)
            else:
                # otherwise we need to merge
                existing = _merge_entities(existing, entity)
                conversation.update_entity(
                    workspace_id=workspace_id,
                    entity=existing['entity'],
                    new_values=existing['values'],
                    new_description=entity['description']
                    if 'description' in entity.keys()
                    else existing['description'],
                    new_fuzzy_match=entity['fuzzy_match']
                    if 'fuzzy_match' in entity.keys()
                    else existing['fuzzy_match'])