Files
gorkycode-2025/input-to-route/database.py
Nikidze e3f8caf59f final
2025-10-31 22:08:55 +03:00

95 lines
3.3 KiB
Python
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

import json
def search_database(database_file, query):
"""
Поиск по базе данных с группировкой тегов по приоритетам.
Args:
database: список словарей с данными
query: список списков тегов, например [["Памятник"], ["Архитектура"]]
Returns:
список найденных записей с добавленными полями priority и измененным category_id
"""
with open(database_file, 'r', encoding='utf-8') as f:
database = json.load(f)
tag_mapping = {}
tag_counter = 1
# Создаем маппинг тегов
any_mode = False
for priority_group_idx, tag_group in enumerate(query, start=1):
if any_mode:
tag_counter+=1
if 'ANY' in tag_group:
tag_group.remove('ANY')
any_mode = True
else:
any_mode = False
if 'MULTI' in tag_group:
tag_group.remove('MULTI')
multi_mode = True
else:
multi_mode = False
for tag in tag_group:
if tag not in tag_mapping:
tag_mapping[tag] = {
'priority': priority_group_idx,
'tag_number': tag_counter,
'multi': multi_mode
}
if not any_mode:
tag_counter += 1
# Поиск и обработка записей
results = []
seen_ids = set()
for entry in database:
categories = entry.get('category_id')
categories =list(categories.split(', '))
found = False
for ctg in categories:
if ctg in tag_mapping:
found = True
category= ctg
break
if found:
entry_id = (entry.get('coordinate'), entry.get('title'))
if entry_id not in seen_ids:
seen_ids.add(entry_id)
result_entry = entry.copy()
result_entry['type'] = tag_mapping[category]['tag_number']
result_entry['priority'] = tag_mapping[category]['priority']
results.append(result_entry)
# Сортируем по приоритету
tag_priority = {v['tag_number']: (v['priority'],v['multi']) for v in tag_mapping.values()}
# Then verify no conflicts
if len(set((v['tag_number'], v['priority']) for v in tag_mapping.values())) != len(tag_priority):
raise ValueError("Conflicting priorities for same tag_number")
return results,tag_priority
# Пример использования
if __name__ == "__main__":
# Загрузка базы данных из файла
with open('output.json', 'r', encoding='utf-8') as f:
database = json.load(f)
# Запрос с группами тегов
query = [["Памятник", "Музей"], ["Архитектура"], ["Парк", "Сквер"]]
# Выполнение поиска
results = search_database(database, query)
print(results)
# Сохранение результатов
with open('search_results.json', 'w', encoding='utf-8') as f:
json.dump(results, f, ensure_ascii=False, indent=2)
print(f"Найдено записей: {len(results)}")