Source

quechua / modules / processor / reverter / processoreverter.cc

/* 
 * Quechua - the lightweight data mining framework
 *
 * Copyright (C) 2012 Marek Denis <quechua@octogan.net>
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

#ifndef REVERTER_H
#define REVERTER_H

#include "../../../include/quechua.h"
#include "../../../include/interface.h"
#include "../../../include/types.h"
#include "../../channel/stdch/mydata.h"
#include <map>
#include <ev++.h>


class Reverter : public Processor {
    typedef std::map<string_t,string_t> words_t;
 public:
     Reverter();
     ~Reverter();

     datapack_ptr preprocess(datapack_ptr data);
     datapack_ptr postprocess(datapack_ptr data);
 private:
    words_t words;
    words_t words_post;

};

Reverter::Reverter() : Processor() {};


// returns new array of chars however in reverted order
datapack_ptr Reverter::preprocess(datapack_ptr data) {
    string_t word = reinterpret_cast<MyData*>(data.get())->content;
    string_t new_word;
    data.reset();
    
    words_t::iterator it;
    words_t::iterator end = words.end();
    if((it=words.find(word))!=end) {
        new_word = it->second;
    }
    else {
        for(int i=word.size();i>0;--i) {
            new_word.push_back(word.at(i-1));
        }

        words.insert(std::pair<string_t,string_t>(word,new_word));
        words_post.insert(std::pair<string_t,string_t>(new_word,word));
    }
    ProcessorData* d = new ProcessorData();
    
    d->content = new_word;
    return datapack_ptr(d);
};

datapack_ptr Reverter::postprocess(datapack_ptr data) {
    string_t word = reinterpret_cast<ProcessorData*>(data.get())->content;
    string_t new_word;
    words_t::iterator it;
    words_t::iterator end = words_post.end();
    it = words_post.find(word);
    new_word = it->second;

    ProcessorData* d = new ProcessorData();
    d->content = new_word;
    return datapack_ptr(d);
};

Reverter::~Reverter() {
    words.clear();
    words_post.clear();
};

extern "C" Processor* create_processor() {
    return new Reverter();
};

#endif