zoukankan      html  css  js  c++  java
  • 按类别DF特征词选择算法

    声明:

    按类别特征词选择算法声明
    vector<pair<string,double> >LocalDFFeatureSelectionForPerclass(DICTIONARY& mymap,CONTINGENCY& contingencyTable,string classLabel);//局部DF法针对每个词对每个类别进行排序
            void DFFeatureSelection(vector<string> classLabels,DICTIONARY &mymap,CONTINGENCY& contingencyTable,int N,char *address);//调用局部DF特征词选择函数

    函数实现:

    对词典中的每个词,统计其在某一个类别中出现的次数,并按词频从大到小排序
    /************************************************************************/
    /*  按类别的DF特征词选择法                                               */
    /************************************************************************/
    vector
    <pair<string,double> > Preprocess::LocalDFFeatureSelectionForPerclass(DICTIONARY& mymap,CONTINGENCY& contingencyTable ,string classLabel)
    {
        
    //int finalKeyWordsCount=0;//计算共取了多少个关键词
        clock_t start,finish;
        
    double totaltime;
        start
    =clock();
        vector
    <pair<string,double> >DFinfo;
        
    for(map<string,vector<pair<int,int>>>::iterator it=mymap.begin();it!=mymap.end();++it)
        {
            
            pair
    <string,string>compoundKey=make_pair(it->first,classLabel);
            
    double classCount=(double)contingencyTable[compoundKey].first;
            DFinfo.push_back(make_pair(it
    ->first,classCount));
            
        }

        stable_sort(DFinfo.begin(),DFinfo.end(),isLarger);
        finish
    =clock();
        totaltime
    =(double)(finish-start)/CLOCKS_PER_SEC;
        cout
    <<"为类别"<<classLabel<<"遴选特征词共用了"<<totaltime<<endl;

        
    return DFinfo;


    }

    DF特征词选择法:

    代码
    /************************************************************************/
    /* DF特征词选择法                                                                     */
    /************************************************************************/
    void Preprocess:: DFFeatureSelection(vector<string >classLabels,DICTIONARY &mymap,CONTINGENCY& contingencyTable,int N,char *address)
    {
        clock_t start,finish;
        
    double totaltime;
        
    int totalTraingingCorpus=endIndex-beginIndex+1;//训练语料库总共的文章数目
        set<string>finalKeywords;//存放最终遴选出的特征词
        vector<pair<string,double>>DFInfo;
        start
    =clock();
        
    for(vector<string>::iterator it=classLabels.begin();it!=classLabels.end();it++)
        {
            
    //训练语料库中某个类别的文章数目
            int N_subClassCnt=getCategorizationNum(*it,"TrainingCorpus");
            
    //threshold决定每个类别遴选多少个特征词
            int threshold=N_subClassCnt*N/totalTraingingCorpus;
            DFInfo
    =LocalDFFeatureSelectionForPerclass(mymap,contingencyTable,*it);
            
    for(vector<pair<string,double> >::size_type j=0;j<threshold;j++)
            {
                finalKeywords.insert(DFInfo[j].first);

            }
            DFInfo.clear();




        }


        ofstream outfile(address);
        
    int finalKeyWordsCount=finalKeywords.size();
        
    for (set<string>::iterator it=finalKeywords.begin();it!=finalKeywords.end();it++)
        {
            outfile
    <<*it<<endl;

        }
        outfile.close();
        cout
    <<"最后共选择特征词"<<finalKeyWordsCount<<endl;
        finish
    =clock();
        totaltime
    =(double)(finish-start)/CLOCKS_PER_SEC;
        cout
    <<"遴选特征词共有了"<<totaltime<<endl;

    }

     主函数调用:

    代码
    p.LoadDictionary(mymap,"F:\\finallyliuyu\\dict.dat");
        p.LoadContingencyTable(contingenyTable,
    "F:\\finallyliuyu\\contingency.dat");
        p.DFFeatureSelection(labels,mymap,contingenyTable,
    2000,"F:\\finallyliuyu\\keywords.dat");
  • 相关阅读:
    Java学习笔记-Java中的常用类
    Java学习笔记-Java中的常用类
    Java学习笔记-泛型
    GeoIP简介与资源,定位经纬度,获取用户IP
    【插代码】手机号码所在地查询,引用代码
    最后一个对象属性后边不要加豆号的bug,血淋淋的教训啊,模块化开发IE7下的严重错误,养成好习惯
    CSS自定义滚动条样式
    css实现input文本框的双边框美化
    在函数内部定义的变量加与不加var的区别,匿名函数和有名函数内声明变量的区别
    js定时器关闭,js定时器停止,一次关闭所有正在运行的定时器,自定义函数clearIntervals()一次关闭所有正在运行的定时器
  • 原文地址:https://www.cnblogs.com/finallyliuyu/p/1842008.html
Copyright © 2011-2022 走看看