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

Finally fix classifier storage and reading

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