Commit 261d9490 authored by Adrien Dorsaz's avatar Adrien Dorsaz

Refractor classifier name

parent 75da9502
......@@ -18,7 +18,7 @@ QML_IMPORT_PATH =
SOURCES += main.cpp \
emotions.cpp \
mycallback.cpp \
naivebaiseclassifier.cpp
naivebayesclassifier.cpp
# Please do not modify the following two lines. Required for deployment.
include(qmlapplicationviewer/qmlapplicationviewer.pri)
......@@ -29,7 +29,7 @@ include(./think-core/src/think-core.pri)
HEADERS += \
mycallback.h \
emotions.h \
naivebaiseclassifier.h
naivebayesclassifier.h
OTHER_FILES += \
android/src/org/kde/necessitas/ministro/IMinistro.aidl \
......
......@@ -2,8 +2,8 @@
Emotions::Emotions()
{
_arousalClassifier=new NaiveBaiseClassifier();
_valenceClassifier=new NaiveBaiseClassifier();
_arousalClassifier=new NaiveBayesClassifier();
_valenceClassifier=new NaiveBayesClassifier();
_saveCalm=false;
_saveJoy=false;
_saveSad=false;
......@@ -55,13 +55,13 @@ void Emotions::arousalValence(double arousal, double valence){
qDebug()<<"Emo : klass "<<klass<<" has "<<_trainedArousalClasses->value(klass)->size()<<" values";
}
if(_arousalClassifier==NULL){
_arousalClassifier = new NaiveBaiseClassifier(_trainedArousalClasses, _totalArousalOccurrences);
_arousalClassifier = new NaiveBayesClassifier(_trainedArousalClasses, _totalArousalOccurrences);
}
foreach(QString klass, _trainedValenceClasses->keys()){
qDebug()<<"Emo : klass "<<klass<<" has "<<_trainedValenceClasses->value(klass)->size()<<" values";
}
if(_valenceClassifier==NULL){
_valenceClassifier = new NaiveBaiseClassifier(_trainedValenceClasses, _totalValenceOccurrences);
_valenceClassifier = new NaiveBayesClassifier(_trainedValenceClasses, _totalValenceOccurrences);
}
curArousal = _arousalClassifier->classify(_arousalSet);
......
......@@ -7,7 +7,7 @@
#include <QMapIterator>
#include <QVariant>
#include "sbs2common.h"
#include "naivebaiseclassifier.h"
#include "naivebayesclassifier.h"
// Number of decimals for arousal/valence (too high give bad results, too low too)
#define EMOTION_AROUSAL_ACCURACY 4
......@@ -17,8 +17,8 @@ class Emotions : public QObject{
Q_OBJECT
private:
NaiveBaiseClassifier* _arousalClassifier;
NaiveBaiseClassifier* _valenceClassifier;
NaiveBayesClassifier* _arousalClassifier;
NaiveBayesClassifier* _valenceClassifier;
QMap<QString,QMap<double, double>*>* _trainedArousalClasses; // calm / exited
QMap<QString,QMap<double, double>*>* _trainedValenceClasses; // negative / positive
......@@ -62,6 +62,5 @@ public slots:
signals:
void giveEmotion(QVariant emotion);
};
//Q_DECLARE_METATYPE(NaiveBaiseClassifier);
#endif // EMOTIONS_H
#include "naivebaiseclassifier.h"
#include "naivebayesclassifier.h"
NaiveBaiseClassifier::NaiveBaiseClassifier(){
NaiveBayesClassifier::NaiveBayesClassifier(){
_trainedClasses=new QMap<QString,QMap<double, double>*>();
_totalFeatureOccurrences = new QMap<double,double>();
}
NaiveBaiseClassifier::NaiveBaiseClassifier(QMap<QString, QMap<double, double> *> *trainedClasses,
NaiveBayesClassifier::NaiveBayesClassifier(QMap<QString, QMap<double, double> *> *trainedClasses,
QMap<double, double> *totalFeatureOccurrences){
_trainedClasses = new QMap<QString, QMap<double,double>*>(*trainedClasses);
foreach(QString klass, _trainedClasses->keys()){
......@@ -15,7 +15,7 @@ NaiveBaiseClassifier::NaiveBaiseClassifier(QMap<QString, QMap<double, double> *>
_totalFeatureOccurrences=new QMap<double,double>(*totalFeatureOccurrences);
}
NaiveBaiseClassifier::NaiveBaiseClassifier(QMap<QString, QMap<double, double> *> &trainedClasses,
NaiveBayesClassifier::NaiveBayesClassifier(QMap<QString, QMap<double, double> *> &trainedClasses,
QMap<double, double> &totalFeatureOccurrences){
_trainedClasses = new QMap<QString, QMap<double,double>*>(trainedClasses);
foreach(QString klass, _trainedClasses->keys()){
......@@ -25,7 +25,7 @@ NaiveBaiseClassifier::NaiveBaiseClassifier(QMap<QString, QMap<double, double> *>
_totalFeatureOccurrences=new QMap<double,double>(totalFeatureOccurrences);
}
QString NaiveBaiseClassifier::classify(QMap<double, double> *featureSet){
QString NaiveBayesClassifier::classify(QMap<double, double> *featureSet){
QMap<QString, double> * classProbability = new QMap<QString, double>();
foreach(QString klass, _trainedClasses->keys()){
......@@ -62,37 +62,37 @@ QString NaiveBaiseClassifier::classify(QMap<double, double> *featureSet){
return resultClass;
}
double NaiveBaiseClassifier::probability(double feature, QString klass){
double NaiveBayesClassifier::probability(double feature, QString klass){
double num = _trainedClasses->value(klass)->value(feature)+1.0;
double den = _totalFeatureOccurrences->value(feature)+_trainedClasses->size();
return num/den;
}
QMap<QString,QMap<double, double>*>& NaiveBaiseClassifier::getTrainedClasses() const{
QMap<QString,QMap<double, double>*>& NaiveBayesClassifier::getTrainedClasses() const{
return *_trainedClasses;
}
QMap<double, double>& NaiveBaiseClassifier::getTotalFeatureOccurences() const{
QMap<double, double>& NaiveBayesClassifier::getTotalFeatureOccurences() const{
return *_totalFeatureOccurrences;
}
QDataStream &operator<<(QDataStream &out, const NaiveBaiseClassifier &naiveBaise){
QDataStream &operator<<(QDataStream &out, const NaiveBayesClassifier &naiveBayes){
QMap<QString, QMap<double,double>*>* trainedClasses =
new QMap<QString, QMap<double,double>*>(naiveBaise.getTrainedClasses());
qDebug()<<"size : "<<naiveBaise.getTrainedClasses().size();
out<<naiveBaise.getTrainedClasses().size();
new QMap<QString, QMap<double,double>*>(naiveBayes.getTrainedClasses());
qDebug()<<"size : "<<naiveBayes.getTrainedClasses().size();
out<<naiveBayes.getTrainedClasses().size();
foreach(QString klass, trainedClasses->keys()){
out<<klass;
out<<*(trainedClasses->value(klass));
}
out << naiveBaise.getTotalFeatureOccurences();
out << naiveBayes.getTotalFeatureOccurences();
return out;
}
QDataStream &operator>>(QDataStream &in, NaiveBaiseClassifier &naiveBaise){
QDataStream &operator>>(QDataStream &in, NaiveBayesClassifier &naiveBayes){
int sizeMap;
naiveBaise._trainedClasses=new QMap<QString,QMap<double, double>*>();
naiveBayes._trainedClasses=new QMap<QString,QMap<double, double>*>();
QMap<double, double> totalFeatureOccurences;
......@@ -101,8 +101,8 @@ QDataStream &operator>>(QDataStream &in, NaiveBaiseClassifier &naiveBaise){
QMap<double, double> trainedValue;
QString trainedKey;
in >> trainedKey >> trainedValue;
naiveBaise._trainedClasses->insert(trainedKey, &trainedValue);
naiveBayes._trainedClasses->insert(trainedKey, &trainedValue);
}
in >> *(naiveBaise._totalFeatureOccurrences);
in >> *(naiveBayes._totalFeatureOccurrences);
return in;
}
......@@ -2,8 +2,8 @@
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
#ifndef NAIVEBAYESCLASSIFIER_H
#define NAIVEBAYESCLASSIFIER_H
#include <QMap>
#include <QMapIterator>
......@@ -13,11 +13,11 @@ http://www.nils-haldenwang.de/computer-science/machine-learning/how-to-apply-nai
#include <QDataStream>
#include <QDebug>
class NaiveBaiseClassifier: public QObject{
class NaiveBayesClassifier: public QObject{
Q_OBJECT
friend QDataStream& operator <<( QDataStream& stream, const NaiveBaiseClassifier& bookmark );
friend QDataStream& operator >>( QDataStream& stream, NaiveBaiseClassifier& bookmark );
friend QDataStream& operator <<( QDataStream& stream, const NaiveBayesClassifier& bookmark );
friend QDataStream& operator >>( QDataStream& stream, NaiveBayesClassifier& bookmark );
private:
QMap<QString,QMap<double, double>*> * _trainedClasses;
......@@ -26,20 +26,20 @@ private:
double probability(double feature, QString klass);
public:
NaiveBaiseClassifier();
NaiveBaiseClassifier(QMap<QString,QMap<double, double>*>* trainedClasses, // key : class name, value : value found during training and their number of occurences
NaiveBayesClassifier();
NaiveBayesClassifier(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
NaiveBaiseClassifier(QMap<QString,QMap<double, double>*>& trainedClasses,
NaiveBayesClassifier(QMap<QString,QMap<double, double>*>& trainedClasses,
QMap<double, double>& totalFeatureOccurrences);
NaiveBaiseClassifier(const NaiveBaiseClassifier& naiveBaise);
NaiveBaiseClassifier& operator=(const NaiveBaiseClassifier& naiveBaise);
NaiveBayesClassifier(const NaiveBayesClassifier& naiveBayes);
NaiveBayesClassifier& operator=(const NaiveBayesClassifier& naiveBayes);
QString classify(QMap<double, double>* featureSet); // featureSet : set of values to classify with number of occurence
QMap<QString,QMap<double, double>*> &getTrainedClasses() const;
QMap<double, double> &getTotalFeatureOccurences() const;
};
QDataStream &operator<<(QDataStream &out, const NaiveBaiseClassifier &naiveBaise);
QDataStream &operator>>(QDataStream &in, NaiveBaiseClassifier &naiveBaise);
QDataStream &operator<<(QDataStream &out, const NaiveBayesClassifier &naiveBayes);
QDataStream &operator>>(QDataStream &in, NaiveBayesClassifier &naiveBayes);
#endif // NAIVEBAISECLASSIFIER_H
#endif // NAIVEBAYESCLASSIFIER_H
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment