码迷,mamicode.com
首页 > 其他好文 > 详细

统计 feed 的中英文以及标点符号的数目

时间:2018-10-04 09:15:38      阅读:180      评论:0      收藏:0      [点我收藏+]

标签:\n   rac   cte   key   default   英文   sub   chinese   try   

import feedparser # package to parse rss
import re # package to match regular expression

def getwordscount(url):
    d = feedparser.parse(url)
    sum = 0 # sum for all the words
    sum_en = 0
    sum_zh = 0
    sum_punc = 0
    wordcount_zh = {} # dict to store chinese and its frequency
    wordcount_en = {}
    wordcount_punc = {}
    for e in d[‘entries‘]:
        if ‘summary‘ in e:# every entry should have a ‘summary‘ or a ‘description‘
            summary = e.summary
        else:
            summary = e.description

        words_zh, words_en, words_punc = getwords(e.title + ‘ ‘ + summary)
        for word in words_zh:
            wordcount_zh.setdefault(word, 0)#to set a dict[item] to default 0 if it doesn‘t exist
            wordcount_zh[word] += 1
            sum_zh += 1
        for word in words_en:
            wordcount_en.setdefault(word, 0)
            wordcount_en[word] += 1
            sum_en += 1
        for word in words_punc:
            wordcount_punc.setdefault(word, 0)
            wordcount_punc[word] += 1
            sum_punc += 1
    sum = sum_zh + sum_en + sum_punc
    wordcount_zh = sorted(wordcount_zh.items(), key = lambda d:d[1], reverse = True)# sort the dict with the value, and from the largest to smallest
    wordcount_en = sorted(wordcount_en.items(), key = lambda d:d[1], reverse = True)
    wordcount_punc = sorted(wordcount_punc.items(), key = lambda d:d[1], reverse = True)
    print (d[‘feed‘][‘title‘])
    print (‘Chinese: %d \n %s‘%(sum_zh, wordcount_zh))
    print (‘English: %d \n %s‘%(sum_en, wordcount_en))
    print (‘Punctuation: %d \n %s‘%(sum_punc, wordcount_punc))
    print (‘There are %d words in total‘%(sum))

def getwords(html):
    txt = re.compile(r‘<[^>]+>‘).sub(‘ ‘, html)# re to replace ‘<title><\title>‘ and so on with backspace
    words_zh = re.compile(r‘[\u4E00-\u9FFA]‘).findall(txt)# chinese character is between \u4E00-\u9FFA
    words_en = re.compile(r‘[A-Za-z]+‘).findall(txt)
    words_punc = re.compile(r‘[\s+\.\!\/_,$%^*(+\"\‘]+|[+——!,。?、~@#¥%……&*()]+‘).findall(txt)
    return words_zh, words_en, words_punc

(getwordscount(‘http://feed.cnblogs.com/blog/u/426928/rss‘))

具体采用的是自己的博客园 feed, 随着博客的更新数据会不一样
其中 中文的utf-8的编码范围为\u4E00-\u9FFA

统计 feed 的中英文以及标点符号的数目

标签:\n   rac   cte   key   default   英文   sub   chinese   try   

原文地址:https://www.cnblogs.com/qiulinzhang/p/9739893.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!