This repository has been archived by the owner on Feb 2, 2022. It is now read-only.
Parse more deceased field formats #90
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -68,7 +68,6 @@ async def fetch_detail_page(session, url): | |
:return: the page content. | ||
:rtype: str | ||
""" | ||
|
||
return await fetch_text(session, url) | ||
|
||
|
||
|
@@ -286,46 +285,140 @@ def parse_name(name): | |
return d | ||
|
||
|
||
def parse_deceased_field(deceased_field): | ||
def dob_search(split_deceased_field): | ||
""" | ||
Parse the deceased field. | ||
|
||
At this point the deceased field, if it exists, is garbage as it contains First Name, Last Name, Ethnicity, | ||
Gender, D.O.B. and Notes. We need to explode this data into the appropriate fields. | ||
Search for the DOB in a deceased field. | ||
|
||
:param str deceased_field: the deceased field from the fatality report | ||
:return: a dictionary representing a deceased field. | ||
:rtype: dict | ||
:param list split_deceased_field: a list representing the deceased field | ||
:return: the DOB index within the split deceased field. | ||
:rtype: int | ||
""" | ||
deceased_field = re.split(r' |(?<=[A-Za-z])/', deceased_field) | ||
dob_index = -1 | ||
dob_tokens = [Fields.DOB, '(D.O.B', '(D.O.B.', '(D.O.B:', '(DOB', '(DOB:', 'D.O.B.', 'DOB:'] | ||
while dob_index < 0 and dob_tokens: | ||
dob_token = dob_tokens.pop() | ||
try: | ||
dob_index = deceased_field.index(dob_token) | ||
dob_index = split_deceased_field.index(dob_token) | ||
except ValueError: | ||
pass | ||
else: | ||
break | ||
|
||
if dob_index < 0: | ||
raise ValueError(f'Cannot parse {Fields.DECEASED}: {deceased_field}') | ||
return dob_index | ||
|
||
|
||
def parse_deceased_field(deceased_field): | ||
""" | ||
Parse the deceased field. | ||
|
||
At this point the deceased field, if it exists, is garbage as it contains First Name, Last Name, Ethnicity, | ||
Gender, D.O.B. and Notes. We need to explode this data into the appropriate fields. | ||
|
||
:param str deceased_field: the deceased field from the fatality report | ||
:return: a dictionary representing a deceased field. | ||
:rtype: dict | ||
""" | ||
# Try to parse the deceased fields when the fields are comma separated. | ||
try: | ||
return parse_comma_delimited_deceased_field(deceased_field) | ||
except Exception: | ||
pass | ||
|
||
# Try to parse the deceased fields when the fields are pipe separated. | ||
try: | ||
return parse_pipe_delimited_deceased_field(deceased_field) | ||
except Exception: | ||
pass | ||
|
||
try: | ||
return parse_space_delimited_deceased_field(deceased_field) | ||
except Exception: | ||
pass | ||
|
||
raise ValueError(f'Cannot parse {Fields.DECEASED}: {deceased_field}') | ||
|
||
|
||
def parse_comma_delimited_deceased_field(deceased_field): | ||
"""Parse deceased fields seperated with commas. | ||
|
||
:param list split_deceased_field: a list representing the deceased field | ||
:return: a dictionary representing the deceased field. | ||
:rtype: dict | ||
""" | ||
d = {} | ||
d[Fields.DOB] = deceased_field[dob_index + 1] | ||
notes = deceased_field[dob_index + 2:] | ||
split_deceased_field = re.split(r' |(?<=[A-Za-z])/', deceased_field) | ||
dob_index = dob_search(split_deceased_field) | ||
if dob_index < 0: | ||
raise ValueError(f'Cannot find DOB in the deceased field: {deceased_field}') | ||
raw_dob = split_deceased_field[dob_index + 1] | ||
validated_dob = date_utils.clean_date_string(raw_dob, True) | ||
d[Fields.DOB] = validated_dob | ||
notes = split_deceased_field[dob_index + 2:] | ||
if notes: | ||
d[Fields.NOTES] = ' '.join(notes) | ||
|
||
# `fleg` stands for First, Last, Ethnicity, Gender. It represents the info stored before the DOB. | ||
fleg = deceased_field[:dob_index] | ||
fleg = split_deceased_field[:dob_index] | ||
d.update(parse_fleg(fleg)) | ||
return d | ||
|
||
|
||
def parse_pipe_delimited_deceased_field(deceased_field): | ||
""" | ||
Parse deceased fields separated with pipes. | ||
|
||
:param str deceased_field: the deceased field as a string. | ||
:return: a dictionary representing the deceased field. | ||
:rtype: dict | ||
""" | ||
d = {} | ||
split_deceased_field = deceased_field.split('|') | ||
raw_dob = split_deceased_field[-1].strip() | ||
d[Fields.DOB] = date_utils.clean_date_string(raw_dob, True) | ||
|
||
fleg = (split_deceased_field[0] + split_deceased_field[1]).split() | ||
d.update(parse_fleg(fleg)) | ||
return d | ||
|
||
|
||
def parse_space_delimited_deceased_field(deceased_field): | ||
""" | ||
Parse deceased fields separated with spaces. | ||
|
||
:param str deceased_field: the deceased field as a string. | ||
:return: a dictionary representing the deceased field. | ||
:rtype: dict | ||
""" | ||
d = {} | ||
split_deceased_field = re.split(r' |/', deceased_field) | ||
raw_dob = split_deceased_field[-1].strip() | ||
d[Fields.DOB] = date_utils.clean_date_string(raw_dob, True) | ||
|
||
fleg = split_deceased_field[:-1] | ||
d.update(parse_fleg(fleg)) | ||
return d | ||
|
||
|
||
def parse_fleg(fleg): | ||
""" | ||
Parse FLEG. | ||
|
||
:param list fleg: [description] | ||
:return: [description] | ||
:rtype: dict | ||
""" | ||
# Try to pop out the results one by one. If pop fails, it means there is nothing left to retrieve, | ||
# For example, there is no first name and last name. | ||
d = {} | ||
try: | ||
d[Fields.GENDER] = fleg.pop().replace(',', '').lower() | ||
if d.get(Fields.GENDER) == 'f': | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Didn't even realize there were f's and m's...good catch |
||
d[Fields.GENDER] = 'female' | ||
elif d.get(Fields.GENDER) == 'm': | ||
d[Fields.GENDER] = 'male' | ||
|
||
d[Fields.ETHNICITY] = fleg.pop().replace(',', '') | ||
if d.get(Fields.ETHNICITY) == 'W': | ||
d[Fields.ETHNICITY] = 'White' | ||
except IndexError: | ||
pass | ||
|
||
|
@@ -350,7 +443,7 @@ def parse_page_content(detail_page, notes_parsed=False): | |
(Fields.CASE, re.compile(r'Case:.*\s([0-9\-]+)<')), | ||
(Fields.CRASHES, re.compile(r'Traffic Fatality #(\d{1,3})')), | ||
(Fields.DATE, re.compile(r'>Date:.*\s{2,}([^<]*)</')), | ||
(Fields.DECEASED, re.compile(r'>Deceased:.*\s{2,}([^<]*\d)\)?<')), | ||
(Fields.DECEASED, re.compile(r'>Deceased:\s*(?:</span>)?(?:</strong>)?\s*>?([^<]*\d)\s*.*\)?<')), | ||
(Fields.LOCATION, re.compile(r'>Location:.*>\s{2,}([^<]+)')), | ||
(Fields.TIME, re.compile(r'>Time:.*>\s{2,}([^<]+)')), | ||
] | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
So much delimiter support now 🎉 A lot of similar code between pipe and space, maybe these could be one function in the future with a delimiter parameter.