Commit de840af7 authored by Adrien Dorsaz's avatar Adrien Dorsaz

Let NaiveBaiseClassifier manage the classifier maps

It simpilifies the way to store/update/restore these maps and avoid to
suppress data by a bad map sync between both classes.
parent c2a968fb
......@@ -9,20 +9,11 @@ Emotions::Emotions()
+".dat";
qDebug()<<"Data path : "<<_dataPath;
/* Classifiers to fill (either by file in data path or by UI) */
_arousalClassifier=new NaiveBayesClassifier();
_valenceClassifier=new NaiveBayesClassifier();
_saveCalm=false;
_saveJoy=false;
_saveSad=false;
_saveFear=false;
_arousalSet = new QMap<double,double>();
_valenceSet = new QMap<double,double>();
_totalArousalOccurrences = new QMap<double, double>();
_totalValenceOccurrences = new QMap<double, double>();
_trainedArousalClasses = new QMap<QString, QMap<double, double>*>();
_trainedValenceClasses = new QMap<QString, QMap<double, double>*>();
/* UI buttons */
_guess=false;
_record=false;
_saveCalm=false;
......@@ -30,6 +21,10 @@ Emotions::Emotions()
_saveSad=false;
_saveFear=false;
/* These sets are used to record current to find later the emotion*/
_arousalSet = new QMap<double,double>();
_valenceSet = new QMap<double,double>();
getClassifiers();
}
......@@ -108,11 +103,6 @@ void Emotions::guessEmotion(){
_guess=true;
}
void Emotions::insertValueAndTotal(QMap<double, double> *valueSet, QMap<double, double> *totalSet, double val){
insertValue(valueSet, val);
insertValue(totalSet, val);
}
void Emotions::insertValue(QMap<double, double> *valueSet, double val){
if(valueSet->contains(val)){
valueSet->insert(val, valueSet->value(val)+1);
......@@ -122,22 +112,8 @@ void Emotions::insertValue(QMap<double, double> *valueSet, double val){
}
void Emotions::updateTrainedClass(QString arousalStr, int arousalVal, QString valenceStr, int valenceVal){
insertValueAndTotal(_arousalSet, _totalArousalOccurrences, arousalVal);
insertValueAndTotal(_valenceSet, _totalValenceOccurrences, valenceVal);
if(_trainedArousalClasses->contains(arousalStr)){
insertValue(_trainedArousalClasses->value(arousalStr), arousalVal);
}else{
_trainedArousalClasses->insert(arousalStr, new QMap<double,double>(*_arousalSet));
}
if(_trainedValenceClasses->contains(valenceStr)){
insertValue(_trainedValenceClasses->value(valenceStr), valenceVal);
}else{
_trainedValenceClasses->insert(valenceStr, new QMap<double,double>(*_valenceSet));
}
_arousalClassifier = new NaiveBayesClassifier(_trainedArousalClasses,_totalArousalOccurrences);
_valenceClassifier = new NaiveBayesClassifier(_trainedValenceClasses,_totalValenceOccurrences);
_arousalClassifier->addValueForClass(arousalStr, arousalVal);
_valenceClassifier->addValueForClass(valenceStr, valenceVal);
}
void Emotions::storeClassifiers(){
......
......@@ -19,16 +19,12 @@ class Emotions : public QObject{
private:
QString _dataPath;
/* Classifiers to fill (either by file in data path or by UI) */
NaiveBayesClassifier* _arousalClassifier;
NaiveBayesClassifier* _valenceClassifier;
QMap<QString,QMap<double, double>*>* _trainedArousalClasses; // calm / exited
QMap<QString,QMap<double, double>*>* _trainedValenceClasses; // negative / positive
// Sets with value and with number of occurences over all the process and over all states
QMap<double, double>* _totalArousalOccurrences;
QMap<double, double>* _totalValenceOccurrences;
/* UI buttons */
bool _saveCalm;
bool _saveJoy;
bool _saveSad;
......@@ -42,7 +38,6 @@ private:
QString curArousal;
QString curValence;
void insertValueAndTotal(QMap<double, double>* valueSet, QMap<double, double>* totalSet, double val);
void insertValue(QMap<double, double> *valueSet, double val);
void updateTrainedClass(QString arousalStr, int arousalVal, QString valenceStr, int valenceVal);
void getClassifiers();
......
......@@ -44,9 +44,9 @@ QString NaiveBayesClassifier::classify(QMap<double, double> *featureSet){
}
}
foreach(QString klass, classProbability->keys()){
/*foreach(QString klass, classProbability->keys()){
qDebug()<<"Finally class "<<klass<<" has prob. "<<classProbability->value(klass)<< " and size "<<_trainedClasses->value(klass)->size();
}
}*/
QString resultClass="";
......@@ -73,6 +73,23 @@ double NaiveBayesClassifier::probability(double feature, QString klass){
return prob;
}
void NaiveBayesClassifier::addValueForClass(QString klass, double value){
incrementNbValueForSet(_totalFeatureOccurrences, value);
if(_trainedClasses->contains(klass)){
incrementNbValueForSet(_trainedClasses->value(klass), value);
}else{
_trainedClasses->insert(klass, new QMap<double,double>());
}
}
void NaiveBayesClassifier::incrementNbValueForSet(QMap<double, double> *valueSet, double val){
if(valueSet->contains(val)){
valueSet->insert(val, valueSet->value(val)+1);
}else{
valueSet->insert(val, 1);
}
}
QMap<QString,QMap<double, double>*>& NaiveBayesClassifier::getTrainedClasses() const{
return *_trainedClasses;
}
......
......@@ -24,6 +24,7 @@ private:
QMap<double, double> * _totalFeatureOccurrences;
double probability(double feature, QString klass);
void incrementNbValueForSet(QMap<double, double> *valueSet, double val);
public:
NaiveBayesClassifier();
......@@ -32,8 +33,10 @@ public:
NaiveBayesClassifier(QMap<QString,QMap<double, double>*>& trainedClasses,
QMap<double, double>& totalFeatureOccurrences);
NaiveBayesClassifier(const NaiveBayesClassifier& naiveBayes);
NaiveBayesClassifier& operator=(const NaiveBayesClassifier& naiveBayes);
void addValueForClass(QString klass, double value);
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;
......
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