• jieba库的使用


    jieba库的使用:

          jieba库是一款优秀的 Python 第三方中文分词库,jieba 支持三种分词模式:精确模式、全模式和搜索引擎模式,下面是三种模式的特点。

         精确模式:试图将语句最精确的切分,不存在冗余数据,适合做文本分析

         全模式:将语句中所有可能是词的词语都切分出来,速度很快,但是存在冗余数据

          搜索引擎模式:在精确模式的基础上,对长词再次进行切分.

    jieba的使用

    # -*- coding: utf-8 -*-
    import jieba

    seg_str = "好好学习,天天向上。"

    print("/".join(jieba.lcut(seg_str))) # 精简模式,返回一个列表类型的结果
    print("/".join(jieba.lcut(seg_str, cut_all=True))) # 全模式,使用 'cut_all=True' 指定 
    print("/".join(jieba.lcut_for_search(seg_str))) # 搜索引擎模式

    jieba库对英文单词的统计

    # -*- coding: utf-8 -*-

    def get_text():
    txt = open("1.txt", "r", encoding='UTF-8').read()
    txt = txt.lower()
    for ch in '!"#$%&()*+,-./:;<=>?@[\]^_‘{|}~':
    txt = txt.replace(ch, " ") # 将文本中特殊字符替换为空格
    return txt

    file_txt = get_text()
    words = file_txt.split() # 对字符串进行分割,获得单词列表
    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(5):
    word, count = items[i]
    print("{0:<5}->{1:>5}".format(word, count))

    词云的制作

    完成安装jieba , wordcloud ,matplotlib

    (1)打开taglue官网,点击import words,把运行的结果copy过来。
    (2)选择形状,在这里是网上下载的图片进行的导入。
    (3)选择字体。
    (4)点击Visualize生成图片。

    复制代码
    from wordcloud import WordCloud
    import matplotlib.pyplot as plt
    import jieba
     
    def create_word_cloud(filename):
        text = open("哈姆雷特.txt".format(filename)).read()
        
        wordlist = jieba.cut(text, cut_all=True) 
        wl = " ".join(wordlist)
     
    
        wc = WordCloud(
            background_color="black",
            max_words=2000,
            font_path='simsun.ttf',
            height=1200,
            width=1600,
            max_font_size=100,
            random_state=100,
        )
     
        myword = wc.generate(wl) 
        plt.imshow(myword)
        plt.axis("off")
        plt.show()
        wc.to_file('img_book.png')
    
    if __name__ == '__main__':
        create_word_cloud('mytext')
    
    
  • 相关阅读:
    C#中的配置文件自定义解析 [转帖]
    pagevisibility event
    [转] 翻译:web制作、开发人员需知的Web缓存知识
    离开和新的开始
    为什么我要自己写html5游戏引擎
    html5游戏长宽设置
    防止横竖屏时,iphone自动缩放的一段代码
    image to base64 工具
    一段代码,给游戏添加统一的封面和旋屏提示
    程序员应该具备的知识和技术(转)
  • 原文地址:https://www.cnblogs.com/zhoukun520/p/10649666.html
Copyright © 2020-2023  润新知