import nltk

 def freq_sorted(text,ranklimit):
fd=nltk.FreqDist(text)
cumulative = 0.0
for rank, (word,freq) in enumerate(sorted(fd.items(), key=lambda x: (-1*x[1], x[0]))[:ranklimit]):
cumulative += fd[word] * 100 / fd.N()
print "%3d %6.2f%% %s" % (rank+1, cumulative, word) def test_freq_sorted():
freq_sorted(nltk.corpus.brown.words(),15)

结果为:

  1   5.00% the
2 10.00% ,
3 14.00% .
4 17.00% of
5 19.00% and
6 21.00% to
7 22.00% a
8 23.00% in
9 23.00% that
10 23.00% is
11 23.00% was
12 23.00% for
13 23.00% ``
14 23.00% ''
15 23.00% The

Frequent Distribution sorted by frequency的更多相关文章

  1. [LeetCode] Top K Frequent Words 前K个高频词

    Given a non-empty list of words, return the k most frequent elements. Your answer should be sorted b ...

  2. Top K Frequent Words

    Given a non-empty list of words, return the k most frequent elements. Your answer should be sorted b ...

  3. [Swift]LeetCode692. 前K个高频单词 | Top K Frequent Words

    Given a non-empty list of words, return the k most frequent elements. Your answer should be sorted b ...

  4. LeetCode - Top K Frequent Words

    Given a non-empty list of words, return the k most frequent elements. Your answer should be sorted b ...

  5. [leetcode]692. Top K Frequent Words K个最常见单词

    Given a non-empty list of words, return the k most frequent elements. Your answer should be sorted b ...

  6. #Leetcode# 692. Top K Frequent Words

    https://leetcode.com/problems/top-k-frequent-words/ Given a non-empty list of words, return the k mo ...

  7. 692. Top K Frequent Words

    Given a non-empty list of words, return the k most frequent elements. Your answer should be sorted b ...

  8. [LC] 692. Top K Frequent Words

    Given a non-empty list of words, return the k most frequent elements. Your answer should be sorted b ...

  9. 【LeetCode】692. Top K Frequent Words 解题报告(Python)

    [LeetCode]692. Top K Frequent Words 解题报告(Python) 标签: LeetCode 题目地址:https://leetcode.com/problems/top ...

随机推荐

  1. JS-将input输入框写入的小写字母全部转换成为大写字母的JS代码

    <input name="htmer" type="text" onkeyup="this.value=this.value.toUpperCa ...

  2. WPF之让ListView中的CheckBox居中显示

    第一步:在资源中定义一个居中的样式: <Window.Resources> <Style x:Key="ListViewItemStyle" TargetType ...

  3. ANT 配置和安装 1

    --"JAVA_HOME" (JDK HOME)C:\Program Files\Java\jdk1.7.0_79 -- "ANT_HOME" (ANT HOM ...

  4. mybatis 的一点问题

    写法1:   public User queryUserByUsername(String username); 写法2:   public User queryUserByUsername(@Par ...

  5. OpenGL中shader读取实现

    1.需要shader在OpenGL中工作,必须经过如下过程 2.代码实现 /********** * loadshader.h **********/ #pragma once #define _CR ...

  6. css 重新学习系列(2)

    摘自: http://www.cnblogs.com/liuzhaoyang/articles/3289456.html Position定位:relative | absolute 定位一直是WEB ...

  7. JAVA-面向对象--封装

    1. 如果一个类中没有定义构造函数,会自动加上一个空参的默认构造函数 如果定义了一个构造函数,默认的空参构造函数就没有了. 2. this 当成员变量与局部变量重名的时候,可以使用this来区分.th ...

  8. Bom2016/4/21

    Browser Object Model 浏览器对象模型 open(地址,打开方式) open('http://www.baidu.com','_self')本窗口打开百度 open('http:// ...

  9. UVALive 2522 Chocolate(概率DP)

    思路:定义DP方程dp[i][j]标记选到第i个巧克力的时候,桌面上还剩下j个巧克力,状态转移有两个方向,dp[i-1][j-1],dp[i-1]lj+1],分别表示桌面上多了一个和消了一个,乘上需要 ...

  10. Callable、Future和FutureTask区别

    在前面的文章中我们讲述了创建线程的2种方式,一种是直接继承Thread,另外一种就是实现Runnable接口. 这2种方式都有一个缺陷就是:在执行完任务之后无法获取执行结果. 如果需要获取执行结果,就 ...