2 * Copyright (c) 2016 Samsung Electronics Co., Ltd.
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
17 #include "PlaceCateger.h"
18 #include "../utils/Median.h"
19 #include "../facade/UserPlacesParams.h"
20 #include "../facade/UserPlacesTypes.h"
24 void ctx::PlaceCateger::__reduceOutliers(ctx::Visits &visits)
26 int size = visits.size();
27 visits.erase(std::remove_if(
31 return v.categs[PLACE_CATEG_ID_HOME] < PLACES_CATEGER_MIN_VISITS_SCORE
32 && v.categs[PLACE_CATEG_ID_WORK] < PLACES_CATEGER_MIN_VISITS_SCORE
33 && v.categs[PLACE_CATEG_ID_OTHER] < PLACES_CATEGER_MIN_VISITS_SCORE;
36 int newSize = visits.size();
38 _D("Visits number from %d to %d (visits min scores checking)", size, newSize);
42 * Change category if home or work has to few visits
44 bool ctx::PlaceCateger::__reduceCategory(const PlaceCategId &categId, const ctx::Visits &visits)
46 return (categId == PLACE_CATEG_ID_HOME && visits.size() < PLACES_CATEGER_MIN_VISITS_PER_HOME)
47 || (categId == PLACE_CATEG_ID_WORK && visits.size() < PLACES_CATEGER_MIN_VISITS_PER_WORK);
50 void ctx::PlaceCateger::categorize(ctx::Visits &visits, ctx::Place &place)
52 __reduceOutliers(visits);
54 place.categId = PLACE_CATEG_ID_NONE;
55 place.categConfidence = 0.0;
57 if (!visits.empty()) {
58 const std::vector<PlaceCategId> categIds = {
65 for (PlaceCategId categId : categIds) {
66 std::vector<num_t> categVector = __categVectorFromVisits(visits, categId);
68 num_t score = median(categVector, i, j);
70 if (score > maxScore) {
72 place.categId = categId;
76 place.categConfidence = maxScore / sumScore;
77 if (__reduceCategory(place.categId, visits)) {
78 place.categId = PLACE_CATEG_ID_OTHER;
79 place.categConfidence = 0.0;
83 place.name = __categId2Name(place.categId);
86 std::vector<ctx::num_t> ctx::PlaceCateger::__categVectorFromVisits(const ctx::Visits &visits, PlaceCategId categId)
88 std::vector<ctx::num_t> vec;
89 for (auto &visit : visits) {
90 auto search = visit.categs.find(categId);
91 if (search != visit.categs.end())
92 vec.push_back(search->second);
97 std::string ctx::PlaceCateger::__categId2Name(PlaceCategId categId) {
99 case PLACE_CATEG_ID_HOME:
101 case PLACE_CATEG_ID_WORK:
103 case PLACE_CATEG_ID_OTHER:
105 case PLACE_CATEG_ID_NONE: