Beispiel #1
0
async def private_service():
    """Return the outcome of this private service call."""
    name_analysis_service = ProtectedNameAnalysisService()
    service = name_analysis_service
    np_svc_prep_data = service.name_processing_service
    np_svc_prep_data.prepare_data()

    json_data = await request.get_json()
    list_dist = json_data.get('list_dist')
    list_desc = json_data.get('list_desc')
    list_name = json_data.get('list_name')
    dict_substitution = json_data.get('dict_substitution')
    dict_synonyms = json_data.get('dict_synonyms')
    matches = json_data.get('names')

    app.logger.debug('Number of matches: {0}'.format(len(matches)))

    result = await asyncio.gather(*[
        auto_analyze(name, list_name, list_dist, list_desc, dict_substitution,
                     dict_synonyms, np_svc_prep_data) for name in matches
    ])
    return jsonify(result=result)
Beispiel #2
0
    get_classification,
    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

Beispiel #3
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
    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
Beispiel #5
0
import re

import pytest
from hamcrest import assert_that

from namex.services.name_request.auto_analyse.protected_name_analysis import ProtectedNameAnalysisService
from namex.services.name_request.name_analysis_builder_v2.name_analysis_builder import NameAnalysisBuilder
from namex.services.synonyms.synonym import SynonymService

service = ProtectedNameAnalysisService()
builder = NameAnalysisBuilder(service)
service.use_builder(builder)
syn_svc = SynonymService()
all_designations = [
    'community contribution company', 'limited liability partnership',
    'unlimited liability company', 'limited liability company',
    'limited liability co.', 'limited partnership', 'co-operative',
    'incorporated', 'corporation', 'cooperative', 'liability', 'company',
    'limited', 'l.l.c.', 'co-op', 'corp.', 'l.l.c', 'corp', 'ltd.', 'coop',
    'ulc.', 'inc.', 'ccc', 'co.', 'llc', 'ulc', 'ltd', 'inc', 'llp', 'co'
]
prefix_list = [
    'un', 're', 'in', 'dis', 'en', 'non', 'in', 'over', 'mis', 'sub', 'pre',
    'inter', 'fore', 'de', 'trans', 'super', 'semi', 'anti', 'mid', 'under',
    'ante', 'bene', 'circum', 'co', 'com', 'con', 'col', 'dia', 'ex', 'h**o',
    'hyper', 'mal', 'micro', 'multi', 'para', 'poly', 'post', 'pro', 'retro',
    'tele', 'therm', 'trans', 'uni'
]
number_list = [
    'nine hundred', 'one hundred', 'eleventh', 'eleven', 'second', 'three',
    'first', 'third', 'eight', 'five', 'nine', 'one', 'two'
    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
    try:

        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)