<commit_before>#include "hasher.hxx"

using namespace std;
using namespace Py;

SequenceHasher::SequenceHasher(){

    _ref = NULL;
    _initialized = false;

}

SequenceHasher::~SequenceHasher(){
    if( _ref ){
        delete _ref;
    }

    list<ObservationSet*>::iterator obs_itr;

    for( obs_itr = _obs.begin(); obs_itr != _obs.end(); obs_itr++ ){
        delete (*obs_itr);
    }
    _obs.clear();
}

priority_queue<pair<double, string> > SequenceHasher::hash(string sequence){

    if( !_initialized ){
        initialize();
    }

    ObservationSet os(sequence);
    list<ObservationSet*>::iterator l_itr;

    priority_queue<pair<double, string> > rpq;
    for(l_itr = _obs.begin(); l_itr != _obs.end(); l_itr++){

        double likelihood = (*l_itr)->likelihood(&os, _ref);
        string nm = (*l_itr)->getName();
        rpq.push(pair<double, string>(likelihood, nm));
    }
    
    return rpq;
}

void SequenceHasher::addReference(string name, string sequence){

    //TODO Ensure that arguments are in a consistent order
    ObservationSet* ob = new ObservationSet(sequence, name);
    _obs.push_back(ob);

    _initialized = false;
    return;
}

void SequenceHasher::initialize(){
    _initialized = true;
    if( !_ref ){ delete _ref; }

    _ref = new ReferenceSet(_obs);
    list<ObservationSet*>::iterator obs_itr;

    return;
}

Object SequenceHasher::py_addReference( const Tuple &args ){
    args.verify_length(2);
    String name = args[0];
    String sequence = args[1];

    addReference(name, sequence);

    return args[0];
}

Object SequenceHasher::py_hash( const Tuple &args ){
    args.verify_length(1);
    String sequence = args[0];
    priority_queue<pair<double, string> > results = hash(sequence);

    Dict d;
    
    while( !results.empty() ){
        double likelihood = results.top().first;
        string name = results.top().second;
        d[String(name)] = Float(floor(likelihood * 10000));
        results.pop();
    }

    return d;
}

Object SequenceHasher::getattr( const char *name ){
    return getattr_methods(name);
}

Object SequenceHasher::repr(){
    return Py::String("A Hashed based Naive Bayes Classifier for sequence classification");
}

void SequenceHasher::init_type(){
    behaviors().name("SequenceHasher");
    behaviors().doc("SequenceHasher objects: nil");
    behaviors().supportGetattr();
    behaviors().supportRepr();<commit_msg>We have a Naive Bayes Classifier for sequence analysis. It works with python. It produces correct results.<commit_after>#include "hasher.hxx"

using namespace std;
using namespace Py;

SequenceHasher::SequenceHasher(){

    _ref = NULL;
    _initialized = false;

}

SequenceHasher::~SequenceHasher(){
    if( _ref ){
        delete _ref;
    }

    list<ObservationSet*>::iterator obs_itr;

    for( obs_itr = _obs.begin(); obs_itr != _obs.end(); obs_itr++ ){
        delete (*obs_itr);
    }
    _obs.clear();
}

priority_queue<pair<double, string> > SequenceHasher::hash(string sequence){

    if( !_initialized ){
        initialize();
    }

    ObservationSet os(sequence);
    list<ObservationSet*>::iterator l_itr;

    priority_queue<pair<double, string> > rpq;
    for(l_itr = _obs.begin(); l_itr != _obs.end(); l_itr++){

        double likelihood = (*l_itr)->likelihood(&os, _ref);
        string nm = (*l_itr)->getName();
        rpq.push(pair<double, string>(likelihood, nm));
    }
    
    return rpq;
}

void SequenceHasher::addReference(string name, string sequence){

    //TODO Ensure that arguments are in a consistent order
    ObservationSet* ob = new ObservationSet(sequence, name);
    _obs.push_back(ob);

    _initialized = false;
    return;
}

void SequenceHasher::initialize(){
    _initialized = true;
    if( !_ref ){ delete _ref; }

    _ref = new ReferenceSet(_obs);
    list<ObservationSet*>::iterator obs_itr;

    return;
}

Object SequenceHasher::py_addReference( const Tuple &args ){
    args.verify_length(2);
    String name = args[0];
    String sequence = args[1];

    addReference(name, sequence);

    return args[0];
}

Object SequenceHasher::py_hash( const Tuple &args ){
    args.verify_length(1);
    String sequence = args[0];
    priority_queue<pair<double, string> > results = hash(sequence);

    List d;
    
    while( !results.empty() ){
        string name = results.top().second;
        d.append(String(name));
        results.pop();
    }

    return d;
}

Object SequenceHasher::getattr( const char *name ){
    return getattr_methods(name);
}

Object SequenceHasher::repr(){
    return Py::String("A Hashed based Naive Bayes Classifier for sequence classification");
}

void SequenceHasher::init_type(){
    behaviors().name("SequenceHasher");
    behaviors().doc("SequenceHasher objects: nil");
    behaviors().supportGetattr();
    behaviors().supportRepr();
{'fraction_non_alphanumeric': 0.13706004140786748, 'fraction_numerical': 0.0035196687370600416, 'mean_word_length': 3.536150234741784, 'pattern_counts': {'":': 0, '<': 23, '<?xml version=': 0, '>': 27, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1936840', 'n_tokens_mistral': 1539, 'n_tokens_neox': 1528, 'n_words': 348}