1. 程式人生 > 其它 >cmake編譯多個資料夾_CMake --- 生成多個執行檔案

cmake編譯多個資料夾_CMake --- 生成多個執行檔案

技術標籤:學習角segmentfault大資料

之前說要做一個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)