Exemplo n.º 1
0
def validate_name_request(location, entity_type, request_action):
    # Raise error if location is invalid
    if location not in ValidLocations.list():
        raise ValueError('Invalid location provided')

    # Raise error if request_action is invalid
    if request_action not in AnalysisRequestActions.list():
        raise ValueError('Invalid request action provided')

    # Throw any errors related to invalid entity_type or request_action for a location
    if location == ValidLocations.CA_BC.value:
        is_protected = False
        is_unprotected = False

        # Determine what request actions are valid
        valid_request_actions = ()

        if entity_type in BCProtectedNameEntityTypes.list():
            is_protected = True
            valid_request_actions = (AnalysisRequestActions.NEW.value,
                                     AnalysisRequestActions.AML.value)

        elif entity_type in BCUnprotectedNameEntityTypes.list():
            is_unprotected = True
            valid_request_actions = (AnalysisRequestActions.NEW.value,
                                     AnalysisRequestActions.DBA.value)

        if is_protected and is_unprotected:
            raise ValueError(
                'An entity name cannot be both protected and unprotected')

        if is_protected and entity_type not in BCProtectedNameEntityTypes.list(
        ):
            raise ValueError(
                'Invalid entity_type provided for a protected BC entity name')

        if is_unprotected and entity_type not in BCUnprotectedNameEntityTypes.list(
        ):
            raise ValueError(
                'Invalid entity_type provided for an unprotected BC entity name'
            )

        if request_action not in valid_request_actions:
            raise Exception('Operation not currently supported')

    elif location in (ValidLocations.CA_NOT_BC.list(),
                      ValidLocations.INTL.list()):
        # If XPRO, nothing is protected (for now anyway)
        valid_request_actions = (AnalysisRequestActions.NEW.value,
                                 AnalysisRequestActions.DBA.value)

        if entity_type not in XproUnprotectedNameEntityTypes.list():
            raise ValueError('Invalid entity_type provided for an XPRO entity')

        if request_action not in valid_request_actions:
            raise Exception('Operation not currently supported')

    return True
Exemplo n.º 2
0
def validate_name_request(location, entity_type, request_action):
    """ Validates the name analysis request."""
    # Raise error if location is invalid
    if location not in ValidLocations.list():
        raise ValueError('Invalid location provided')

    # Raise error if request_action is invalid
    if request_action not in AnalysisRequestActions.list():
        raise ValueError('Invalid request action provided')

    # Throw any errors related to invalid entity_type or request_action for a location
    if location == ValidLocations.CA_BC.value:
        is_protected = False
        is_unprotected = False
        valid_request_actions = []

        if entity_type in BCProtectedNameEntityTypes.list():
            is_protected = True
            valid_request_actions = [
                AnalysisRequestActions.NEW.value,
                AnalysisRequestActions.CHG.value,
                AnalysisRequestActions.MVE.value
            ]
        elif entity_type in BCUnprotectedNameEntityTypes.list():
            is_unprotected = True
            valid_request_actions = (AnalysisRequestActions.NEW.value, AnalysisRequestActions.DBA.value)

        if is_protected and is_unprotected:
            raise ValueError('An entity name cannot be both protected and unprotected')

        if is_protected and entity_type not in BCProtectedNameEntityTypes.list():
            raise ValueError('Invalid entity_type provided for a protected BC entity name')

        if is_unprotected and entity_type not in BCUnprotectedNameEntityTypes.list():
            raise ValueError('Invalid entity_type provided for an unprotected BC entity name')

    else:
        valid_location = location in [ValidLocations.CA_NOT_BC.value, ValidLocations.INTL.value]
        valid_request_actions = [
            AnalysisRequestActions.NEW.value,
            AnalysisRequestActions.AML.value,
            AnalysisRequestActions.CHG.value,
            AnalysisRequestActions.ASSUMED.value,
            AnalysisRequestActions.REN.value,
            AnalysisRequestActions.REH.value
        ]

        if not valid_location:
            raise ValueError('Invalid location provided')

        if entity_type not in XproUnprotectedNameEntityTypes.list():
            raise ValueError('Invalid entity_type provided for an XPRO entity')

    if request_action not in valid_request_actions:
        raise Exception('Operation not currently supported')

    return True
Exemplo n.º 3
0
def get_entity_type_code_DEPRECATED(entity_type_str):
    entity_type_code = None
    if BCProtectedNameEntityTypes.has_value(entity_type_str):
        entity_type_code = BCProtectedNameEntityTypes(entity_type_str)
    elif BCUnprotectedNameEntityTypes.has_value(entity_type_str):
        entity_type_code = BCUnprotectedNameEntityTypes(entity_type_str)
    elif XproUnprotectedNameEntityTypes.has_value(entity_type_str):
        entity_type_code = XproUnprotectedNameEntityTypes(entity_type_str)

    return entity_type_code
Exemplo n.º 4
0
    def _set_designations_by_entity_type_user(self):
        syn_svc = self.synonym_service
        entity_type = self.entity_type

        entity_type_code = None
        if BCProtectedNameEntityTypes.has_value(
                entity_type) and BCProtectedNameEntityTypes(entity_type):
            entity_type_code = BCProtectedNameEntityTypes(entity_type)
        elif BCUnprotectedNameEntityTypes.has_value(
                entity_type) and BCUnprotectedNameEntityTypes(entity_type):
            entity_type_code = BCUnprotectedNameEntityTypes(entity_type)
        elif XproUnprotectedNameEntityTypes.has_value(
                entity_type) and XproUnprotectedNameEntityTypes(entity_type):
            entity_type_code = XproUnprotectedNameEntityTypes(entity_type)
        else:
            raise Exception(
                'Could not set entity type user designations - entity type [' +
                entity_type + '] was not found in BC or XPRO entity types!')

        self._eng_designation_any_list_correct = syn_svc.get_designations(
            entity_type_code=entity_type_code.value,
            position_code=DesignationPositionCodes.ANY.value,
            lang=LanguageCodes.ENG.value).data

        self._eng_designation_end_list_correct = syn_svc.get_designations(
            entity_type_code=entity_type_code.value,
            position_code=DesignationPositionCodes.END.value,
            lang=LanguageCodes.ENG.value).data

        self._fr_designation_any_list_correct = syn_svc.get_designations(
            entity_type_code=entity_type_code.value,
            position_code=DesignationPositionCodes.ANY.value,
            lang=LanguageCodes.FR.value).data

        self._fr_designation_end_list_correct = syn_svc.get_designations(
            entity_type_code=entity_type_code.value,
            position_code=DesignationPositionCodes.END.value,
            lang=LanguageCodes.FR.value).data

        self._eng_designation_all_list_correct = self._eng_designation_any_list_correct + self._eng_designation_end_list_correct
        self._eng_designation_all_list_correct.sort(key=len, reverse=True)

        self._fr_designation_all_list_correct = self._fr_designation_any_list_correct + self._fr_designation_end_list_correct
        self._fr_designation_all_list_correct.sort(key=len, reverse=True)

        self._designation_any_list_correct = self._eng_designation_any_list_correct + self._fr_designation_any_list_correct
        self._designation_any_list_correct.sort(key=len, reverse=True)

        self._designation_end_list_correct = self._eng_designation_end_list_correct + self._fr_designation_end_list_correct
        self._designation_end_list_correct.sort(key=len, reverse=True)
Exemplo n.º 5
0
def validate_name_request(location, entity_type, request_action):
    # Raise error if location is invalid
    if location not in ValidLocations.list():
        raise ValueError('Invalid location provided')

    # Raise error if request_action is invalid
    if request_action not in AnalysisRequestActions.list():
        raise ValueError('Invalid request action provided')

    # Throw any errors related to invalid entity_type or request_action for a location
    if location != ValidLocations.CA_BC.value:
        raise ValueError('Invalid location provided')

    is_protected = False
    is_unprotected = False
    valid_request_actions = []

    if entity_type in BCProtectedNameEntityTypes.list():
        is_protected = True
        valid_request_actions = [
            AnalysisRequestActions.NEW.value, AnalysisRequestActions.CHG.value,
            AnalysisRequestActions.MVE.value
        ]
    elif entity_type in BCUnprotectedNameEntityTypes.list():
        is_unprotected = True
        valid_request_actions = (AnalysisRequestActions.NEW.value,
                                 AnalysisRequestActions.DBA.value)

    if is_protected and is_unprotected:
        raise ValueError(
            'An entity name cannot be both protected and unprotected')

    if is_protected and entity_type not in BCProtectedNameEntityTypes.list():
        raise ValueError(
            'Invalid entity_type provided for a protected BC entity name')

    if is_unprotected and entity_type not in BCUnprotectedNameEntityTypes.list(
    ):
        raise ValueError(
            'Invalid entity_type provided for an unprotected BC entity name')

    if request_action not in valid_request_actions:
        raise Exception('Operation not currently supported')

    return True
Exemplo n.º 6
0
    def _set_designations_by_entity_type_user(self):
        syn_svc = self.synonym_service
        entity_type = self.entity_type

        entity_type_code = None
        if BCProtectedNameEntityTypes(entity_type):
            entity_type_code = BCProtectedNameEntityTypes(entity_type)
        elif BCUnprotectedNameEntityTypes(entity_type):
            entity_type_code = BCUnprotectedNameEntityTypes(entity_type)
        elif XproUnprotectedNameEntityTypes(entity_type):
            entity_type_code = XproUnprotectedNameEntityTypes(entity_type)

        self._eng_designation_any_list_correct = syn_svc.get_designations(
            entity_type_code=entity_type_code.value,
            position_code=DesignationPositionCodes.ANY.value,
            lang=LanguageCodes.ENG.value).data

        self._eng_designation_end_list_correct = syn_svc.get_designations(
            entity_type_code=entity_type_code.value,
            position_code=DesignationPositionCodes.END.value,
            lang=LanguageCodes.ENG.value).data

        self._fr_designation_any_list_correct = syn_svc.get_designations(
            entity_type_code=entity_type_code.value,
            position_code=DesignationPositionCodes.ANY.value,
            lang=LanguageCodes.FR.value).data

        self._fr_designation_end_list_correct = syn_svc.get_designations(
            entity_type_code=entity_type_code.value,
            position_code=DesignationPositionCodes.END.value,
            lang=LanguageCodes.FR.value).data

        self._eng_designation_all_list_correct = self._eng_designation_any_list_correct + self._eng_designation_end_list_correct
        self._eng_designation_all_list_correct.sort(key=len, reverse=True)

        self._fr_designation_all_list_correct = self._fr_designation_any_list_correct + self._fr_designation_end_list_correct
        self._fr_designation_all_list_correct.sort(key=len, reverse=True)

        self._designation_any_list_correct = self._eng_designation_any_list_correct + self._fr_designation_any_list_correct
        self._designation_any_list_correct.sort(key=len, reverse=True)

        self._designation_end_list_correct = self._eng_designation_end_list_correct + self._fr_designation_end_list_correct
        self._designation_end_list_correct.sort(key=len, reverse=True)
Exemplo n.º 7
0
    def _set_designations_by_entity_type_user(self):
        syn_svc = self.synonym_service
        entity_type = self.entity_type

        entity_type_code = None
        if BCProtectedNameEntityTypes.has_value(entity_type):
            entity_type_code = BCProtectedNameEntityTypes(entity_type)
        elif BCUnprotectedNameEntityTypes.has_value(entity_type):
            entity_type_code = BCUnprotectedNameEntityTypes(entity_type)
        elif XproUnprotectedNameEntityTypes.has_value(entity_type):
            entity_type_code = XproUnprotectedNameEntityTypes(entity_type)

        any_list = syn_svc.get_designations(
            entity_type_code=entity_type_code.value,
            position_code=DesignationPositionCodes.ANY.value,
            lang=LanguageCodes.ENG.value).data

        end_list = syn_svc.get_designations(
            entity_type_code=entity_type_code.value,
            position_code=DesignationPositionCodes.END.value,
            lang=LanguageCodes.ENG.value).data

        self._designation_any_list_correct.extend(any_list)
        self._designation_end_list_correct.extend(end_list)
Exemplo n.º 8
0
def bc_validate_name_request(location, entity_type, request_action):
    """Validate the params for bc analysis."""
    errors = []
    # Check location is valid
    if location != ValidLocations.CA_BC.value:
        errors.append('Invalid location provided')

    # Check request_action is valid
    if request_action not in AnalysisRequestActions.list():
        errors.append('Invalid request action provided')

    is_protected = False
    is_unprotected = False
    valid_request_actions = []
    if entity_type in BCProtectedNameEntityTypes.list():
        is_protected = True
        valid_request_actions = [
            AnalysisRequestActions.NEW.value, AnalysisRequestActions.CHG.value,
            AnalysisRequestActions.MVE.value, AnalysisRequestActions.REH.value,
            AnalysisRequestActions.REN.value, AnalysisRequestActions.CNV.value,
            AnalysisRequestActions.AML.value
        ]
    # elif entity_type in BCUnprotectedNameEntityTypes.list():
    #     is_unprotected = True
    #     valid_request_actions = (AnalysisRequestActions.NEW.value, AnalysisRequestActions.DBA.value)

    if is_protected and is_unprotected:
        errors.append(
            'An entity name cannot be both protected and unprotected')

    if is_protected and entity_type not in BCProtectedNameEntityTypes.list():
        errors.append(
            'Invalid entity_type provided for a protected BC entity name')

    if is_unprotected and entity_type not in BCUnprotectedNameEntityTypes.list(
    ):
        errors.append(
            'Invalid entity_type provided for an unprotected BC entity name')

    if request_action not in valid_request_actions:
        errors.append('Operation not currently supported')

    return errors
    def execute_analysis(self):
        try:
            builder = self.builder
            syn_svc = self.synonym_service
            wc_svc = self.word_classification_service
            token_svc = self.token_classifier_service
            np_svc = self._name_processing_service
            stand_alone_words = np_svc.get_stand_alone_words()

            analysis = []

            # Configure the analysis for the supplied builder
            get_classification(self, stand_alone_words, syn_svc, self.name_tokens, wc_svc, token_svc)

            if auto_analyze_config in ('WELL_FORMED_NAME', 'EXACT_MATCH', 'SEARCH_CONFLICTS'):
                check_words_to_avoid = builder.check_words_to_avoid(self.name_tokens, self.processed_name)
                if not check_words_to_avoid.is_valid:
                    analysis.append(check_words_to_avoid)
                    return analysis

                self.set_skip_search_conflicts(True)
                check_name_is_well_formed = builder.check_name_is_well_formed(
                    self._dict_name_words,
                    self._list_dist_words,
                    self._list_desc_words,
                    self.compound_descriptive_name_tokens,
                    self.processed_name,
                    self.name_original_tokens
                )
                if check_name_is_well_formed.result_code in (
                        AnalysisIssueCodes.ADD_DISTINCTIVE_WORD, AnalysisIssueCodes.ADD_DESCRIPTIVE_WORD) and \
                        self.entity_type not in (BCUnprotectedNameEntityTypes.list()) and \
                        not check_name_is_well_formed.is_valid:
                    analysis.append(check_name_is_well_formed)
                elif check_name_is_well_formed.result_code == AnalysisIssueCodes.CORPORATE_CONFLICT:
                    self.set_skip_search_conflicts(True)
                else:
                    self.set_skip_search_conflicts(False)

                check_name_has_valid_number = builder.is_valid_year(self.name_original_tokens)
                if not check_name_has_valid_number.is_valid:
                    analysis.append(check_name_has_valid_number)
                    return analysis

                if analysis:
                    return analysis

                check_word_limit = builder.check_word_limit(self.name_tokens)
                if not check_word_limit.is_valid:
                    analysis.append(check_word_limit)
                    return analysis

                check_word_unclassified = builder.check_unclassified_words(self.name_tokens, self.get_list_none())
                if not check_word_unclassified.is_valid:
                    analysis.append(check_word_unclassified)

                # If the error coming back is that a name is not well formed
                # OR if the error coming back has words to avoid...
                # eg. result.result_code = AnalysisIssueCodes.CONTAINS_UNCLASSIFIABLE_WORD
                # don't return the result yet, the name is well formed, we just have an unclassified
                # word in the result.

                issues_that_must_be_fixed = [
                    AnalysisIssueCodes.ADD_DISTINCTIVE_WORD,
                    AnalysisIssueCodes.ADD_DESCRIPTIVE_WORD,
                    AnalysisIssueCodes.INCORRECT_YEAR,
                    AnalysisIssueCodes.WORDS_TO_AVOID,
                    AnalysisIssueCodes.TOO_MANY_WORDS
                ]

                issue_must_be_fixed = False
                result_codes = list(map(lambda r: r.result_code, analysis))

                for code in result_codes:
                    if code in issues_that_must_be_fixed:
                        issue_must_be_fixed = True
                        break

                if issue_must_be_fixed:
                    return analysis
                    #  Name is not well formed - do not continue

                # If the WORD_TO_AVOID check failed, the UNCLASSIFIED_WORD check
                # will have failed too because words to avoid are never classified.
                # Strip out the unclassified words errors involving the same name words.
                list_avoid = []

                has_words_to_avoid = self._has_analysis_issue_type(analysis, AnalysisIssueCodes.WORDS_TO_AVOID)
                if has_words_to_avoid:
                    matched_words_to_avoid = \
                        self._get_analysis_issue_type_issues(analysis, AnalysisIssueCodes.WORDS_TO_AVOID)

                    for procedure_result in matched_words_to_avoid:
                        list_avoid = list_avoid + procedure_result.values.get('list_avoid', [])

                    def remove_words_to_avoid(result):
                        if result.result_code == AnalysisIssueCodes.CONTAINS_UNCLASSIFIABLE_WORD:
                            for word in list_avoid:
                                result.values['list_none'].remove(word)
                        return result

                    analysis = list(map(remove_words_to_avoid, analysis))

                analysis_issues_sort_order = [
                    AnalysisIssueCodes.ADD_DISTINCTIVE_WORD,
                    AnalysisIssueCodes.ADD_DESCRIPTIVE_WORD,
                    AnalysisIssueCodes.INCORRECT_YEAR,
                    AnalysisIssueCodes.WORDS_TO_AVOID,
                    AnalysisIssueCodes.TOO_MANY_WORDS,
                    AnalysisIssueCodes.CONTAINS_UNCLASSIFIABLE_WORD,
                    AnalysisIssueCodes.WORD_SPECIAL_USE,
                    AnalysisIssueCodes.NAME_REQUIRES_CONSENT,
                    AnalysisIssueCodes.QUEUE_CONFLICT,
                    AnalysisIssueCodes.CORPORATE_CONFLICT,
                    AnalysisIssueCodes.DESIGNATION_NON_EXISTENT,
                    AnalysisIssueCodes.END_DESIGNATION_MORE_THAN_ONCE,
                    AnalysisIssueCodes.DESIGNATION_MISPLACED,
                    AnalysisIssueCodes.DESIGNATION_MISMATCH,
                    AnalysisIssueCodes.DESIGNATION_REMOVAL
                ]

            analysis = analysis + self.do_analysis()
            if self.skip_search_conflicts:
                analysis.append(check_name_is_well_formed)

            analysis = self.sort_analysis_issues(analysis, analysis_issues_sort_order)

            return analysis

        except Exception as error:
            print('Error executing name analysis: ' + repr(error))
            raise
Exemplo n.º 10
0
    def get():
        name = unquote_plus(request.args.get(
            'name').strip()) if request.args.get('name') else None
        location = unquote_plus(request.args.get(
            'location').strip()) if request.args.get('location') else None
        entity_type = unquote_plus(request.args.get('entity_type').strip(
        )) if request.args.get('entity_type') else None
        # TODO: Let's not call var request_type because it's ambiguous - change to request_action on frontend too
        request_action = unquote_plus(
            request.args.get('request_action').strip()
        ) if request.args.get(
            'request_action'
        ) else None  # TODO: Use request_action not request_type, this needs to be updated on the front end!!!
        # request_action = unquote_plus(request.args.get('request_type').strip()) if request.args.get('request_type') else None  # TODO: Leave this as request_type for now...

        # Do our service stuff
        # Instantiate an appropriate service and register a builder for that service (subclasses of NameAnalysisDirector)
        # We can optionally use a different builder if we want to further tweak analysis by modifying the following procedures:
        # do_analysis (performs the checks)
        # check_name_is_well_formed
        # check_words_to_avoid
        # search_conflicts
        # check_words_requiring_consent
        # check_designation

        service = None

        if not validate_name_request(location, entity_type, request_action):
            return  # TODO: Return invalid response! What is it?

        try:
            if location == ValidLocations.CA_BC.value and entity_type in BCProtectedNameEntityTypes.list(
            ) and request_action in (AnalysisRequestActions.NEW.value,
                                     AnalysisRequestActions.AML.value):
                # Use ProtectedNameAnalysisService
                service = ProtectedNameAnalysisService()
                builder = NameAnalysisBuilder(service)

            elif location == ValidLocations.CA_BC.value and entity_type in BCUnprotectedNameEntityTypes.list(
            ) and request_action in (AnalysisRequestActions.NEW.value):
                # Use UnprotectedNameAnalysisService
                service = UnprotectedNameAnalysisService()
                builder = NameAnalysisBuilder(service)

            elif location in (
                    ValidLocations.CA_NOT_BC.value, ValidLocations.INTL.value
            ) and entity_type in XproUnprotectedNameEntityTypes.list(
            ) and request_action in (AnalysisRequestActions.NEW.value,
                                     AnalysisRequestActions.DBA.value):
                # Use UnprotectedNameAnalysisService
                service = UnprotectedNameAnalysisService()
                builder = NameAnalysisBuilder(service)

            else:
                raise Exception('Invalid scenario')

            if not service:
                raise ValueError('Invalid service provided')
            if not builder:
                raise ValueError('Invalid builder provided')

            # Register and initialize the builder
            service.use_builder(builder)  # Required step! TODO: Enforce this!
            service.set_entity_type(
                entity_type)  # Required step! TODO: Enforce this!
            service.set_name(name)  # Required step! TODO: Enforce this!

        except Exception as error:
            print('Error initializing NameAnalysisService: ' + repr(error))
            raise

        # Perform the name analysis - execute analysis using the supplied builder
        # @:return an array of ProcedureResult[]
        analysis = service.execute_analysis()

        # Build the appropriate response for the analysis result
        analysis_response = AnalysisResponse(service, analysis)
        payload = analysis_response.build_response().to_json()

        response = make_response(payload, 200)
        return response
Exemplo n.º 11
0
    def get():
        name = get_query_param_str('name')
        location = get_query_param_str('location')
        entity_type = get_query_param_str('entity_type_cd')
        request_action = get_query_param_str('request_action_cd')

        service = None

        if not validate_name_request(location, entity_type, request_action):
            return  # TODO: Return invalid response! What is it?

        valid_location = location == ValidLocations.CA_BC.value
        valid_protected_entity_type = entity_type in BCProtectedNameEntityTypes.list(
        )
        valid_unprotected_entity_type = entity_type in BCUnprotectedNameEntityTypes.list(
        )
        is_protected_action = request_action in [
            AnalysisRequestActions.NEW.value, AnalysisRequestActions.CHG.value,
            AnalysisRequestActions.MVE.value
        ]
        is_unprotected_action = request_action in [
            AnalysisRequestActions.NEW.value
        ]

        try:
            if valid_location and valid_protected_entity_type and is_protected_action:
                # Use ProtectedNameAnalysisService
                service = ProtectedNameAnalysisService()
                builder = NameAnalysisBuilder(service)

            elif valid_location and valid_unprotected_entity_type and is_unprotected_action:
                # Use UnprotectedNameAnalysisService
                service = UnprotectedNameAnalysisService()
                builder = NameAnalysisBuilder(service)

            else:
                raise Exception('Invalid scenario')

            if not service:
                raise ValueError('Invalid service provided')
            if not builder:
                raise ValueError('Invalid builder provided')

            # Register and initialize the builder
            service.use_builder(builder)  # Required step! TODO: Enforce this!
            service.set_entity_type(
                entity_type)  # Required step! TODO: Enforce this!
            service.set_name(name)  # Required step! TODO: Enforce this!

        except Exception as err:
            print('Error initializing BcNameAnalysis service: ' +
                  repr(err.with_traceback(None)))
            raise

        # Perform the name analysis - execute analysis using the supplied builder
        # @:return an array of ProcedureResult[]
        analysis = service.execute_analysis()

        # Build the appropriate response for the analysis result
        analysis_response = AnalysisResponse(service, analysis)
        payload = analysis_response.build_response().to_json()

        response = make_response(payload, 200)
        return response