def _resolve_category_for_room(klass, a_room, cats): """ Looks for texts in a_room that looks like a category identifier and assigns the cat_name attribute to a_room. If no category ID is found, the algorithm tries categories by their human names. Arguments: - a_room: a room dictionary with a "texts" key containing the list of texts to be analyzed. - cats: a dictionary indexed by category ID. Values represents the category human name. Example: { "AUL01" : "Aula" } Return value: True if a match is found, False otherwise. """ all_texts = [t["text"].strip().upper() for t in a_room["texts"]] # 1 - Look for exact category ids match_id = next((t for t in all_texts if t in cats), None) if match_id: a_room["cat_id"] = match_id return True # 2 - look for category name match match_name = next((t for t in all_texts if RoomCategory.get_cat_id_by_name(t)), None) if match_name: a_room["cat_id"] = RoomCategory.get_cat_id_by_name(match_name) return True return False
def _resolve_category_for_room(klass, a_room, cats): """ Looks for texts in a_room that looks like a category identifier and assigns the cat_name attribute to a_room. If no category ID is found, the algorithm tries categories by their human names. Arguments: - a_room: a room dictionary with a "texts" key containing the list of texts to be analyzed. - cats: a dictionary indexed by category ID. Values represents the category human name. Example: { "AUL01" : "Aula" } Return value: True if a match is found, False otherwise. """ all_texts = [ t["text"].strip().upper() for t in a_room["texts"] ] # 1 - Look for exact category ids match_id = next((t for t in all_texts if t in cats), None) if match_id: a_room["cat_id"] = match_id return True # 2 - look for category name match match_name = next((t for t in all_texts if RoomCategory.get_cat_id_by_name(t)), None) if match_name: a_room["cat_id"] = RoomCategory.get_cat_id_by_name(match_name) return True return False
def prepare_rooms(self, floor_id, rooms): """ Transform a list of rooms in a dictionary indexed by room id. Arguments: - floor_id: a string representing the floor identifier, - rooms: a list of rooms. Returns: a dictionary of rooms. Validate the r_id using Building.is_valid_rid function and discard rooms with invalid id. Create and return a dictionary of validated rooms. """ result = {} discarded_rooms = set() for r in map(self.sanitize_room, rooms): if not Building.is_valid_rid(r["r_id"]): discarded_rooms.add(r["r_id"]) continue if "cat_id" in r: r["cat_id"] = RoomCategory.get_cat_id_by_name(r.get("cat_name", "")) del r["cat_name"] r_id = r["r_id"] del r["r_id"] result[r_id] = r if discarded_rooms: Logger.warning( "Rooms discarded from floor", floor_id, "for having an invalid room id:", ", ".join(discarded_rooms) ) return result
def prepare_rooms(self, floor_id, rooms): """ Transform a list of rooms in a dictionary indexed by room id. Arguments: - floor_id: a string representing the floor identifier, - rooms: a list of rooms. Returns: a dictionary of rooms. Validate the r_id using Building.is_valid_rid function and discard rooms with invalid id. Create and return a dictionary of validated rooms. """ result = {} discarded_rooms = set() for r in map(self.sanitize_room, rooms): if not Building.is_valid_rid(r["r_id"]): discarded_rooms.add(r["r_id"]) continue if "cat_id" in r: r["cat_id"] = RoomCategory.get_cat_id_by_name( r.get("cat_name", "")) del r["cat_name"] r_id = r["r_id"] del r["r_id"] result[r_id] = r if discarded_rooms: Logger.warning("Rooms discarded from floor", floor_id, "for having an invalid room id:", ", ".join(discarded_rooms)) return result