python库之jieba小试牛刀 3

 关键词提取

1 基于 TF-IDF 算法的关键词抽取

    import jieba.analyse

    jieba.analyse.extract_tags(sentence, topK=20, withWeight=False, allowPOS=())jieba.analyse.TFIDF(idf_path=None) 新建 TFIDF 实例,idf_path 为 IDF 频率文件

  •         sentence 为待提取的文本
  •         topK 为返回几个 TF/IDF 权重最大的关键词,默认值为 20
  •         withWeight 为是否一并返回关键词权重值,默认值为 False
  •         allowPOS 仅包括指定词性的词,默认值为空,即不筛选


       jieba.analyse.TFIDF(idf_path=None) 新建 TFIDF 实例,idf_path 为 IDF 频率文件

       关键词提取所使用逆向文件频率(IDF)文本语料库可以切换成自定义语料库的路径
            用法:jieba.analyse.set_idf_path(file_name) # file_name为自定义语料库的路径

       关键词提取所使用停止词(Stop Words)文本语料库可以切换成自定义语料库的路径
            用法: jieba.analyse.set_stop_words(file_name) # file_name为自定义语料库的路径

 

    #!usr/bin/env python  
    #-*- coding:utf-8-*-  
    import jieba  
    import jieba.analyse  
    import codecs  
    file_path='./data/000000_0'  
    result_file_path="./data/000000_0_result_ifidf"  
    f=open(file_path,'r')  
    f_result=codecs.open(result_file_path,'w+','utf-8')  
    f_result.write('[')  
    texts=f.readlines()  
    num_text=0  
    for text in texts:  
        text_id=texts.split('\t')[0]  
        text_content=texts.split('\t')[3]  
        f_result.write('{\"text_id\": %s\n'% text_id)  
          
        keywords=jieba.analyse.extract_tags(text_content,topK=20,withWeight=True,allowPOS=('ns', 'n', 'vn', 'v'),withFlag=True)  
        f_result.write('\"keyword\":{')  
        num_text+=1  
        for keyword in keywords:  
            (word,flag)=keyword[0]  
            weight=keyword[1]  
            f_result.write('\"word\":%s,\"weight\":%s,\"nature\": %s\n'% (word,weight,flag))  
        f_result.write('}')  
        if num_text%100=0:  
            print "已经处理%s篇文章" % num_text  
    f_result.write(']')  
    print "总共处理%s篇文章" %num_text  
    f_result.close()  
    f.close()  

 

2 基于 TextRank 算法的关键词抽取    

  •       jieba.analyse.textrank(sentence, topK=20, withWeight=False, allowPOS=('ns', 'n', 'vn', 'v')) 直接使用,接口相同,注意默认过滤词性。
  •       jieba.analyse.TextRank() 新建自定义 TextRank 实例

    基本思想:
    1,将待抽取关键词的文本进行分词
    2,以固定窗口大小(默认为5,通过span属性调整),词之间的共现关系,构建图
    3,计算图中节点的PageRank,注意是无向带权

    #!usr/bin/env python  
    #-*- coding:utf-8-*-  
    import jieba  
    import jieba.analyse  
    import codecs  
    file_path='./data/000000_0'  
    result_file_path="./data/000000_0_result_textrank"  
    f=open(file_path,'r')  
    f_result=codecs.open(result_file_path,'w+','utf-8')  
    f_result.write('[\n')  
    texts=f.readlines()  
    num_text=0  
    for text in texts:  
        text_id=texts.split('\t')[0]  
        text_content=texts.split('\t')[3]  
        f_result.write('{\"text_id\": %s\n'% text_id)  
          
        keywords=jieba.analyse.textrank(text_content,topK=20,withWeight=True,allowPOS=('ns', 'n', 'vn', 'v'),withFlag=True)  
        f_result.write('\"keyword\":{')  
        num_text+=1  
        for keyword in keywords:  
            (word,flag)=keyword[0]  
            weight=keyword[1]  
            f_result.write('\"word\":%s,\"weight\":%s,\"nature\": %s\n'% (word,weight,flag))  
        f_result.write('}\n')  
        if num_text%100=0:  
            print "已经处理%s篇文章" % num_text  
    f_result.write(']\n')  
    print "总共处理%s篇文章" num_text  
          
    f_result.close()  
    f.close()  

 

 

引用资源

http://blog.csdn.net/zhangyu132/article/details/51970286

posted on 2018-02-05 18:36  奔跑吧,蜗牛!  阅读(465)  评论(0编辑  收藏  举报

导航