返回信息流来个题目做做
问题:有一篇英文文章,文本格式。要求统计出这篇文章中所有单词的词频,并列出词频最高的10个单词的出现次数。
规则:连续的字母算一个单词,忽略所有非英文字母,忽略大小写。
比如:输入这篇文章
Bash is an sh-compatible command language interpreter that executes
commands read from the standard input or from a file. Bash also incor-
porates useful features from the Korn and C shells (ksh and csh).
Bash is intended to be a conformant implementation of the Shell and
Utilities portion of the IEEE POSIX specification (IEEE Standard
1003.1). Bash can be configured to be POSIX-conformant by default.
输出:
4 the
4 bash
3 from
3 be
3 and
2 to
2 standard
2 posix
2 of
2 is
语言不限,one-liner最高。
这是一条镜像帖。来源:北邮人论坛 / soft-design / #28068同步于 2008/7/12
该镜像源已超过 30 天没有更新,可能在源站已被删除。
SoftDesign机器人发帖
[各种脚本]统计英文文章中频率最高的10个单词
wks
2008/7/12镜像同步24 回复
订阅后,新回复会通过你的通知中心匿名送达。
9 条回复
记得unix课李sir讲过这道题
【 在 wks (cloverprince) 的大作中提到: 】
: 来个题目做做
: 问题:有一篇英文文章,文本格式。要求统计出这篇文章中所有单词的词频,并列出词频最高的10个单词的出现次数。
: 规则:连续的字母算一个单词,忽略所有非英文字母,忽略大小写。
: ...................
以前写的一个,凑合看吧
【 在 wks 的大作中提到: 】
: 来个题目做做
: 问题:有一篇英文文章,文本格式。要求统计出这篇文章中所有单词的词频,并列出词频最高的10个单词的出现次数。
: 规则:连续的字母算一个单词,忽略所有非英文字母,忽略大小写。
: ...................
附件(829B) freq.pl
为什么?
因为没用map reduce?
【 在 voicespeed 的大作中提到: 】
: 曾在面试中被问到这个问题,遂用C写了一个算法
: 最后被鄙视得五体投地
#!/usr/bin/python
#-*- coding=utf8 -*-
# 我不是 one-liner
import re
filename = 'text.txt'
words = [] # include all the words in the text
dic = {} # include all the words and their count
# read the file and turn all the letters into lower case
textstr = open(filename ,'r').read().lower()
for x in re.compile(r'[^a-z]').sub(' ', textstr).split(' '):
if x is not '':
words.append(x)
# count all the words
for word in words:
if word not in dic:
dic[word] = 0
dic[word] += 1
# output the final result
x = zip(dic.values(), dic.keys())
x.sort()
x.reverse()
for item in x:
if item[0] != 1:
print item[0], item[1]
【 在 wks 的大作中提到: 】
: 来个题目做做
: 问题:有一篇英文文章,文本格式。要求统计出这篇文章中所有单词的词频,并列出词频最高的10个单词的出现次数。
: 规则:连续的字母算一个单词,忽略所有非英文字母,忽略大小写。
: ...................