Ejemplo n.º 1
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 in [
            ValidLocations.CA_NOT_BC.value, ValidLocations.INTL.value
        ]
        valid_entity_type = entity_type in XproUnprotectedNameEntityTypes.list(
        )

        try:
            if valid_location and valid_entity_type and request_action in (
                    AnalysisRequestActions.NEW.value,
                    AnalysisRequestActions.AML.value,
                    AnalysisRequestActions.CHG.value,
                    AnalysisRequestActions.ASSUMED.value,
                    AnalysisRequestActions.REN.value,
                    AnalysisRequestActions.REH.value):

                # Use UnprotectedNameAnalysisService
                service = XproNameAnalysisService()
                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 XproNameAnalysis 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
Ejemplo n.º 2
0
    get_flat_list,
    remove_double_letters_list_dist_words,
    remove_spaces_list,
    subsequences,
)
from namex.services.name_request.auto_analyse.protected_name_analysis import ProtectedNameAnalysisService
from namex.services.name_request.builders.name_analysis_builder import NameAnalysisBuilder
from nltk.stem import PorterStemmer
from swagger_client import SynonymsApi as SynonymService

porter = PorterStemmer()

synonym_service = SynonymService()
name_processing_service = NameProcessingService()
name_analysis_service = ProtectedNameAnalysisService()
builder = NameAnalysisBuilder(name_analysis_service)

STEM_W = 0.85
SUBS_W = 0.65
OTHER_W_DESC = 3.0
OTHER_W_DIST = 0.35

EXACT_MATCH = 1.0
HIGH_SIMILARITY = 0.85
MEDIUM_SIMILARITY = 0.71
MINIMUM_SIMILARITY = 0.66

HIGH_CONFLICT_RECORDS = 20


# ok deep function
Ejemplo n.º 3
0
    def get():
        """Get structure analysis for a name."""
        name = get_query_param_str('name')
        allowed_special_chars = [
            '/', '[', ']', '^', '*', '+', '=', '&', '(', ')', ',', '”', '’',
            '#', '@', '!', '?', ';', ':'
        ]
        for special_char in allowed_special_chars:
            name = name.replace(special_char, ' ')
        location = get_query_param_str('location')
        entity_type = get_query_param_str('entity_type_cd')
        request_action = get_query_param_str('request_action_cd')
        designation_only = get_query_param_str('analysis_type') in [
            'designation', None
        ]
        # TODO: take out xpro flow entirely (not used in name analysis flow anymore)
        xpro = get_query_param_str('jurisdiction') not in ['BC', None]
        if xpro:
            return jsonify(
                message=['xpro not supported']), HTTPStatus.NOT_IMPLEMENTED
        service = None

        errors = bc_validate_name_request(location, entity_type,
                                          request_action)  # \
        # if not xpro else xpro_validate_name_request(location, entity_type, request_action)
        if errors:
            return jsonify(message=errors), HTTPStatus.BAD_REQUEST

        # # used for BC
        # valid_protected_entity_type = None
        # valid_unprotected_entity_type = None
        # is_protected_action = None
        # is_unprotected_action = None
        # # used for XPRO
        # valid_xpro_action = None
        # # used for both
        # valid_location = None
        # valid_entity_type = None
        # if xpro:
        #     valid_location = location in [ValidLocations.CA_NOT_BC.value, ValidLocations.INTL.value]
        #     valid_entity_type = entity_type in XproUnprotectedNameEntityTypes.list()
        #     valid_xpro_action = request_action in [
        #         AnalysisRequestActions.NEW.value,
        #         AnalysisRequestActions.AML.value,
        #         AnalysisRequestActions.CHG.value,
        #         AnalysisRequestActions.ASSUMED.value,
        #         AnalysisRequestActions.REN.value,
        #         AnalysisRequestActions.REH.value,
        #         AnalysisRequestActions.MVE.value
        #     ]
        # else:
        #     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 xpro:
            #     if valid_location and valid_entity_type and valid_xpro_action:
            #         service = ProtectedNameAnalysisService() \
            #             if request_action == AnalysisRequestActions.MVE.value else XproNameAnalysisService()
            #         builder = NameAnalysisBuilder(service)
            #     else:
            #         return jsonify(message=['Invalid scenario']), HTTPStatus.BAD_REQUEST

            # else:
            # if valid_location and valid_protected_entity_type:  # and is_protected_action:
            #  # Use ProtectedNameAnalysisService
            service = ProtectedNameAnalysisService()
            builder = NameAnalysisBuilder(service)
            # else:  # valid_location and valid_unprotected_entity_type:  # and is_unprotected_action:
            # Use UnprotectedNameAnalysisService
            # service = UnprotectedNameAnalysisService()
            # builder = NameAnalysisBuilder(service)
            # else:
            #     return jsonify(message=['Invalid scenario']), HTTPStatus.BAD_REQUEST

            if not service:
                return jsonify(message=['Failed to initialize service'
                                        ]), HTTPStatus.INTERNAL_SERVER_ERROR
            if not builder:
                return jsonify(message=['Failed to initialize builder'
                                        ]), HTTPStatus.INTERNAL_SERVER_ERROR

            # 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, designation_only)  # Required step! TODO: Enforce this!

        except Exception as err:
            current_app.logger.error(
                'Error initializing NameAnalysis 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(designation_only)

        # Build the appropriate response for the analysis result
        analysis_response = BcAnalysisResponse(service, analysis)  # \
        # if not xpro else XproAnalysisResponse(service, analysis)

        # Remove issues for end designation more than once if they are not duplicates
        valid_issues = []
        for issue in analysis_response.issues:
            if issue.issue_type == AnalysisIssueCodes.END_DESIGNATION_MORE_THAN_ONCE:
                valid_name_actions = []
                seen_designations = []
                for name_action in issue.name_actions:
                    if name_action.word in seen_designations:
                        valid_name_actions.append(name_action)
                    else:
                        seen_designations.append(name_action.word)
                if len(valid_name_actions) > 0:
                    issue.name_actions = valid_name_actions
                    valid_issues.append(issue)
                # else ^ this issue will not be added to the response
            else:
                valid_issues.append(issue)

        analysis_response.issues = valid_issues
        payload = analysis_response.build_response().to_json()
        response = make_response(payload, HTTPStatus.OK)
        return response
Ejemplo n.º 4
0
        sql = "select r.id, r.nr_num, r.state_cd, n.choice, n.name, n.decision_text, n.conflict1_num, n.conflict1, n.conflict1_num " \
              "from requests r, names n " \
              "where r.id = n.nr_id and n.state = " + '\'' + State.REJECTED + '\'' + " and " \
              "r.state_cd in(" + '\'' + State.APPROVED + '\',' + '\'' + State.CONDITIONAL + '\',' + '\'' + State.REJECTED + '\') and '\
              "r.request_type_cd = " + '\'' + EntityTypes.CORPORATION.value + '\'' + " and " \
              "r.nr_num not in (select nr_num from uat_results) " \
              "order by r.submitted_date " \
              "limit " + MAX_ROW_LIMIT

        requests = db.session.execute(sql)
        for request_id, nr_num, state_cd, choice, name, decision_text, conflict1_num, conflict1, conflict_num1 in requests:
            if entry_params['entity_type'] in BCProtectedNameEntityTypes.list():
                start_time_name = datetime.utcnow()
                service = ProtectedNameAnalysisService()
                builder = NameAnalysisBuilder(service)

                service.use_builder(builder)
                service.set_entity_type(entry_params.get('entity_type'))
                service.set_name(name)

                analysis = service.execute_analysis()

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

                profile_data = name_profile_data(nr_num, state_cd, choice, name, decision_text,
                                                 conflict1_num, conflict1)
                response_data = name_response_data(payload, duration=end_time_name - start_time_name)