无为清净楼资源网 Design By www.qnjia.com
思路
"(?x) (?: [\w-]+ | [\x80-\xff]{3} )"获得utf-8文档中的英文单词和汉字的列表。
"codetitle">复制代码 代码如下:
#!/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], i[1]
if __name__=='__main__':
main()
Tips
由于使用了files=sys.argv[1:] 来接收参数,因此./counter.py file1 file2 ...可以将参数指定的文件的词频累加计算输出。
可以自定义该程序。例如,
"codetitle">复制代码 代码如下:
regex=re.compile("(?x) ( [\w-]+ | [\x80-\xff]{3} )")
words=[w for w in regex.split(line) if w]
这样得到的列表是包含分隔符在内的单词列表,方便于以后对全文分词再做操作。
"<[^>]+","",content),这样的结果对于某些文档更精确。
"(?x) (?: [\w-]+ | [\x80-\xff]{3} )"获得utf-8文档中的英文单词和汉字的列表。
"codetitle">复制代码 代码如下:
#!/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], i[1]
if __name__=='__main__':
main()
Tips
由于使用了files=sys.argv[1:] 来接收参数,因此./counter.py file1 file2 ...可以将参数指定的文件的词频累加计算输出。
可以自定义该程序。例如,
"codetitle">复制代码 代码如下:
regex=re.compile("(?x) ( [\w-]+ | [\x80-\xff]{3} )")
words=[w for w in regex.split(line) if w]
这样得到的列表是包含分隔符在内的单词列表,方便于以后对全文分词再做操作。
"<[^>]+","",content),这样的结果对于某些文档更精确。
标签:
统计汉字,英文单词数
无为清净楼资源网 Design By www.qnjia.com
广告合作:本站广告合作请联系QQ:858582 申请时备注:广告合作(否则不回)
免责声明:本站文章均来自网站采集或用户投稿,网站不提供任何软件下载或自行开发的软件! 如有用户或公司发现本站内容信息存在侵权行为,请邮件告知! 858582#qq.com
免责声明:本站文章均来自网站采集或用户投稿,网站不提供任何软件下载或自行开发的软件! 如有用户或公司发现本站内容信息存在侵权行为,请邮件告知! 858582#qq.com
无为清净楼资源网 Design By www.qnjia.com
暂无评论...
RTX 5090要首发 性能要翻倍!三星展示GDDR7显存
三星在GTC上展示了专为下一代游戏GPU设计的GDDR7内存。
首次推出的GDDR7内存模块密度为16GB,每个模块容量为2GB。其速度预设为32 Gbps(PAM3),但也可以降至28 Gbps,以提高产量和初始阶段的整体性能和成本效益。
据三星表示,GDDR7内存的能效将提高20%,同时工作电压仅为1.1V,低于标准的1.2V。通过采用更新的封装材料和优化的电路设计,使得在高速运行时的发热量降低,GDDR7的热阻比GDDR6降低了70%。
更新日志
2024年09月21日
2024年09月21日
- 命运圣契2024公测最新兑换码大全 命运圣契开服可用兑换码汇总
- 银霞.1979-《回答·兰花草》新加坡限量复黑版[WAV+CUE]
- 王菲.1996-《乐乐精逊SACD日本限量版[WAV+CUE]
- 王菲.1996-《乐乐精逊SACD日本限量版[WAV+CUE]
- 不止黑神话!杭州给原创企业真金白银扶持 打造“杭州游戏”品牌
- 很有多样性:《战锤》首次引入非二元性别角色
- 《少女前线2:追放》项目组将裁员 官方:进一步优化组织结构
- 陈慧琳.2002-闪亮每一天新歌+精选2CD【正东】【WAV+CUE】
- 袁培华.1992-有缘【歌林】【WAV+CUE】
- 王瑞霞.1996-是我太软心【名冠】【WAV+CUE】
- 《谭维维 30首经典流行歌曲精选辑 2CD》[WAV/分轨][1.1GB]
- 《赵传 出道三十年全新大碟 你过得还好吗》[WAV/分轨][460MB]
- 《发烧四大天王 磁性男声HiFi发烧碟 2CD》[WAV/分轨][1GB]
- 黑神话悟空上品鼠司空精魄获取方法一览|上品鼠司空精魄收集攻略
- 《真三国无双:起源》新情报将于9月26日公开,实机演示亮相!