NLP:高频词提取

import glob
import jieba


def get_content(path):
    with open(path,'r',encoding='gbk',errors='ignore') as f:
        content=''
        for l in f:
            l=l.strip()
            content+=l
        return content
def get_TF(words,topK=10):
    tf_dic ={}
    for w in words:
        tf_dic[w]=tf_dic.get(w,0)+1
    return sorted(tf_dic.items(),key=lambda x:x[1],reverse=True)[:topK]

def stop_word(path):
    with open(path) as f:
        return [l.strip() for l in f]

if __name__ == '__main__':
    files=glob.glob('./data/*.txt')
    corpus=[get_content(x) for x in files]
    index =1
    split_words =[x for x in jieba.cut(corpus[index]) if x not in stop_word('stop_words.txt')]
    print('content:'+corpus[index])
    print('分词效果:' +'/'.join(split_words))
    print('样本的top10:'+str(get_TF(split_words)))

结果:

/Users/dongyang/AI/anaconda3/python.app/Contents/MacOS/python /Users/dongyang/PycharmProjects/high_freq_word_ex
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值