naivebaiseclassifier.h 1.86 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12
/*
This code was inspired by this article :
http://www.nils-haldenwang.de/computer-science/machine-learning/how-to-apply-naive-bayes-classifiers-to-document-classification-problems
*/
#ifndef NAIVEBAISECLASSIFIER_H
#define NAIVEBAISECLASSIFIER_H

#include <QMap>
#include <QMapIterator>
#include <QString>
#include <math.h>
#include <QObject>
Adrien Dorsaz's avatar
Adrien Dorsaz committed
13 14
#include <QDataStream>
#include <QDebug>
15

Adrien Dorsaz's avatar
Adrien Dorsaz committed
16
class NaiveBaiseClassifier: public QObject{
17
    Q_OBJECT
18 19 20 21

    friend QDataStream& operator <<( QDataStream& stream, const NaiveBaiseClassifier& bookmark );
    friend QDataStream& operator >>( QDataStream& stream, NaiveBaiseClassifier& bookmark );

22 23 24 25 26 27 28
private:
    QMap<QString,QMap<double, double>*> * _trainedClasses;
    QMap<double, double> * _totalFeatureOccurrences;

    double probability(double feature, QString klass);

public:
Adrien Dorsaz's avatar
Adrien Dorsaz committed
29
    NaiveBaiseClassifier();
30 31
    NaiveBaiseClassifier(QMap<QString,QMap<double, double>*>* trainedClasses,   // key : class name, value : value found during training and their number of occurences
                         QMap<double, double>* totalFeatureOccurrences); // key : found feature, value : total of occurences sum of every class
Adrien Dorsaz's avatar
Adrien Dorsaz committed
32 33 34 35 36
    NaiveBaiseClassifier(QMap<QString,QMap<double, double>*>& trainedClasses,
                         QMap<double, double>& totalFeatureOccurrences);
    NaiveBaiseClassifier(const NaiveBaiseClassifier& naiveBaise);
    NaiveBaiseClassifier&  operator=(const NaiveBaiseClassifier& naiveBaise);

37
    QString classify(QMap<double, double>* featureSet); // featureSet : set of values to classify with number of occurence
Adrien Dorsaz's avatar
Adrien Dorsaz committed
38 39
    QMap<QString,QMap<double, double>*> &getTrainedClasses() const;
    QMap<double, double> &getTotalFeatureOccurences() const;
40 41
};

Adrien Dorsaz's avatar
Adrien Dorsaz committed
42 43 44
QDataStream &operator<<(QDataStream &out, const NaiveBaiseClassifier &naiveBaise);
QDataStream &operator>>(QDataStream &in, NaiveBaiseClassifier &naiveBaise);

45
#endif // NAIVEBAISECLASSIFIER_H