之前说要做一个jieba分词的程序,在网上看了下,最简便简单而且容易理解的方式就是下面这个了。
实话说,思维从面向程序到面向对象,简直是一个飞越。
自己在做一些东西的时候,还是要把思维模式放宽,想问题要多想一层,否则,事倍功半。代码见下:
#!/usr/bin/env python
# -*- coding: utf-8 -*-
import jieba
import jieba.analyse
import codecs
import re
from collections import Counter
class WordCounter(object):
def count_from_file(self, file, top_limit=0):
with codecs.open(file, 'r', 'gbk') as f:
content = f.read()
content = re.sub(r'\s+', r' ', content)
content = re.sub(r'\.+', r' ', content)
return self.count_from_str(content, top_limit=top_limit)
def count_from_str(self, content, top_limit=0):
if top_limit <= 0:
top_limit = 100
tags = jieba.analyse.extract_tags(content, topK=100)
words = jieba.cut(content)
counter = Counter()
for word in words:
if word in tags:
counter[word] += 1
return counter.most_common(top_limit)
if __name__ == '__main__':
counter = WordCounter()
result = counter.count_from_file(r'C:/Users/user1/Desktop/20210112/cipin.txt', top_limit=100)
for k,v in result:
print(k,v)