summaryrefslogtreecommitdiff
path: root/dtrain/scfg/features/count/featurecount.cc
blob: db31885cba21eb55f244ce16b8dbd5d68a2f1c8d (plain)
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
#include "featurecount.hh"


void
FeatureCountMapper::map( HadoopPipes::MapContext &context )
{
  string line = context.getInputValue();

  // get features substr
  size_t i = 0, c = 0, beg = 0, end = 0;
  string::iterator it = line.begin();
  string s;
  while ( c != 12 ) {
    s = *it;
    if ( s == "|" ) c += 1;
    if ( beg == 0 && c == 9 ) beg = i+2;
    if ( c == 12 ) end = i-beg-3;
    it++;
    i++;
  }
  string sub = line.substr( beg, end );

  // emit feature:1
  vector<string> f_tok;
  boost::split( f_tok, sub, boost::is_any_of(" ") );
  vector<string>::iterator f;
  for ( f = f_tok.begin(); f != f_tok.end(); f++ ) {
    if ( f->find("=1") != string::npos ) context.emit(*f, "1");
  }
}

void
FeatureCountReducer::reduce( HadoopPipes::ReduceContext &context )
{
  size_t sum = 0;
  while ( context.nextValue() ) sum += HadoopUtils::toInt( context.getInputValue() );
  context.emit( context.getInputKey(), HadoopUtils::toString(sum) );
}


int
main( int argc, char * argv[] )
{
  HadoopPipes::TemplateFactory2<FeatureCountMapper,
                                FeatureCountReducer> factory;

  return HadoopPipes::runTask(factory);
}