思路
•使用正则式 "(?x) (?: [w-]+ | [x80-xff]{3} )"获得utf-8文档中的 英文单词和 汉字的列表。
•使用dictionary来记录每个 单词/ 汉字出现的频率,如果出现过则+1,如果没出现则置1。
•将dictionary按照value排序,输出。
源码
代码如下:
#!/usr/bin/python
# -*- coding: utf-8 -*-
#
#author: rex
#blog: http://iregex.org
#filename counter.py
#created: Mon Sep 20 21:00:52 2010
#desc: convert .py file to html with VIM.
import sys
import re
from operator import itemgetter
def readfile(f):
with file(f,"r") as pFile:
return pFile.read()
def divide(c, regex):
#the regex below is only valid for utf8 coding
return regex.findall(c)
def update_dict(di,li):
for i in li:
•使用正则式 "(?x) (?: [w-]+ | [x80-xff]{3} )"获得utf-8文档中的 英文单词和 汉字的列表。
•使用dictionary来记录每个 单词/ 汉字出现的频率,如果出现过则+1,如果没出现则置1。
•将dictionary按照value排序,输出。
源码
代码如下:
#!/usr/bin/python
# -*- coding: utf-8 -*-
#
#author: rex
#blog: http://iregex.org
#filename counter.py
#created: Mon Sep 20 21:00:52 2010
#desc: convert .py file to html with VIM.
import sys
import re
from operator import itemgetter
def readfile(f):
with file(f,"r") as pFile:
return pFile.read()
def divide(c, regex):
#the regex below is only valid for utf8 coding
return regex.findall(c)
def update_dict(di,li):
for i in li: