Page Speed Optimization Libraries  1.9.32.2
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
mobilize_label_filter.h
Go to the documentation of this file.
1 /*
2  * Copyright 2014 Google Inc.
3  *
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
7  *
8  * http:///www.apache.org/licenses/LICENSE-2.0
9  *
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.
15  */
16 
18 
19 #ifndef NET_INSTAWEB_REWRITER_PUBLIC_MOBILIZE_LABEL_FILTER_H_
20 #define NET_INSTAWEB_REWRITER_PUBLIC_MOBILIZE_LABEL_FILTER_H_
21 
22 #include <vector>
23 
26 #include "pagespeed/kernel/base/basictypes.h"
27 #include "pagespeed/kernel/base/string.h"
28 
29 namespace net_instaweb {
30 
31 class HtmlCharactersNode;
32 class HtmlElement;
33 class HtmlParse;
34 class RewriteDriver;
35 class Statistics;
36 class Variable;
37 
48 struct ElementSample {
49  ElementSample(int relevant_tag_depth, int tag_count,
50  int content_bytes, int content_non_blank_bytes);
51 
55  GoogleString ToString(bool readable, HtmlParse* parser);
56 
57  HtmlElement* element;
60  std::vector<double> features;
61 };
62 
79  public:
80  enum LabelingMode {
81  kDoNotLabel = 0,
82  kUseTagNames = 1,
83  kUseClassifier = 2,
84  kUseTagNamesAndClassifier = 3,
85  };
86 
88  static const char kPagesLabeled[];
89  static const char kPagesRoleAdded[];
90  static const char kNavigationalRoles[];
91  static const char kHeaderRoles[];
92  static const char kContentRoles[];
93  static const char kMarginalRoles[];
94  static const char kDivsUnlabeled[];
95  static const char kAmbiguousRoleLabels[];
96 
97  explicit MobilizeLabelFilter(RewriteDriver* driver);
98  virtual ~MobilizeLabelFilter();
99 
100  static void InitStats(Statistics* statistics);
101 
102  virtual const char* Name() const { return "MobilizeLabel"; }
103  virtual void StartDocumentImpl();
104  virtual void StartElementImpl(HtmlElement* element);
105  virtual void EndElementImpl(HtmlElement* element);
106  virtual void Characters(HtmlCharactersNode* characters);
107  virtual void EndDocument();
110  void set_labeling_mode(LabelingMode m) {
111  labeling_mode_ = m;
112  }
113  LabelingMode labeling_mode() const {
114  return labeling_mode_;
115  }
116 
117  private:
118  void Init();
119  void HandleDivLikeElement(HtmlElement* element, MobileRole::Level role);
120  void CheckAttributeStrings(HtmlElement* element);
121  void SetMobileRole(HtmlElement* element, MobileRole::Level role);
122  ElementSample* MakeNewSample(HtmlElement* element);
123  void PopSampleStack();
124  void ComputeContained(ElementSample* sample);
125  void AggregateToTopOfStack(ElementSample* sample);
126  void IncrementRelevantTagDepth();
127  void SanityCheckEndOfDocumentState();
128  void ComputeProportionalFeatures();
129  void Label();
130  void DebugLabel();
131 
132  HtmlElement* active_no_traverse_element_;
133  int relevant_tag_depth_;
134  int max_relevant_tag_depth_;
135  int tag_count_;
136  int content_bytes_;
137  int content_non_blank_bytes_;
138  bool were_roles_added_;
139  LabelingMode labeling_mode_;
140 
141  std::vector<ElementSample*> samples_;
142  std::vector<ElementSample*> sample_stack_;
143 
144  Variable* pages_labeled_;
145  Variable* pages_role_added_;
146  Variable* role_variables_[MobileRole::kInvalid];
147  Variable* divs_unlabeled_;
148  Variable* ambiguous_role_labels_;
149 
150  DISALLOW_COPY_AND_ASSIGN(MobilizeLabelFilter);
151 };
152 
153 }
154 
155 #endif
Definition: common_filter.h:48
ElementSample * parent
NULL for global count.
Definition: mobilize_label_filter.h:58
void set_labeling_mode(LabelingMode m)
Definition: mobilize_label_filter.h:110
virtual void Characters(HtmlCharactersNode *characters)
static const char kPagesLabeled[]
Monitoring variable names.
Definition: mobilize_label_filter.h:88
Definition: mobilize_rewrite_filter.h:58
Definition: mobilize_label_filter.h:78
Definition: mobilize_label_filter.h:48
MobileRole::Level role
Mobile role (from parent where applicable)
Definition: mobilize_label_filter.h:59
Level
Definition: mobilize_rewrite_filter.h:40
Definition: rewrite_driver.h:98
void ComputeProportionalFeatures(ElementSample *normalized)
HtmlElement * element
NULL for global count.
Definition: mobilize_label_filter.h:57
std::vector< double > features
feature vector, always of size kNumFeatures.
Definition: mobilize_label_filter.h:60