-
Notifications
You must be signed in to change notification settings - Fork 43
Add fetch_entity_names
method
#230
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
Merged
Merged
Changes from 1 commit
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
92223a7
Fetch names from properties
jm-rivera bdd0535
Merge branch 'master' into add-fetch-names
jm-rivera 8c27cf8
property constants and default changes
jm-rivera 6ead002
Merge branch 'master' into add-fetch-names
jm-rivera bb8c0a3
A more complete return object
jm-rivera 39ce7f1
Add `Name` class
jm-rivera 44d78a2
Merge remote-tracking branch 'upstream/master' into add-fetch-names
jm-rivera File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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 |
---|---|---|
@@ -0,0 +1,65 @@ | ||
from datacommons_client.models.node import Node | ||
from datacommons_client.utils.names import extract_name_from_english_name_property | ||
from datacommons_client.utils.names import extract_name_from_property_with_language | ||
|
||
|
||
def test_extract_name_from_english_name_property_with_list(): | ||
"""Test extracting name from a list of Nodes.""" | ||
properties = [Node(value="Test Name")] | ||
result = extract_name_from_english_name_property(properties) | ||
assert result == "Test Name" | ||
|
||
|
||
def test_extract_name_from_english_not_list(): | ||
"""Test extracting name from a single Node (not in a list).""" | ||
property_node = Node(value="Single Node Name") | ||
result = extract_name_from_english_name_property(property_node) | ||
assert result == "Single Node Name" | ||
|
||
|
||
def test_extract_name_from_property_with_language_match(): | ||
"""Test extracting name when desired language is present.""" | ||
properties = [ | ||
Node(value="Nombre@es"), | ||
Node(value="Name@en"), | ||
] | ||
result = extract_name_from_property_with_language(properties, | ||
language="es", | ||
fallback_to_en=True) | ||
assert result == "Nombre" | ||
|
||
|
||
def test_extract_name_from_property_with_language_fallback(): | ||
"""Test fallback to English when desired language is not found.""" | ||
properties = [ | ||
Node(value="Name@en"), | ||
Node(value="Nom@fr"), | ||
] | ||
result = extract_name_from_property_with_language(properties, | ||
language="de", | ||
fallback_to_en=True) | ||
assert result == "Name" | ||
|
||
|
||
def test_extract_name_from_property_with_language_no_fallback(): | ||
"""Test no result when language is not found and fallback is disabled.""" | ||
properties = [ | ||
Node(value="Name@en"), | ||
Node(value="Nom@fr"), | ||
] | ||
result = extract_name_from_property_with_language(properties, | ||
language="de", | ||
fallback_to_en=False) | ||
assert result is None | ||
|
||
|
||
def test_extract_name_from_property_without_language_tags(): | ||
"""Test that properties without language tags are skipped.""" | ||
properties = [ | ||
Node(value="Plain str"), | ||
Node(value="Name@en"), | ||
] | ||
result = extract_name_from_property_with_language(properties, | ||
language="en", | ||
fallback_to_en=False) | ||
assert result == "Name" |
This file contains hidden or 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 |
---|---|---|
@@ -0,0 +1,50 @@ | ||
from typing import Optional | ||
|
||
from datacommons_client.models.node import Node | ||
|
||
|
||
def extract_name_from_english_name_property(properties: list | Node) -> str: | ||
""" | ||
Extracts the name from a list of properties with English names. | ||
Args: | ||
properties (list): A list of properties with English names. | ||
Returns: | ||
str: The extracted name. | ||
""" | ||
if isinstance(properties, Node): | ||
properties = [properties] | ||
|
||
return properties[0].value | ||
|
||
|
||
def extract_name_from_property_with_language( | ||
properties: list, language: str, fallback_to_en: bool) -> Optional[str]: | ||
""" | ||
Extracts the name from a list of properties with language tags. | ||
Args: | ||
properties (list): A list of properties with language tags. | ||
language (str): The desired language code. | ||
fallback_to_en (bool): Whether to fall back to English if the desired language is not found. | ||
""" | ||
# If a non-English language is requested, unpack the response to get it. | ||
fallback_name = None | ||
|
||
# Iterate through the properties to find the name in the specified language | ||
for candidate in properties: | ||
# If no language is specified, skip the candidate | ||
if "@" not in candidate.value: | ||
continue | ||
|
||
# Split the candidate value into name and language | ||
name, lang = candidate.value.rsplit("@", 1) | ||
|
||
# If the language matches, add the name to the dictionary. | ||
if lang == language: | ||
return name | ||
# If language is 'en', store the name as a fallback | ||
if lang == "en": | ||
fallback_name = name | ||
|
||
# If no name was found in the specified language, use the fallback name (if available and | ||
# fallback_to_en is True) | ||
return fallback_name if fallback_to_en else None |
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.
Uh oh!
There was an error while loading. Please reload this page.