zoukankan      html  css  js  c++  java
  • python自然语言处理学习笔记2

    基础语法

    搜索文本----词语索引使我们看到词的上下

    text1.concordance("monstrous")


    词出现在相似的上下文中

    text1.similar("monstrous")


    函数common_contexts允许我们研究两个或两个以上的词共同的上下文

    text2.common_contexts(["monstrous", "very"])


    以判断词在文本中的位置,用离散图表示  ,每一个竖线代表一个单词,每一行代表整个文本

    text4.dispersion_plot(["citizens", "democracy", "freedom", "duties", "America"])


    不同风格产生一些随机文本

    text3.generate()


    获取文本长度

    len(text3)


    获得text3 的词汇表(set为集合,元素不可重复)

    set(text3)


    得到一个词汇项的排序表(排序表中大写字母出现在小写字母之前)

    sorted(set(text3))


    了每个字平均被使用次数(使用的是浮点除法)

    from __future__ import division

    len(text3) / len(set(text3))


    计数一个词在文本中出现的次数,计算一个特定的词在文本中占据的百分比。

    text3.count("smote")

    100 * text4.count('a') / len(text4)


    了每个字平均被使用次数(函数)

    def lexical_diversity(text):

    return len(text) / len(set(text))


    百分比

    def percentage(count, total):

    return 100 * count / total


    链表(list,也叫列表)(nltk.book 已经为你定义了一些链表 sent2~sent9)

    sent1 = ['Call', 'me', 'Ishmael', '.']


    对链表使用Python 加法运算----连接----它将多个链表组合为一个链表

    ['Monty', 'Python'] + ['and', 'the', 'Holy', 'Grail']

    sent4 + sent1


    向链表中增加一个元素----追加

    sent1.append("Some")


    索引列表(索引从零开始)

    text4[173]


    获取子链表----切片

    text5[16715:16735]


    切片5:8 包含索引5,6 和7    m:n 表示元素m...n-1

    sent = ['word1', 'word2', 'word3', 'word4', 'word5','word6', 'word7', 'word8', 'word9', 'word10']

    sent[5:8]


    如果切片从链表第一个元素开始,我们可以省略第一个数字;如果切片到链表最后一个元素处结尾,我们可以省略第二个数字:

    sent[:3]

    text2[141525:]


    从倒数第二个开始到最后

    tokens[-2:]


    通过指定它的索引值来修改链表中的元素

    sent = ['word1', 'word2', 'word3', 'word4', 'word5','word6', 'word7', 'word8', 'word9', 'word10']

    sent[0] = 'First'


    也可以用新内容替换掉一整个片段

    sent[1:9] = ['Second', 'Third']


    字符串(切片,乘法,加法,连接,分割)

    name = 'Monty'

    name[0]

    name[:4]

    name * 2

    name + '!'

    ' '.join(['Monty', 'Python'])

    'Monty Python'.split()



  • 相关阅读:
    SetThreadAffinityMask设置线程亲缘性
    Delphi 获取北京时间(通过百度和timedate网站)
    delphi 实现微信开发
    翻书的效果:FMX.TSwipeTransitionEffect Animation
    [每日一题] OCP1z0-047 :2013-07-15 drop column
    Delphi获取当前系统时间(使用API函数GetSystemTime)
    Delphi代码中嵌入ASM代码
    Delphi Jpg和Gif转Bmp
    Delphi RichEdit的内容保存为图片
    Delphi 实现任务栏多窗口图标显示
  • 原文地址:https://www.cnblogs.com/yan456jie/p/5369433.html
Copyright © 2011-2022 走看看