朴素贝叶斯算法-C++实现+拉普拉斯平滑+算法优化

分类:

#include <iostream>
#include <algorithm>
#include <iomanip>
#include <fstream>
#include <sstream>
#include <cstring>
#include <string>
#include <vector>
#include <cmath>
#include <time.h>
#include <map>

using namespace std;


struct train_data {
    int index;                      //训练文本序号
    int emotion_value;              //情感值
    string emotion;                 //情感状态
    vector<string> word;            //训练文本单词
    int onehot[1000];               //onehot矩阵中的值
    double distance;                //距离

    train_data(int a = 0, int b = 0, string c = "", double d = 0.0) {
        index = a;
        emotion_value = b;
        emotion = c;
        distance = d;
        word.clear();
        for (int i = 0; i < 1000; i ++)
            onehot[i] = 0;
    }
};

vector<string> train_text;          //每个完整的训练文本
vector<string> all_words;           //所有不同的单词 ,纵轴
vector<train_data> all_trains;      //所有训练文本,横轴
int right_sum;                      //预测正确的个数


//NB adding part
struct each_word {
    string name;
    double time;
    each_word(string a = "", double b = 0) {
        name = a;
        time = b;
    }
};
//构建词带

struct emotion {
    string emotion2;
    vector<each_word> word_bag;
    double sum_words;
    double probability;
    double times;
    emotion(double a = 0, double b = 0, double c = 0) {
        word_bag.clear();
        sum_words = a;
        probability = b;
        times = c;
    }
};

emotion each_emotion[7];  



//NB adding part

// youhua part

double add[7] = {
  
  0};

//youhua part

void reading_file(void );
void class_calculating();
bool cmp(const emotion & , const emotion & );

int main() {

    train_text.clear();
    all_words.clear();
    all_trains.clear();

    each_emotion[1].emotion2 = "anger";
    each_emotion[2].emotion2 = "disgust";
    each_emotion[3].emotion2 = "fear";
    each_emotion[4].emotion2 = "joy";
    each_emotion[5].emotion2 = "sad";
    each_emotion[6].emotion2 = "surprise";
    reading_file();
    class_calculating();


    return 0;
}

void reading_file() {
    ifstream train("train.txt");
    char read[100];
    string temp;
    train.getline(read, 100);
  
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值