NLTK笔记1

来源:互联网 发布:网络协议概念三要素 编辑:程序博客网 时间:2024/05/20 04:51

1、链表的连接

  • list1+list2
  • list1.append(“word”)

2、链表的索引

  • list[10]
  • list.index(“word”)//链表的第一个”word”的位置
  • list.count(“word”)

3、频率分布

  • fdist1 = FreqDist(text1)
  • dist= FreqDist(samples) 创建包含给定样本的频率分布
  • fdist.inc(sample) 增加样本
  • fdist[‘monstrous’] 计数给定样本出现的次数
  • fdist.freq(‘monstrous’) 给定样本的频率
  • fdist.N() 样本总数
  • fdist.keys() 以频率递减顺序排序的样本链表
  • forsample in fdist: 以频率递减的顺序遍历样本
  • fdist.max() 数值最大的样本
  • fdist.tabulate() 绘制频率分布表
  • fdist.plot() 绘制频率分布
  • dist.plot(cumulative=True) 绘制累积频率分布图
  • fdist1< fdist2 测试样本在 fdist1中出现的频率是否小于 fdist2

4、字符串操作

  • .startswith(t) 测试 s是否以t开头
  • s.endswith(t) 测试 s是否以t结尾
  • t in s 测试 s是否包含t
  • s.islower() 测试 s中所有字符是否都是小写字母
  • s.isupper() 测试 s中所有字符是否都是大写字母
  • s.isalpha() 测试 s中所有字符是否都是字母
  • s.isalnum() 测试 s中所有字符是否都是字母或数字
  • s.isdigit() 测试 s中所有字符是否都是数字
  • s.istitle() 测试 s是否首字母大写( s中所有的词都首字母大写
    5、for循环

  • sorted([item foritem in set(text6)if item.istitle()])
    6、语法规则

  • 所有的Python控制结构都以冒号结尾。冒号表示当前语句与后面的缩进块有关联

7、访问语料库

  • 古腾堡项目的语料库(http://www.gutenberg.org/)
  • nltk.corpus.gutenberg.fileids()//返回所有的文件名标识符
    [ ‘austen-emma.txt’, ‘austen-persuasion.txt’, ‘austen-sense.txt’, ‘bible-kjv.txt’,
    ‘blake-poems.txt’, ‘bryant-stories.txt’, ‘burgess-busterbrown.txt’,
    ‘carroll-alice.txt’, ‘chesterton-ball.txt’, ‘chesterton-brown.txt’,
    ‘chesterton-thursday.txt’, ‘edgeworth-parents.txt’, ‘melville-moby_dick.txt’,
    ‘milton-paradise.txt’, ‘shakespeare-caesar.txt’, ‘shakespeare-hamlet.txt’,
    ‘shakespeare-macbeth.txt’, ‘whitman-leaves.txt’…]
  • emma = nltk.corpus.gutenberg.words(‘austen-emma.txt’)//这篇文件的具体内容
  • emma = gutenberg.raw(“austen-emma.txt”);#输出的是整个文章
    这里写图片描述

note:这里是对nltk.corpus进行研究,之前的text是对nltk中的九个文本进行的研究,
例如用text1.concordance()命令对文本内容进行检索,这里如果想像这样对文本进行操作,
则emma = nltk.Text(nltk.corpus.gutenberg.words(‘austen-emma.txt’))
emma.concordance(“surprize”)

0 0