File: feature.h

package info (click to toggle)
zinnia 0.06-1
  • links: PTS, VCS
  • area: main
  • in suites: squeeze, wheezy
  • size: 2,368 kB
  • ctags: 2,156
  • sloc: cpp: 13,195; sh: 9,667; perl: 223; python: 200; makefile: 195; ansic: 70; ruby: 47
file content (64 lines) | stat: -rw-r--r-- 1,382 bytes parent folder | download | duplicates (8)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
//
//  Zinnia: Online hand recognition system with machine learning
//
//  $Id: feature.h 17 2009-04-05 11:40:32Z taku-ku $;
//
//  Copyright(C) 2008 Taku Kudo <taku@chasen.org>
//
#ifndef zinnia_FEATURE_H__
#define zinnia_FEATURE_H__

namespace zinnia {

class Character;

struct FeatureNode {
  int index;
  float value;
};

inline double dot(const FeatureNode *x1,
                  const FeatureNode *x2) {
  double sum = 0;
  while (x1->index >= 0 && x2->index >= 0) {
    if (x1->index == x2->index) {
      sum += (x1->value * x2->value);
      ++x1;
      ++x2;
    } else if (x1->index < x2->index) {
      ++x1;
    } else {
      ++x2;
    }
  }
  return sum;
}

struct Node {
  float x;
  float y;
};

class Features {
 private:
  std::vector<FeatureNode> features_;

  struct NodePair {
    const Node *first;
    const Node *last;
    NodePair(): first(0), last(0) {}
  };

  void makeBasicFeature(int id, const Node *first, const Node *last);
  void makeMoveFeature(int id, const Node *first, const Node *last);
  void makeVertexFeature(int id, std::vector<NodePair> *node_pairs);
  void getVertex(const Node *first, const Node *last,
                 int id, std::vector<NodePair> *node_pairs) const;
  void addFeature(int index, float value);

 public:
  bool read(const Character &character);
  const FeatureNode *get() const { return &features_[0]; }
};
}
#endif