Add abhijeet.k@samsung.com to AUTHORS list.
[chromium-blink-merge.git] / components / dom_distiller / core / distillable_page_detector.cc
blobaa7ddcf302e0fa516611e7f600fb69acd63ba9a9
1 // Copyright 2015 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 #include "components/dom_distiller/core/distillable_page_detector.h"
7 #include "base/logging.h"
8 #include "grit/components_resources.h"
9 #include "ui/base/resource/resource_bundle.h"
11 namespace dom_distiller {
13 const DistillablePageDetector* DistillablePageDetector::GetDefault() {
14 static DistillablePageDetector* detector = nullptr;
15 if (!detector) {
16 std::string serialized_proto =
17 ResourceBundle::GetSharedInstance()
18 .GetRawDataResource(IDR_DISTILLABLE_PAGE_SERIALIZED_MODEL)
19 .as_string();
20 scoped_ptr<AdaBoostProto> proto(new AdaBoostProto);
21 CHECK(proto->ParseFromString(serialized_proto));
22 detector = new DistillablePageDetector(proto.Pass());
24 return detector;
27 DistillablePageDetector::DistillablePageDetector(
28 scoped_ptr<AdaBoostProto> proto)
29 : proto_(proto.Pass()), threshold_(0.0) {
30 CHECK(proto_->num_stumps() == proto_->stump_size());
31 for (int i = 0; i < proto_->num_stumps(); ++i) {
32 const StumpProto& stump = proto_->stump(i);
33 CHECK(stump.feature_number() >= 0);
34 CHECK(stump.feature_number() < proto_->num_features());
35 threshold_ += stump.weight() / 2.0;
39 DistillablePageDetector::~DistillablePageDetector() {
42 bool DistillablePageDetector::Classify(
43 const std::vector<double>& features) const {
44 return Score(features) > threshold_;
47 double DistillablePageDetector::Score(
48 const std::vector<double>& features) const {
49 if (features.size() != size_t(proto_->num_features())) {
50 return 0.0;
52 double score = 0.0;
53 for (int i = 0; i < proto_->num_stumps(); ++i) {
54 const StumpProto& stump = proto_->stump(i);
55 if (features[stump.feature_number()] > stump.split()) {
56 score += stump.weight();
59 return score;
62 double DistillablePageDetector::GetThreshold() const {
63 return threshold_;
66 } // namespace dom_distiller