예제 #1
0
def cons_train_sample_for_cla(filename, indexs, local_fun, dic_path,
                              sample_save_path, delete, str_splitTag,
                              tc_splitTag):
    '''根据提供的词典,将指定文件中的指定位置上的内容构造成SVM所需的问题格式,并进行保存'''
    dic_list, global_weight = fileutil.read_dic_ex(dic_path, dtype=str)
    if type(local_fun) == types.StringType:
        local_fun = measure.local_f(local_fun)
    label = set()
    #对原训练样本进行词干化处理
    print "-----------------正在对源文本进行词干化处理-------------------"
    stem.stemFile(filename, str_splitTag, tc_splitTag)

    f = file(filename, 'r')
    fs = file(sample_save_path, 'w')
    for line in f.readlines():
        text = line.strip().split(tc_splitTag)
        text_temp = ""
        if len(text) < indexs[len(indexs) - 1] + 1:
            continue
        for i in indexs:
            text_temp += str_splitTag + text[i]
        y, x = ctmutil.cons_pro_for_svm(text[0],
                                        text_temp.strip().split(str_splitTag),
                                        dic_list, local_fun, global_weight)
        if delete == True and len(x[0]) == 0:
            continue
        save_dic_train_sample(fs, y, x)
        label.add(y[0])
    f.close()
    fs.close()
    return label
def cons_train_sample_for_cla(filename,indexs,local_fun,dic_path,sample_save_path,delete,str_splitTag,tc_splitTag):
    '''根据提供的词典,将指定文件中的指定位置上的内容构造成SVM所需的问题格式,并进行保存'''
    dic_list,global_weight = fileutil.read_dic_ex(dic_path,dtype=str)
    if type(local_fun)==types.StringType:
        local_fun = measure.local_f(local_fun)
    label = set()
    #对原训练样本进行词干化处理
    print "-----------------正在对源文本进行词干化处理-------------------"
    stem.stemFile(filename,str_splitTag,tc_splitTag)    
    
    f= file(filename,'r')
    fs = file(sample_save_path,'w')
    for line in f.readlines():
        text = line.strip().split(tc_splitTag)
        text_temp=""
        if len(text)<indexs[len(indexs)-1]+1:
            continue
        for i in indexs:
          text_temp+=str_splitTag+text[i]  
        y,x = ctmutil.cons_pro_for_svm(text[0],text_temp.strip().split(str_splitTag),dic_list,local_fun,global_weight)
        if delete == True and len(x[0])==0:
            continue
        save_dic_train_sample(fs,y,x)
        label.add(y[0])
    f.close()
    fs.close()
    return label
def cal_sc_optim(lab,m,text,dic_list,local_fun,global_weight,str_splitTag):
    '''输入标签,模型,待预测的文本,词典,以及词分词用的符号
    返回的是一个预测标签与得分,如果是二分类,返回的是直接得分,如果为多分类,返回的是经过计算的综合分数。
    '''
    local_fun = measure.local_f(local_fun)
    y,x = ctmutil.cons_pro_for_svm(lab,text.strip().split(str_splitTag),dic_list,local_fun,global_weight)
    p_lab,p_acc,p_sc=tms_svm.predict(y,x,m)  
    #在这里要判定是二分类还是多分类,如果为二分类,返回相应的分数,如果为多分类,则返回预测的标签。
    return p_lab[0],tms_svm.classer_value(p_sc[0])