Commit 67d671fe authored by Adrien Dorsaz's avatar Adrien Dorsaz

Finally fix classifier storage and reading

parent 4187716f
...@@ -2,8 +2,8 @@ ...@@ -2,8 +2,8 @@
Emotions::Emotions() Emotions::Emotions()
{ {
_arousalClassifier=NULL; _arousalClassifier=new NaiveBaiseClassifier();
_valenceClassifier=NULL; _valenceClassifier=new NaiveBaiseClassifier();
_saveCalm=false; _saveCalm=false;
_saveJoy=false; _saveJoy=false;
_saveSad=false; _saveSad=false;
...@@ -140,6 +140,7 @@ void Emotions::updateTrainedClass(QString arousal, QString valence){ ...@@ -140,6 +140,7 @@ void Emotions::updateTrainedClass(QString arousal, QString valence){
if(_trainedArousalClasses->contains(arousal)){ if(_trainedArousalClasses->contains(arousal)){
QMapIterator<double, double> arousalIt(*_arousalSet); QMapIterator<double, double> arousalIt(*_arousalSet);
while (arousalIt.hasNext()) { while (arousalIt.hasNext()) {
arousalIt.next();
double curKey = arousalIt.key(); double curKey = arousalIt.key();
double curVal = arousalIt.value(); double curVal = arousalIt.value();
if(_trainedArousalClasses->value(arousal)->contains(curKey)){ if(_trainedArousalClasses->value(arousal)->contains(curKey)){
...@@ -148,7 +149,6 @@ void Emotions::updateTrainedClass(QString arousal, QString valence){ ...@@ -148,7 +149,6 @@ void Emotions::updateTrainedClass(QString arousal, QString valence){
}else{ }else{
_trainedArousalClasses->value(arousal)->insert(curKey,curVal); _trainedArousalClasses->value(arousal)->insert(curKey,curVal);
} }
arousalIt.next();
} }
}else{ }else{
_trainedArousalClasses->insert(arousal, _arousalSet); _trainedArousalClasses->insert(arousal, _arousalSet);
...@@ -157,6 +157,7 @@ void Emotions::updateTrainedClass(QString arousal, QString valence){ ...@@ -157,6 +157,7 @@ void Emotions::updateTrainedClass(QString arousal, QString valence){
if(_trainedValenceClasses->contains(valence)){ if(_trainedValenceClasses->contains(valence)){
QMapIterator<double, double> valenceIt(*_valenceSet); QMapIterator<double, double> valenceIt(*_valenceSet);
while (valenceIt.hasNext()) { while (valenceIt.hasNext()) {
valenceIt.next();
double curKey = valenceIt.key(); double curKey = valenceIt.key();
double curVal = valenceIt.value(); double curVal = valenceIt.value();
if(_trainedValenceClasses->value(valence)->contains(curKey)){ if(_trainedValenceClasses->value(valence)->contains(curKey)){
...@@ -165,7 +166,6 @@ void Emotions::updateTrainedClass(QString arousal, QString valence){ ...@@ -165,7 +166,6 @@ void Emotions::updateTrainedClass(QString arousal, QString valence){
}else{ }else{
_trainedValenceClasses->value(valence)->insert(curKey,curVal); _trainedValenceClasses->value(valence)->insert(curKey,curVal);
} }
valenceIt.next();
} }
}else{ }else{
_trainedValenceClasses->insert(valence, _valenceSet); _trainedValenceClasses->insert(valence, _valenceSet);
......
...@@ -55,6 +55,6 @@ public slots: ...@@ -55,6 +55,6 @@ public slots:
signals: signals:
void giveEmotion(QVariant emotion); void giveEmotion(QVariant emotion);
}; };
Q_DECLARE_METATYPE(NaiveBaiseClassifier); //Q_DECLARE_METATYPE(NaiveBaiseClassifier);
#endif // EMOTIONS_H #endif // EMOTIONS_H
#include "naivebaiseclassifier.h" #include "naivebaiseclassifier.h"
NaiveBaiseClassifier::NaiveBaiseClassifier(const NaiveBaiseClassifier& naiveBaise){
qDebug()<<"will copy nbc by constructor";
_trainedClasses = new QMap<QString, QMap<double, double>*>(naiveBaise.getTrainedClasses());
_totalFeatureOccurrences = new QMap<double,double>(naiveBaise.getTotalFeatureOccurences());
}
NaiveBaiseClassifier& NaiveBaiseClassifier::operator=(const NaiveBaiseClassifier& naiveBaise) {
if ( this == &naiveBaise ) {
return *this; //Self assignment : nothing to do
}
QMap<double,double>* tmpOcc = new QMap<double,double>(naiveBaise.getTotalFeatureOccurences());
foreach(double feature, tmpOcc->keys()){
qDebug()<<feature;
//_totalFeatureOccurrences->insert(feature, tmpOcc->value(feature));
}
qDebug()<<"Done !";
QMapIterator<QString, QMap<double,double>*> trainedCl(naiveBaise.getTrainedClasses());
while(trainedCl.hasNext()){
_trainedClasses->insert(trainedCl.key(),trainedCl.value());
trainedCl.next();
}
return *this;
}
NaiveBaiseClassifier::NaiveBaiseClassifier(){ NaiveBaiseClassifier::NaiveBaiseClassifier(){
_trainedClasses=new QMap<QString,QMap<double, double>*>(); _trainedClasses=new QMap<QString,QMap<double, double>*>();
_totalFeatureOccurrences = new QMap<double,double>(); _totalFeatureOccurrences = new QMap<double,double>();
...@@ -118,7 +92,8 @@ QDataStream &operator<<(QDataStream &out, const NaiveBaiseClassifier &naiveBaise ...@@ -118,7 +92,8 @@ QDataStream &operator<<(QDataStream &out, const NaiveBaiseClassifier &naiveBaise
QDataStream &operator>>(QDataStream &in, NaiveBaiseClassifier &naiveBaise){ QDataStream &operator>>(QDataStream &in, NaiveBaiseClassifier &naiveBaise){
int sizeMap; int sizeMap;
QMap<QString,QMap<double, double>*> trainedClasses; naiveBaise._trainedClasses=new QMap<QString,QMap<double, double>*>();
QMap<double, double> totalFeatureOccurences; QMap<double, double> totalFeatureOccurences;
in >> sizeMap; in >> sizeMap;
...@@ -126,12 +101,8 @@ QDataStream &operator>>(QDataStream &in, NaiveBaiseClassifier &naiveBaise){ ...@@ -126,12 +101,8 @@ QDataStream &operator>>(QDataStream &in, NaiveBaiseClassifier &naiveBaise){
QMap<double, double> trainedValue; QMap<double, double> trainedValue;
QString trainedKey; QString trainedKey;
in >> trainedKey >> trainedValue; in >> trainedKey >> trainedValue;
trainedClasses.insert(trainedKey, &trainedValue); naiveBaise._trainedClasses->insert(trainedKey, &trainedValue);
} }
in >> *(naiveBaise._totalFeatureOccurrences);
in >> totalFeatureOccurences;
qDebug()<<"Ready to read";
naiveBaise = *(new NaiveBaiseClassifier(trainedClasses, totalFeatureOccurences));
qDebug()<<"...read";
return in; return in;
} }
...@@ -15,6 +15,10 @@ http://www.nils-haldenwang.de/computer-science/machine-learning/how-to-apply-nai ...@@ -15,6 +15,10 @@ http://www.nils-haldenwang.de/computer-science/machine-learning/how-to-apply-nai
class NaiveBaiseClassifier: public QObject{ class NaiveBaiseClassifier: public QObject{
Q_OBJECT Q_OBJECT
friend QDataStream& operator <<( QDataStream& stream, const NaiveBaiseClassifier& bookmark );
friend QDataStream& operator >>( QDataStream& stream, NaiveBaiseClassifier& bookmark );
private: private:
QMap<QString,QMap<double, double>*> * _trainedClasses; QMap<QString,QMap<double, double>*> * _trainedClasses;
QMap<double, double> * _totalFeatureOccurrences; QMap<double, double> * _totalFeatureOccurrences;
......
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