From 380eec25913f32d72ae44d676c54b8fea03f8270 Mon Sep 17 00:00:00 2001 From: bilalq99 <133537558+bilalq99@users.noreply.github.com> Date: Tue, 23 Jul 2024 22:11:20 -0500 Subject: [PATCH 1/3] views.py --- parserator_web/views.py | 25 +++++++++++++++++++------ 1 file changed, 19 insertions(+), 6 deletions(-) diff --git a/parserator_web/views.py b/parserator_web/views.py index 0be3f4a9..a0f029f2 100644 --- a/parserator_web/views.py +++ b/parserator_web/views.py @@ -14,11 +14,24 @@ class AddressParse(APIView): renderer_classes = [JSONRenderer] def get(self, request): - # TODO: Flesh out this method to parse an address string using the - # parse() method and return the parsed components to the frontend. - return Response({}) + address_string = request.query_params.get('address', None) + if not address_string: + raise ParseError(detail="No address provided") + + address_components, address_type = self.parse(address_string) + + if address_components is None: + raise ParseError(detail="Invalid address format") + + return Response({ + "input_string": address_string, + "address_components": address_components, + "address_type": address_type + }) def parse(self, address): - # TODO: Implement this method to return the parsed components of a - # given address using usaddress: https://github.com/datamade/usaddress - return address_components, address_type + try: + parsed_address, address_type = usaddress.tag(address) + return parsed_address, address_type + except usaddress.RepeatedLabelError: + return None, None From 2e5b03ae19a5fbaf29263afdf2e081a6b5fb9009 Mon Sep 17 00:00:00 2001 From: bilalq99 <133537558+bilalq99@users.noreply.github.com> Date: Tue, 23 Jul 2024 22:12:59 -0500 Subject: [PATCH 2/3] index.html --- .../templates/parserator_web/index.html | 46 +++++++++++++++++-- 1 file changed, 42 insertions(+), 4 deletions(-) diff --git a/parserator_web/templates/parserator_web/index.html b/parserator_web/templates/parserator_web/index.html index a72d9c80..0dcf86e1 100644 --- a/parserator_web/templates/parserator_web/index.html +++ b/parserator_web/templates/parserator_web/index.html @@ -11,13 +11,13 @@
Dealing with some messy or unstructured addresses? We can parse them for you.
Try it out! Parse an address in the United States into fields like AddressNumber, StreetName and ZipCode.