智联招聘的python岗位数据结巴分词(二)

上次获取第一次分词之后的内容了

但是数据数据量太大了 ,这时候有个模块就派上用场了collections模块的Counter类

Counter类:为hashable对象计数,是字典的子类。

然后使用most_common方法返回一个TopN列表。如果n没有被指定,则返回所有元素。当多个元素计数值相同时,排列是无确定顺序的。

def get_top_words(topn):

    # 从out.txt中读取带词性的分词结果列表
    words_with_attr = read_result()

    # 要过滤掉的词性列表
    stop_attr = ['a', 'ad', 'b', 'c', 'd', 'f', 'df', 'm', 'mq', 'p', 'r', 'rr', 's', 't', 'u', 'v', 'z']

    # 过滤掉不需要的词性的词
    words = [x[0] for x in words_with_attr if x[1] not in stop_attr]

    # 获取topn的词并存入文件topn_words.txt,top_words_with_freq为一个字典,在生成词云的时候会用到,格式为:
    # {'aa':1,'bb':2,'cc':3}
    top_words_with_freq = get_topn_words(words=words, topn=topn)

    return top_words_with_freq
def get_topn_words(words, topn):
    c = Counter(words).most_common(topn) #获取topn列表
    top_words_with_freq = {}
    with open('top{0}_words.txt'.format(topn), 'w+') as f:
        for x in c:
            f.write('{0},{1}\n'.format(x[0], x[1]))
            top_words_with_freq.setdefault(x[0],x[1])
    return top_words_with_freq

  

接下来就是把这个topn的列表进行分词了

 

posted @ 2018-03-05 23:21  公众号python学习开发  阅读(280)  评论(0编辑  收藏  举报