Skip to content

Commit

Permalink
[IMPR] decrease nested flow statements in create_isbn_edition.py
Browse files Browse the repository at this point in the history
Change-Id: I6cae4dd409b513a8b6e22942ac5259ed55ed1bee
  • Loading branch information
xqt committed Nov 19, 2024
1 parent d00c515 commit 01db35c
Showing 1 changed file with 24 additions and 23 deletions.
47 changes: 24 additions & 23 deletions scripts/create_isbn_edition.py
Original file line number Diff line number Diff line change
Expand Up @@ -782,8 +782,7 @@ def get_item_list(item_name: str,
:return: Set of items
"""
pywikibot.debug(f'Search label: {item_name.encode("utf-8")}')
item_list = set() # Empty set
# TODO: try to us search_entities instead?
# TODO: try to use search_entities instead?
params = {
'action': 'wbsearchentities',
'format': 'json',
Expand All @@ -799,31 +798,33 @@ def get_item_list(item_name: str,
result = request.submit()
pywikibot.debug(result)

if 'search' in result:
# Ignore accents and case
item_name_canon = unidecode(item_name).casefold()
if 'search' not in result:
return set()

# Loop though items
for res in result['search']:
item = get_item_page(res['id'])
# Ignore accents and case
item_name_canon = unidecode(item_name).casefold()

# Matching instance
if INSTANCEPROP not in item.claims \
or not item_is_in_list(item.claims[INSTANCEPROP], instance_id):
continue
item_list = set()
# Loop though items
for res in result['search']:
item = get_item_page(res['id'])

# Search all languages, ignore label case and accents
for lang in item.labels:
if (item_name_canon
== unidecode(item.labels[lang].casefold())):
item_list.add(item.getID()) # Label math
break
# Matching instance
if INSTANCEPROP not in item.claims \
or not item_is_in_list(item.claims[INSTANCEPROP], instance_id):
continue

for lang in item.aliases:
for seq in item.aliases[lang]:
if item_name_canon == unidecode(seq).casefold():
item_list.add(item) # Alias match
break
# Search all languages, ignore label case and accents
for lang in item.labels:
if item_name_canon == unidecode(item.labels[lang].casefold()):
item_list.add(item.getID()) # Label math
break

for lang in item.aliases:
for seq in item.aliases[lang]:
if item_name_canon == unidecode(seq).casefold():
item_list.add(item) # Alias match
break

pywikibot.log(item_list)
return item_list
Expand Down

0 comments on commit 01db35c

Please sign in to comment.