def do_guess_normals(self, normals, top=1): if not normals: return [] smart_print(normals, "in guess normals") available = normals[0: top] normal_tags_dict = {} dict_from_items(normal_tags_dict, available) parents_list = [] for normal_item, value in available: parents_list.extend(self.memory_normal_items.normal_all_parents(normal_item.slug, value)) smart_print(parents_list, "parent_list") dict_from_items(normal_tags_dict, parents_list) top_normals = rank_dict(normal_tags_dict, top=1) for key, _ in normal_tags_dict.items(): relations = [] for top_normal, _ in top_normals: relation = top_normal == key or NormalItemEntity.direct_relation(key, top_normal) relations.append(relation) if not any(relations): del normal_tags_dict[key] return rank_dict(normal_tags_dict, top=top)
def do_guess_places(self, countries, cities, places, top=1): countries_dict = {} cities_dict = {} places_dict = {} dict_from_items(countries_dict, countries) dict_from_items(cities_dict, cities) dict_from_items(places_dict, places) smart_print(countries_dict, "countries_dict") smart_print(cities_dict, "cities_dict") smart_print(places_dict, "places_dict") top_country = rank_dict(countries_dict, top=1) available_countries = [country for country, _ in top_country] top_cities = rank_dict(cities_dict, top=top) top_city = [] for city, value in top_cities: relations = [] for available_country in available_countries: relation = PlaceItemEntity.direct_relation(city, available_country) relations.append(relation) if any(relations): top_city.append((city, value)) break available_cities = [city for city, _ in top_city] top_places = [] for place, value in places: relations = [] for city in available_cities: relation = PlaceItemEntity.direct_relation(place, city) relations.append(relation) if any(relations): top_places.append((place, value)) if not available_countries: return cities_dict.items(), cities_dict.items() if not top_places and not top_city: return top_country, [] else: result = [] result.extend(top_places[: top-1]) result.extend(top_city) return result, top_city