Загрузить файлы в «/»

This commit is contained in:
2025-10-27 11:48:41 +03:00
commit ba53689ab4
5 changed files with 962 additions and 0 deletions

86
database.py Normal file
View File

@@ -0,0 +1,86 @@
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
for tag in tag_group:
if tag not in tag_mapping:
tag_mapping[tag] = {
'priority': priority_group_idx,
'tag_number': tag_counter
}
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'] 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)}")