文本分类三

这次特征提取先用开方检验来做。然后进行分类,如果分类结果理想。。。然后再用其他的算法做一下。当然也就有时间再整理一下和继续深入学习一下。

主要参考了

http://www.blogjava.net/zhenandaci/archive/2008/08/31/225966.html

http://www.cnblogs.com/finallyliuyu/archive/2010/09/26/1836232.html

结果不太理想,里面有一些不太好的term。。。

不知道怎么处理。。简单的删除还是不理会?感觉是当初预处理的不好吧——额,其实对文本内容就没有做预处理(无奈~)。

结果就不写了,等整理好了再发!

下面是我的代码:

/*
本程序先练习在类别1中提取特征向量;
字典的内容格式:
term
0 对个数 c1 n1 c2 n2 ... c8 n8 c9 n9
....ni就是term在类别i中的文档频率DF,n9是总的文档频率 
A = nj; B = n9 - nj;
C = num(j) - nj;
D = num(D) - n9 - C;
此次试验中num(1) = 516;
num(D) = 3903
*/
#include<iostream>
#include<fstream>
#include<string>
#include<map>
#include<utility>
#include<vector>
#include<cmath>
using namespace std;
map<string,map<int,int> > myDic;
map<string,int> dic;
vector<pair<string,double> > myFeature;
map<string,map<int,int> >::iterator dicItor;
map<int,int>::iterator iItor;
//vector<pair<string,double> > ::iterator featureItor;
bool cmp(const pair<string,double> & m1, const pair<string,double> & m2) 
{
        return m1.second > m2.second;
}
#define Cate 8
int main()
{
    ifstream ifile ("E:\\TC\\code\\featureSelect\\dic.txt");
    ofstream ofile ("E:\\TC\\code\\featureSelect\\dic10.txt");
    string str;
    int id,df;
    int len;
    double num[9] = {0,516,447,471,492,491,488,500,498};
    double num_D = 3903;
    double A,B,C,D;
    double n[10],n_9;
    while(ifile>>str)
    {
        for(int i = 0; i < 10; i++)n[i] = 0;
        ifile>>id>>len;
        
        for(int i = 1; i <= len; i++)
        {
          ifile>>id>>df;
              n[id] = (double)df;//n接受第i类别的DF
          if(id == 9)
              n_9 = (double)df;
        }
        double chiValue = 0;
        double tmpchiValue = 0;
        for(int i = 1; i <= 8; i++)
        {
                A = n[i];
                B = n_9 - n[i];
                C = num[i]- n[i];
                D = num_D - n_9 - C;
                tmpchiValue = num_D*pow(A*D-C*B,2) / ((A+C)*(B+D)*(A+B)*(C+D));
                if(tmpchiValue > chiValue)
                            chiValue = tmpchiValue;
                            
        }
        myFeature.push_back(make_pair(str,chiValue));
        
    }
    stable_sort(myFeature.begin(),myFeature.end(),cmp);
    //int finalKeyWordsCount++;
    for(vector<pair<string,double> >::size_type j=0;j < 1000;j++)
    {
        ofile<<myFeature[j].first<<" "<<myFeature[j].second<<endl;
        //finalKeyWordsCount++;
    }
    system("pause");
    return 0;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值