Skip to content

Commit

Permalink
Merge pull request #389 from hellohaptik/develop
Browse files Browse the repository at this point in the history
Develop to Master Sync
  • Loading branch information
ankur09011 authored Jan 8, 2021
2 parents 91f4e74 + e23dc79 commit 4ca5de4
Show file tree
Hide file tree
Showing 2 changed files with 74 additions and 36 deletions.
109 changes: 73 additions & 36 deletions ner_v1/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
PARAMETER_READ_EMBEDDINGS_FROM_REMOTE_URL,
PARAMETER_LIVE_CRF_MODEL_PATH)
from django.views.decorators.csrf import csrf_exempt
from django.views.decorators.http import require_http_methods


def to_bool(value):
Expand Down Expand Up @@ -72,7 +73,7 @@ def get_parameters_dictionary(request):
PARAMETER_LIVE_CRF_MODEL_PATH: request.GET.get('live_crf_model_path'),
PARAMETER_PRIOR_RESULTS: json.loads(request.GET.get("predetected_values", '[]'))
}
ner_logger.info("parameters dict - {}".format(parameters_dict))
ner_logger.debug("parameters dict - {}".format(parameters_dict))
return parameters_dict


Expand Down Expand Up @@ -108,9 +109,34 @@ def parse_post_request(request):
PARAMETER_PRIOR_RESULTS: request_data.get("predetected_values", [])
}

ner_logger.debug("parameters dict - {}".format(parameters_dict))

return parameters_dict


def parse_parameters_from_request(request):
"""
Parse parameters from request based on the method type.
It will return a parameters dict.
Args:
request (django.http.HttpRequest): HTTP response from url
Returns:
dict: parameters from the request
"""
parameters_dict = {}
if request.method == "POST":
parameters_dict = parse_post_request(request)
ner_logger.debug('Start POST : %s ' % parameters_dict[PARAMETER_ENTITY_NAME])
elif request.method == "GET":
parameters_dict = get_parameters_dictionary(request)
ner_logger.debug('Start GET : %s ' % parameters_dict[PARAMETER_ENTITY_NAME])

return parameters_dict


@require_http_methods(["GET", "POST"])
@csrf_exempt
def text(request):
"""
Expand Down Expand Up @@ -230,13 +256,7 @@ def text(request):
]
"""
try:
parameters_dict = {}
if request.method == "POST":
parameters_dict = parse_post_request(request)
ner_logger.debug('Start Bulk Detection: %s ' % parameters_dict[PARAMETER_ENTITY_NAME])
elif request.method == "GET":
parameters_dict = get_parameters_dictionary(request)
ner_logger.debug('Start: %s ' % parameters_dict[PARAMETER_ENTITY_NAME])
parameters_dict = parse_parameters_from_request(request)
entity_output = get_text(
message=parameters_dict[PARAMETER_MESSAGE],
entity_name=parameters_dict[PARAMETER_ENTITY_NAME],
Expand All @@ -258,6 +278,8 @@ def text(request):
return HttpResponse(json.dumps({'data': entity_output}), content_type='application/json')


@require_http_methods(["GET", "POST"])
@csrf_exempt
def location(request):
"""This functionality calls the get_location() functionality to detect location. It is called through api call
Expand All @@ -266,8 +288,7 @@ def location(request):
"""
try:
parameters_dict = get_parameters_dictionary(request)
ner_logger.debug('Start: %s ' % parameters_dict[PARAMETER_ENTITY_NAME])
parameters_dict = parse_parameters_from_request(request)
entity_output = get_location(parameters_dict[PARAMETER_MESSAGE], parameters_dict[PARAMETER_ENTITY_NAME],
parameters_dict[PARAMETER_STRUCTURED_VALUE],
parameters_dict[PARAMETER_FALLBACK_VALUE],
Expand All @@ -281,6 +302,8 @@ def location(request):
return HttpResponse(json.dumps({'data': entity_output}), content_type='application/json')


@require_http_methods(["GET", "POST"])
@csrf_exempt
def phone_number(request):
"""This functionality calls the get_phone_number() functionality to detect phone numbers. It is called through
api call
Expand All @@ -290,8 +313,8 @@ def phone_number(request):
"""
try:
parameters_dict = get_parameters_dictionary(request)
ner_logger.debug('Start: %s ' % parameters_dict[PARAMETER_ENTITY_NAME])

parameters_dict = parse_parameters_from_request(request)
entity_output = get_phone_number(parameters_dict[PARAMETER_MESSAGE], parameters_dict[PARAMETER_ENTITY_NAME],
parameters_dict[PARAMETER_STRUCTURED_VALUE],
parameters_dict[PARAMETER_FALLBACK_VALUE],
Expand All @@ -304,6 +327,8 @@ def phone_number(request):
return HttpResponse(json.dumps({'data': entity_output}), content_type='application/json')


@require_http_methods(["GET", "POST"])
@csrf_exempt
def regex(request):
"""This functionality calls the get_regex() functionality to detect text those abide by the specified regex.
It is called through api call
Expand All @@ -313,8 +338,7 @@ def regex(request):
"""
try:
parameters_dict = get_parameters_dictionary(request)
ner_logger.debug('Start: %s ' % parameters_dict[PARAMETER_ENTITY_NAME])
parameters_dict = parse_parameters_from_request(request)
entity_output = get_regex(parameters_dict[PARAMETER_MESSAGE], parameters_dict[PARAMETER_ENTITY_NAME],
parameters_dict[PARAMETER_STRUCTURED_VALUE],
parameters_dict[PARAMETER_FALLBACK_VALUE],
Expand All @@ -328,6 +352,8 @@ def regex(request):
return HttpResponse(json.dumps({'data': entity_output}), content_type='application/json')


@require_http_methods(["GET", "POST"])
@csrf_exempt
def email(request):
"""This functionality calls the get_email() functionality to detect email. It is called through api call
Expand All @@ -336,8 +362,7 @@ def email(request):
"""
try:
parameters_dict = get_parameters_dictionary(request)
ner_logger.debug('Start: %s ' % parameters_dict[PARAMETER_ENTITY_NAME])
parameters_dict = parse_parameters_from_request(request)
entity_output = get_email(parameters_dict[PARAMETER_MESSAGE], parameters_dict[PARAMETER_ENTITY_NAME],
parameters_dict[PARAMETER_STRUCTURED_VALUE],
parameters_dict[PARAMETER_FALLBACK_VALUE],
Expand All @@ -350,6 +375,8 @@ def email(request):
return HttpResponse(json.dumps({'data': entity_output}), content_type='application/json')


@require_http_methods(["GET", "POST"])
@csrf_exempt
def person_name(request):
"""This functionality calls the get_name() functionality to detect name. It is called through api call
Expand All @@ -358,8 +385,7 @@ def person_name(request):
"""
try:
parameters_dict = get_parameters_dictionary(request)
ner_logger.debug('Start: %s ' % parameters_dict[PARAMETER_ENTITY_NAME])
parameters_dict = parse_parameters_from_request(request)
entity_output = get_person_name(message=parameters_dict[PARAMETER_MESSAGE],
entity_name=parameters_dict[PARAMETER_ENTITY_NAME],
structured_value=parameters_dict[PARAMETER_STRUCTURED_VALUE],
Expand All @@ -375,6 +401,8 @@ def person_name(request):
return HttpResponse(json.dumps({'data': entity_output}), content_type='application/json')


@require_http_methods(["GET", "POST"])
@csrf_exempt
def city(request):
"""This functionality calls the get_city() functionality to detect city. It is called through api call
Expand All @@ -383,8 +411,7 @@ def city(request):
"""
try:
parameters_dict = get_parameters_dictionary(request)
ner_logger.debug('Start: %s ' % parameters_dict[PARAMETER_ENTITY_NAME])
parameters_dict = parse_parameters_from_request(request)
entity_output = get_city(parameters_dict[PARAMETER_MESSAGE], parameters_dict[PARAMETER_ENTITY_NAME],
parameters_dict[PARAMETER_STRUCTURED_VALUE],
parameters_dict[PARAMETER_FALLBACK_VALUE],
Expand All @@ -399,6 +426,8 @@ def city(request):
return HttpResponse(json.dumps({'data': entity_output}), content_type='application/json')


@require_http_methods(["GET", "POST"])
@csrf_exempt
def pnr(request):
"""This functionality calls the get_pnr() functionality to detect pnr. It is called through api call
Expand All @@ -407,8 +436,7 @@ def pnr(request):
"""
try:
parameters_dict = get_parameters_dictionary(request)
ner_logger.debug('Start: %s ' % parameters_dict[PARAMETER_ENTITY_NAME])
parameters_dict = parse_parameters_from_request(request)
entity_output = get_pnr(parameters_dict[PARAMETER_MESSAGE], parameters_dict[PARAMETER_ENTITY_NAME],
parameters_dict[PARAMETER_STRUCTURED_VALUE],
parameters_dict[PARAMETER_FALLBACK_VALUE],
Expand All @@ -421,6 +449,8 @@ def pnr(request):
return HttpResponse(json.dumps({'data': entity_output}), content_type='application/json')


@require_http_methods(["GET", "POST"])
@csrf_exempt
def shopping_size(request):
"""This functionality calls the get_shopping_size() functionality to detect size. It is called through api call
Expand All @@ -429,8 +459,7 @@ def shopping_size(request):
"""
try:
parameters_dict = get_parameters_dictionary(request)
ner_logger.debug('Start: %s ' % parameters_dict[PARAMETER_ENTITY_NAME])
parameters_dict = parse_parameters_from_request(request)
entity_output = get_shopping_size(parameters_dict[PARAMETER_MESSAGE], parameters_dict[PARAMETER_ENTITY_NAME],
parameters_dict[PARAMETER_STRUCTURED_VALUE],
parameters_dict[PARAMETER_FALLBACK_VALUE],
Expand All @@ -443,6 +472,8 @@ def shopping_size(request):
return HttpResponse(json.dumps({'data': entity_output}), content_type='application/json')


@require_http_methods(["GET", "POST"])
@csrf_exempt
def number(request):
"""This functionality calls the get_numeric() functionality to detect numbers. It is called through api call
Expand All @@ -451,8 +482,7 @@ def number(request):
"""
try:
parameters_dict = get_parameters_dictionary(request)
ner_logger.debug('Start: %s ' % parameters_dict[PARAMETER_ENTITY_NAME])
parameters_dict = parse_parameters_from_request(request)
entity_output = get_number(parameters_dict[PARAMETER_MESSAGE], parameters_dict[PARAMETER_ENTITY_NAME],
parameters_dict[PARAMETER_STRUCTURED_VALUE],
parameters_dict[PARAMETER_FALLBACK_VALUE],
Expand All @@ -468,6 +498,8 @@ def number(request):
return HttpResponse(json.dumps({'data': entity_output}), content_type='application/json')


@require_http_methods(["GET", "POST"])
@csrf_exempt
def passenger_count(request):
"""This functionality calls the get_passenger_count() functionality to detect passenger count.
It is called through api call
Expand All @@ -480,8 +512,7 @@ def passenger_count(request):
"""
try:
parameters_dict = get_parameters_dictionary(request)
ner_logger.debug('Start: %s ' % parameters_dict[PARAMETER_ENTITY_NAME])
parameters_dict = parse_parameters_from_request(request)
entity_output = get_passenger_count(parameters_dict[PARAMETER_MESSAGE], parameters_dict[PARAMETER_ENTITY_NAME],
parameters_dict[PARAMETER_STRUCTURED_VALUE],
parameters_dict[PARAMETER_FALLBACK_VALUE],
Expand All @@ -495,6 +526,8 @@ def passenger_count(request):
return HttpResponse(json.dumps({'data': entity_output}), content_type='application/json')


@require_http_methods(["GET", "POST"])
@csrf_exempt
def time(request):
"""This functionality calls the get_time() functionality to detect time. It is called through api call
Expand All @@ -504,8 +537,7 @@ def time(request):
response (django.http.response.HttpResponse): HttpResponse object
"""
try:
parameters_dict = get_parameters_dictionary(request)
ner_logger.debug('Start: %s ' % parameters_dict[PARAMETER_ENTITY_NAME])
parameters_dict = parse_parameters_from_request(request)
entity_output = get_time(parameters_dict[PARAMETER_MESSAGE], parameters_dict[PARAMETER_ENTITY_NAME],
parameters_dict[PARAMETER_STRUCTURED_VALUE],
parameters_dict[PARAMETER_FALLBACK_VALUE],
Expand All @@ -519,6 +551,8 @@ def time(request):
return HttpResponse(json.dumps({'data': entity_output}), content_type='application/json')


@require_http_methods(["GET", "POST"])
@csrf_exempt
def time_with_range(request):
"""This functionality calls the get_time_with_range() functionality to detect time. It is called through api call
Expand All @@ -528,8 +562,7 @@ def time_with_range(request):
response (django.http.response.HttpResponse): HttpResponse object
"""
try:
parameters_dict = get_parameters_dictionary(request)
ner_logger.debug('Start: %s ' % parameters_dict[PARAMETER_ENTITY_NAME])
parameters_dict = parse_parameters_from_request(request)
entity_output = get_time_with_range(parameters_dict[PARAMETER_MESSAGE], parameters_dict[PARAMETER_ENTITY_NAME],
parameters_dict[PARAMETER_STRUCTURED_VALUE],
parameters_dict[PARAMETER_FALLBACK_VALUE],
Expand All @@ -543,6 +576,8 @@ def time_with_range(request):
return HttpResponse(json.dumps({'data': entity_output}), content_type='application/json')


@require_http_methods(["GET", "POST"])
@csrf_exempt
def date(request):
"""This functionality calls the get_date() functionality to detect date. It is called through api call
Expand All @@ -551,8 +586,7 @@ def date(request):
"""
try:
parameters_dict = get_parameters_dictionary(request)
ner_logger.debug('Start: %s ' % parameters_dict[PARAMETER_ENTITY_NAME])
parameters_dict = parse_parameters_from_request(request)
entity_output = get_date(parameters_dict[PARAMETER_MESSAGE], parameters_dict[PARAMETER_ENTITY_NAME],
parameters_dict[PARAMETER_STRUCTURED_VALUE],
parameters_dict[PARAMETER_FALLBACK_VALUE],
Expand All @@ -566,6 +600,8 @@ def date(request):
return HttpResponse(json.dumps({'data': entity_output}), content_type='application/json')


@require_http_methods(["GET", "POST"])
@csrf_exempt
def budget(request):
"""This functionality calls the get_budget() functionality to detect budget. It is called through api call
Expand All @@ -574,8 +610,7 @@ def budget(request):
"""
try:
parameters_dict = get_parameters_dictionary(request)
ner_logger.debug('Start: %s ' % parameters_dict[PARAMETER_ENTITY_NAME])
parameters_dict = parse_parameters_from_request(request)
entity_output = get_budget(parameters_dict[PARAMETER_MESSAGE], parameters_dict[PARAMETER_ENTITY_NAME],
parameters_dict[PARAMETER_STRUCTURED_VALUE],
parameters_dict[PARAMETER_FALLBACK_VALUE],
Expand All @@ -590,6 +625,7 @@ def budget(request):
return HttpResponse(json.dumps({'data': entity_output}), content_type='application/json')


@require_http_methods(["GET", "POST"])
def ner(request):
"""This functionality calls the run_ner() functionality to tag the message .
It is called through api call
Expand All @@ -609,6 +645,7 @@ def ner(request):
return HttpResponse(json.dumps({'data': output}), content_type='application/json')


@require_http_methods(["GET", "POST"])
def combine_output(request):
"""This functionality calls the combine_output_of_detection_logic_and_tag() through api call
Expand Down
1 change: 1 addition & 0 deletions ner_v2/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,7 @@ def parse_post_request(request):
PARAMETER_TIMEZONE: request_data.get('timezone'),
PARAMETER_LANGUAGE_SCRIPT: request_data.get('language_script', ENGLISH_LANG),
PARAMETER_SOURCE_LANGUAGE: request_data.get('source_language', ENGLISH_LANG),
PARAMETER_PAST_DATE_REFERENCED: request_data.get('date_past_reference', 'False'),
PARAMETER_MIN_DIGITS: request_data.get('min_number_digits'),
PARAMETER_MAX_DIGITS: request_data.get('max_number_digits'),
PARAMETER_NUMBER_UNIT_TYPE: request_data.get('unit_type'),
Expand Down

0 comments on commit 4ca5de4

Please sign in to comment.