python实现统计汉字/英文单词数的正则表达式
来源:互联网 发布:威盾内网软件重庆代理 编辑:程序博客网 时间:2024/04/30 00:33
思路
•使用正则式 "(?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:
if di.has_key(i):
di[i]+=1
else:
di[i]=1
return di
def main():
#receive files from bash
files=sys.argv[1:]
#regex compile only once
regex=re.compile("(?x) (?: [w-]+ | [x80-xff]{3} )")
dict={}
#get all words from files
for f in files:
words=divide(readfile(f), regex)
dict=update_dict(dict, words)
#sort dictionary by value
#dict is now a list.
dict=sorted(dict.items(), key=itemgetter(1), reverse=True)
#output to standard-output
for i in dict:
print i[0&#…………………………………………………………………………
•使用正则式 "(?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:
if di.has_key(i):
di[i]+=1
else:
di[i]=1
return di
def main():
#receive files from bash
files=sys.argv[1:]
#regex compile only once
regex=re.compile("(?x) (?: [w-]+ | [x80-xff]{3} )")
dict={}
#get all words from files
for f in files:
words=divide(readfile(f), regex)
dict=update_dict(dict, words)
#sort dictionary by value
#dict is now a list.
dict=sorted(dict.items(), key=itemgetter(1), reverse=True)
#output to standard-output
for i in dict:
print i[0&#…………………………………………………………………………
文献:http://www.verydemo.com/demo_c122_i13599.html
0 0
- python实现统计汉字/英文单词数的正则表达式
- 提取英文单词数的正则表达式
- 使用Python+NLTK实现英文单词词频统计
- 使用Python+NLTK实现英文单词词频统计
- 统计英文单词的个数的python代码
- 汉字的正则表达式
- 汉字的正则表达式
- 汉字的正则表达式
- 汉字的正则表达式
- 汉字的正则表达式
- python 在中文中找出英文单词 正则表达式实例
- 统计文章或句中相同的英文单词数 JS
- 匹配汉字的正则表达式
- 匹配汉字的正则表达式
- 匹配汉字的正则表达式
- Android 汉字的正则表达式
- 匹配汉字的正则表达式
- 正则表达式的汉字匹配
- C/C++数组名与指针区别深入探索
- applet
- 第十四周项目3-立体类族共有的抽象类
- 《QoS在IOS中的实现与应用》目录
- Flash AS3 性能优化注意点
- python实现统计汉字/英文单词数的正则表达式
- JavaScript RegExp 对象
- Scripts:查询数据库中表空间的情况汇总dba_tablespaces.sql
- android中hw_get_module函数分析
- Android引用字体
- 文件读写fopen()
- 嵌入式 tcpdump在Linux下的抓包用法
- LVS+HAProxy的配置
- bootstrp Badge Css【color and align】