<commit_before>#include "autocomplete_api.h"
#include "type/pb_converter.h"

namespace navitia { namespace autocomplete {
/**
 * se charge de remplir l'objet protocolbuffer autocomplete passé en paramètre
 *
 */
void create_pb(const std::vector<Autocomplete<nt::idx_t>::fl_quality>& result,
               const nt::Type_e type, uint32_t depth, const nt::Data& data,
               pbnavitia::Response & pb_response){
    for(auto result_item : result){
        pbnavitia::Place* place = pb_response.add_places();
        switch(type){
        case nt::Type_e::StopArea:
            fill_pb_placemark(data.pt_data.stop_areas[result_item.idx], data,place, depth);
            place->set_quality(result_item.quality);
            break;
        case nt::Type_e::Admin:
            fill_pb_placemark(data.geo_ref.admins[result_item.idx], data, place, depth);
            place->set_quality(result_item.quality);
            break;
        case nt::Type_e::StopPoint:
            fill_pb_placemark(data.pt_data.stop_points[result_item.idx], data, place, depth);
            place->set_quality(result_item.quality);
            break;
        case nt::Type_e::Address:
            fill_pb_placemark(data.geo_ref.ways[result_item.idx], data, place, result_item.house_number, result_item.coord, depth);
            place->set_quality(result_item.quality);
            break;
        case nt::Type_e::POI:
            fill_pb_placemark(data.geo_ref.pois[result_item.idx], data, place, depth);
            place->set_quality(result_item.quality);
            break;
        case nt::Type_e::Line:
            fill_pb_object(data.pt_data.lines[result_item.idx], data,
                    place->mutable_line(), depth);
            place->set_name(data.pt_data.lines[result_item.idx]->name);
            place->set_uri(data.pt_data.lines[result_item.idx]->uri);
            place->set_quality(result_item.quality);
            place->set_embedded_type(pbnavitia::LINE);
        default:
            break;
        }
    }
}

int penalty_by_type(type::Type_e ntype, bool is_address_type) {
    // Ordre de tri :
    // Add, SA, POI, SP, Admin : si présence de addressType dans le recherche
    // Admin, SA, POI, Add, SP : si non
    if(is_address_type) {
        const std::map<type::Type_e, uint32_t> penalty_map =
        {
            {nt::Type_e::Admin, 8},
            {nt::Type_e::StopArea, 2},
            {nt::Type_e::POI, 4},
            {nt::Type_e::Address, 0},
            {nt::Type_e::StopPoint, 6},
        };
        auto result = penalty_map.find(ntype);
        return result == penalty_map.end() ? 0 : result->second;
    } else {
        const std::map<type::Type_e, uint32_t> penalty_map =
        {
            {nt::Type_e::Admin, 0},
            {nt::Type_e::StopArea, 2},
            {nt::Type_e::POI, 6},
            {nt::Type_e::Address, 8},
            {nt::Type_e::StopPoint, 4},
        };
        auto result = penalty_map.find(ntype);
        return result == penalty_map.end() ? 0 : result->second;
    }
}


///Mettre à jour la qualité sur le poids de POI
void update_quality_by_poi_type(std::vector<Autocomplete<nt::idx_t>::fl_quality>& ac_result,
        const navitia::type::Data &d){
    for(auto &item : ac_result){
        int poi_weight = 0;
        poi_weight = item.quality + d.geo_ref.pois[item.idx]->weight * 2;
        item.quality = std::min(poi_weight, 100);
    }
}


///Ajouter une pénalité aux objects sans admin par au moin d.geo_ref.word_weight
void update_quality_for_missing_admin(std::vector<Autocomplete<nt::idx_t>::fl_quality>& ac_result,
        const navitia::type::Data &d, navitia::type::Type_e ntype){
    const int penalty = d.geo_ref.word_weight * 2;
    for (auto &item : ac_result){
        bool apply_penalty = false;
        switch(ntype){
        case navitia::type::Type_e::StopArea:
            apply_penalty = !d.pt_data.stop_areas[item.idx]->admin_list.empty();
            break;
        case navitia::type::Type_e::POI:
            apply_penalty = !d.geo_ref.pois[item.idx]->admin_list.empty();
            break;
        case navitia::type::Type_e::Address:
            apply_penalty = !d.geo_ref.ways[item.idx]->admin_list.empty();
            break;
        case navitia::type::Type_e::StopPoint:
            apply_penalty = !d.pt_data.stop_points[item.idx]->admin_list.empty();
            break;
        default:
            break;
        }
        if(apply_penalty)
            item.quality -= penalty;
    }
}

void update_quality(std::vector<Autocomplete<nt::idx_t>::fl_quality>& ac_result, navitia::type::Type_e ntype,
                    bool Is_address_type,
                    const navitia::type::Data &d){
    //Mettre à jour la qualité sur la pénalité par type d'adresse
    const int penalty = penalty_by_type(ntype, Is_address_type);
    for(auto &item : ac_result) {
        item.quality -= penalty;
    }

    //Mettre à jour la qualité sur le poids de POI
    //Cette méthode n'est pas utilisé en absence du poids sur poi-type.
//    if (ntype ==navitia::type::Type_e::POI){<commit_msg>places: Objects without city highly penalized<commit_after>#include "autocomplete_api.h"
#include "type/pb_converter.h"

namespace navitia { namespace autocomplete {
/**
 * se charge de remplir l'objet protocolbuffer autocomplete passé en paramètre
 *
 */
void create_pb(const std::vector<Autocomplete<nt::idx_t>::fl_quality>& result,
               const nt::Type_e type, uint32_t depth, const nt::Data& data,
               pbnavitia::Response & pb_response){
    for(auto result_item : result){
        pbnavitia::Place* place = pb_response.add_places();
        switch(type){
        case nt::Type_e::StopArea:
            fill_pb_placemark(data.pt_data.stop_areas[result_item.idx], data,place, depth);
            place->set_quality(result_item.quality);
            break;
        case nt::Type_e::Admin:
            fill_pb_placemark(data.geo_ref.admins[result_item.idx], data, place, depth);
            place->set_quality(result_item.quality);
            break;
        case nt::Type_e::StopPoint:
            fill_pb_placemark(data.pt_data.stop_points[result_item.idx], data, place, depth);
            place->set_quality(result_item.quality);
            break;
        case nt::Type_e::Address:
            fill_pb_placemark(data.geo_ref.ways[result_item.idx], data, place, result_item.house_number, result_item.coord, depth);
            place->set_quality(result_item.quality);
            break;
        case nt::Type_e::POI:
            fill_pb_placemark(data.geo_ref.pois[result_item.idx], data, place, depth);
            place->set_quality(result_item.quality);
            break;
        case nt::Type_e::Line:
            fill_pb_object(data.pt_data.lines[result_item.idx], data,
                    place->mutable_line(), depth);
            place->set_name(data.pt_data.lines[result_item.idx]->name);
            place->set_uri(data.pt_data.lines[result_item.idx]->uri);
            place->set_quality(result_item.quality);
            place->set_embedded_type(pbnavitia::LINE);
        default:
            break;
        }
    }
}

int penalty_by_type(type::Type_e ntype, bool is_address_type) {
    // Ordre de tri :
    // Add, SA, POI, SP, Admin : si présence de addressType dans le recherche
    // Admin, SA, POI, Add, SP : si non
    if(is_address_type) {
        const std::map<type::Type_e, uint32_t> penalty_map =
        {
            {nt::Type_e::Admin, 8},
            {nt::Type_e::StopArea, 2},
            {nt::Type_e::POI, 4},
            {nt::Type_e::Address, 0},
            {nt::Type_e::StopPoint, 6},
        };
        auto result = penalty_map.find(ntype);
        return result == penalty_map.end() ? 0 : result->second;
    } else {
        const std::map<type::Type_e, uint32_t> penalty_map =
        {
            {nt::Type_e::Admin, 0},
            {nt::Type_e::StopArea, 2},
            {nt::Type_e::POI, 6},
            {nt::Type_e::Address, 8},
            {nt::Type_e::StopPoint, 4},
        };
        auto result = penalty_map.find(ntype);
        return result == penalty_map.end() ? 0 : result->second;
    }
}


///Mettre à jour la qualité sur le poids de POI
void update_quality_by_poi_type(std::vector<Autocomplete<nt::idx_t>::fl_quality>& ac_result,
        const navitia::type::Data &d){
    for(auto &item : ac_result){
        int poi_weight = 0;
        poi_weight = item.quality + d.geo_ref.pois[item.idx]->weight * 2;
        item.quality = std::min(poi_weight, 100);
    }
}

bool has_admin_level8(const std::vector<georef::Admin*>& admins){
    bool admin_level8 = false;
    for(const navitia::georef::Admin* admin : admins){
        if (admin->level == 8){
            admin_level8 = true;
        }
    }
    return admin_level8;
}


///Ajouter une pénalité aux objects sans admin par au moin d.geo_ref.word_weight
void update_quality_for_missing_admin(std::vector<Autocomplete<nt::idx_t>::fl_quality>& ac_result,
        const navitia::type::Data &d, navitia::type::Type_e ntype){
    const int penalty = d.geo_ref.word_weight * 8;
    for (auto &item : ac_result){
        bool apply_penalty = false;
        switch(ntype){
        case navitia::type::Type_e::StopArea:
            apply_penalty = !has_admin_level8(d.pt_data.stop_areas[item.idx]->admin_list);
            break;
        case navitia::type::Type_e::POI:
            apply_penalty = !has_admin_level8(d.geo_ref.pois[item.idx]->admin_list);
            break;
        case navitia::type::Type_e::Address:
            apply_penalty = !has_admin_level8(d.geo_ref.ways[item.idx]->admin_list);
            break;
        case navitia::type::Type_e::StopPoint:
            apply_penalty = !has_admin_level8(d.pt_data.stop_points[item.idx]->admin_list);
            break;
        default:
            break;
        }
        if(apply_penalty)
            item.quality -= penalty;
    }
}

void update_quality(std::vector<Autocomplete<nt::idx_t>::fl_quality>& ac_result, navitia::type::Type_e ntype,
                    bool Is_address_type,
                    const navitia::type::Data &d){
    //Mettre à jour la qualité sur la pénalité par type d'adresse
    const int penalty = penalty_by_type(ntype, Is_address_type);
    for(auto &item : ac_result) {
        item.quality -= penalty;
    }

    //Mettre à jour la qualité sur le poids de POI
    //Cette méthode n'est pas utilisé en absence du poids sur poi-type.
//    if (ntype ==navitia::type::Type_e::POI){
{'fraction_non_alphanumeric': 0.1294510739856802, 'fraction_numerical': 0.005441527446300716, 'mean_word_length': 2.564477713507996, 'pattern_counts': {'":': 0, '<': 24, '<?xml version=': 0, '>': 63, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '7576292', 'n_tokens_mistral': 3533, 'n_tokens_neox': 3451, 'n_words': 634}