第十章 Python第三方库概览

10.1 Python第三方库的获取和安装

10.1.1 pip工具安装
pip install <拟安装库名>
10.1.2 自定义安装
10.1.3 文件安装
10.1.4 pip工具使用
pip -h
pip uninstall <拟卸载库名>
pip list
pip show <拟查询库名>
pip download <拟下载库名>
pip search <拟查询关键字>

10.2 PyInstaller库概述

pip install PyInstaller

10.3 PyInstaller库与程序打包

PyInstaller <Python源程序文件名>
PyInstaller -F <Python源程序文件名>

10.4 jieba库概述

pip install jieba

10.5 jieba库与中文分词

10.6 wordcloud库概述

pip install wordcloud
from wordcloud import WordCloud

txt = "I like python. I'm learning python!"
wordcloud = WordCloud().generate(txt)
wordcloud.to_file("testcloud.png")

10.7 wordcloud库与可视化词云

import jieba
from wordcloud import WordCloud

txt = "程序设计语言是计算机能够理解和识别用户操作意图的一种交互体系,它按照特定规则组织计算机指令,是计算机能够自动进行各种运算处理。"
words = jieba.lcut(txt)  # 精确分词
newtxt = " ".join(words)  # 空格拼接
wordcloud = WordCloud(font_path="msyh.ttc").generate(newtxt)
wordcloud.to_file("词云中文例子图.png")  # 保存图片
from wordcloud import WordCloud
from scipy.misc import imread

mask = imread("AliceMask.png")
with open("AliceInWonderland.txt", mode="r", encoding="utf-8") as file:
    text = file.read()
    wordcloud = WordCloud(background_color="white", width=800, height=600, max_words=200, max_font_size=80,mask=mask).generate(text)
wordcloud.to_file("AliceInWonderland.png")

10.8 实例解析——《红楼梦》人物出场词云

10.8.1 《红楼梦》人物出场统计
import jieba

excludes = {"什么", "一个", "我们", "那里", "你们", "如今", "说道", "知道", "老太太", "起来", "姑娘", "这里", "出来", "他们", "众人", "自己", "一面", "太太", "只见", "怎么", "奶奶", "两个", "没有", "不是", "不知", "这个", "听见"}
f = open("红楼梦.txt", "r", encoding="utf-8")
txt = f.read()
f.close()
words = jieba.lcut(txt)
counts = {}
for word in words:
    if len(word) == 1:  # 排除单个字符的分词结果
        continue
    else:
        counts[word] = counts.get(word, 0) + 1
for word in excludes:
    del (counts[word])
items = list(counts.items())
items.sort(key=lambda x: x[1], reverse=True)
for i in range(5):
    word, count = items[i]
    print("{0:<10}{1:>5}".format(word, count))
10.8.2 《红楼梦》人物出场词云
import jieba
from wordcloud import WordCloud

excludes = {"什么", "一个", "我们", "那里", "你们", "如今", "说道", "知道", "老太太", "起来", "姑娘", "这里", "出来", "他们", "众人", "自己", "一面", "太太", "只见", "怎么", "奶奶", "两个", "没有", "不是", "不知", "这个", "听见"}
f = open("红楼梦.txt", "r", encoding="utf-8")
txt = f.read()
f.close()
words = jieba.lcut(txt)
newtxt = ' '.join(words)
wordcloud = WordCloud(background_color="white", width=800, height=600, font_path="msyh.ttc", max_words=5,max_font_size=80, stopwords=excludes,).generate(newtxt)
wordcloud.to_file('红楼梦基本词云.png')

习题10

一、选择题
  1. 下面关于pip安装方式的说法中错误的是:______

    A pip工具几乎就可以安装任何Python第三方库

    B pip的download子命令可以下载第三方库的安装包并安装

    C pip可以安装已经下载的.whl安装文件

    D Python第三方库有三种安装方式,其中pip是最常用的方式

    正确答案:B

  2. 下面关于jieba库的描述中错误的是:______

    A jieba库是一个中文分词工具

    B jieba库利用基于概率的分词方法

    C jieba库提供增加自定义单词的功能

    D jieba库的分词模式分为模糊模式、精确模式、全模式和搜索引擎模式

    正确答案:D

  3. jieba库中各搜索引擎分词模式的作用是:______

    A 精确地切开句子,适合文本分析

    B 将句子中给所有成词的词语都扫描出来

    C 对长词再次切分,提高召回率

    D 速度快,清除歧义

    正确答案:D

  4. 下面关于wordcloud库的描述中错误的是:______

    A wordcloud库是一个用于生成词云的库

    B wordcloud库默认分词方法是根据空格分词

    C wordcloud库生成中文词云时输出乱码,故无法用于制作中文词云

    D wordcloud库的大多数方法都封装在WordCloud类里面

    正确答案:C

  5. 使用PyInstaller打包程序时,想要在dist文件夹中只生成一个单独的exe文件,所需参数是:______

    A -version B -clean C -onedir D -F

    正确答案:D

  6. 使用PyInstaller打包程序时,–path命令的作用是:______

    A 指定代码文件所在目录

    B 指定PyInstaller所在目录

    C 指定代码所依赖非标准库的路径

    D 指定生成exe文件的目录

    正确答案:C

  7. 关于PyInstaller,下列说法中错误的是:______

    A PyInstaller是用于将Python脚本打包成可执行文件的工具

    B PyInstaller使用起来非常方便,在IDLE交互式环境下输入相应命令即可

    C 使用-p添加多个非标准库的路径信息时,既可以多次使用-p,也可以用分号分割路径

    D --clean参数用于清理打包过程中的临时文件

    正确答案:B

  8. jieba库函数jieba.lcut()返回值的类型是:______

    A 列表 B 迭代器 C 字符串 D 元组

    正确答案:A

  9. 以下不是pip合法命令的是:______

    A install B hash C help D update

    正确答案:B

  10. 使用PyInstaller打包含有中文字符的代码文件时,关于代码文件编码方式的说法中正确的是:______

    A 必须是UTF-8,无BOM编码格式

    B 必须是UTF-8,无BOM编码格式或者ANSI编码格式

    C 可以是任何合法的编码格式

    D 必须是GBK编码格式

    正确答案:A

二、编程题
  1. 使用jieba.cut()对“Python是最有意思的编程语言”进行分词,输出结果,并将该迭代器转换为列表类型。

    import jieba
    
    txt = "Python是最有意思的编程语言"
    word = jieba.lcut(txt)
    print(word)
    
    # 运行结果
    ['Python', '是', '最', '有意思', '的', '编程语言']
    
  2. 使用jieba.cut()对“今天晚上我吃了意大利面”进行分词,输出结果,并使“意大利面”作为一个词出现在结果在。

    import jieba
    
    txt = "今天晚上我吃了意大利面"
    jieba.add_word("意大利面")
    word = jieba.lcut(txt)
    print(word)
    
    # 运行结果
    ['今天', '晚上', '我', '吃', '了', '意大利面']
    
  3. 自选一篇报告或者演讲稿,利用jieba分析出其词频排前5的关键词。

    import jieba
    
    f = open("example.txt", "r", encoding="utf-8")
    txt = f.read()
    f.close()
    words  = jieba.lcut(txt)
    counts = {}
    for word in words:
        if len(word) == 1:  #排除单个字符的分词结果
            continue
        else:
            counts[word] = counts.get(word,0) + 1
    items = list(counts.items())
    items.sort(key=lambda x:x[1], reverse=True) 
    for i in range(15):
        word, count = items[i]
        print ("{0:<10}{1:>5}".format(word, count))
    
06-04 23:09