Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ingest/apply-geolocation-rules: update general rules logic #55

Merged
merged 3 commits into from
Jun 20, 2022
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
42 changes: 25 additions & 17 deletions ingest/bin/apply-geolocation-rules
Original file line number Diff line number Diff line change
Expand Up @@ -114,23 +114,31 @@ def get_annotated_geolocation(geolocation_rules, raw_geolocation, rule_traversal
# If we've used all general rules and we still haven't found a match,
# then there are no applicable rules for this geolocation
if all(value == '*' for value in rule_traversal):
return None

# Find the index of the first of the consecutive '*' from the
# end of the rule_traversal
# [A, *, B, *] => first_consecutive_general_rule_index = 3
# [A, *, *, *] => first_consecutive_general_rule_index = 1
first_consecutive_general_rule_index = 0
for index, field_value in reversed(list(enumerate(rule_traversal))):
if field_value == '*':
first_consecutive_general_rule_index = index
else:
break

# Edit the raw value of the field directly before the first consecutive '*'
# in the rule traversal in hopes that by moving to a general rule on
# a higher level, we can find a matching rule.
rule_traversal[first_consecutive_general_rule_index - 1] = '*'
return None

# If we failed to find matching rule with a general rule as the last
# traversal target, then delete all trailing '*'s to reset rule_traversal
# to end with the last index that is currently NOT a '*'
# [A, *, B, *] => [A, *, B]
# [A, B, *, *] => [A, B]
# [A, *, *, *] => [A]
if rule_traversal[-1] == '*':
# Find the index of the first of the consecutive '*' from the
# end of the rule_traversal
# [A, *, B, *] => first_consecutive_general_rule_index = 3
# [A, B, *, *] => first_consecutive_general_rule_index = 2
# [A, *, *, *] => first_consecutive_general_rule_index = 1
for index, field_value in reversed(list(enumerate(rule_traversal))):
if field_value == '*':
first_consecutive_general_rule_index = index
else:
break

rule_traversal = rule_traversal[:first_consecutive_general_rule_index]

# Set the final value to '*' in hopes that by moving to a general rule,
# we can find a matching rule.
rule_traversal[-1] = '*'
victorlin marked this conversation as resolved.
Show resolved Hide resolved

return get_annotated_geolocation(geolocation_rules, raw_geolocation, rule_traversal)

Expand Down