zoukankan      html  css  js  c++  java
  • Jieba库使用和好玩的词云

    jieba库的使用:

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

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

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

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

     (2)、jieba库常用函数

    函数 描述
    jieba.lcut(s) 精确模式,返回一个列表类型的分词结果
    >>>jieba.lcut("中国是一个伟大的国家")
    ['中国', '', '一个', '伟大', '', '国家']
    jieba.lcut(s,
    cut_all=True)
    全模式,返回一个列表类型的分词结果,存在冗余
    >>>jieba.lcut("中国是一个伟大的国家",cut_all=True)
    ['中国', '国是', '一个', '伟大', '', '国家']
     
     
     
     (3)、jieba库的安装
             Windows 下使用命令安装:在联网状态下,在命令行下输入 pip install jieba 进行安装,安装完成后会提示安装成功 
            在 pyCharm 中安装:打开 settings,搜索 Project Interpreter,在右边的窗口选择 + 号,点击后在搜索框搜索 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))

    四,好玩的词云

    import jieba

    seg_list = jieba.cut("我来到北京清华大学", cut_all=True, HMM=False)
    print("Full Mode: " + "/ ".join(seg_list)) # 全模式

    seg_list = jieba.cut("我来到北京清华大学", cut_all=False, HMM=True)
    print("Default Mode: " + "/ ".join(seg_list)) # 默认模式

    seg_list = jieba.cut("他来到了网易杭研大厦", HMM=False)
    print(", ".join(seg_list))

    seg_list = jieba.cut_for_search("小明硕士毕业于中国科学院计算所,后在日本京都大学深造", HMM=False) # 搜索引擎模式
    print(", ".join(seg_list))

    输出为:

    以上是我对jieba库使用与安装的全部了解,对词云的了解不是很清楚。

  • 相关阅读:
    Chapter 03Using SingleRow Functions to Customize Output(03)
    Chapter 03Using SingleRow Functions to Customize Output(01)
    Chapter 04Using Conversion Functions and Conditional ExpressionsNesting Functions
    Chapter 04Using Conversion Functions and Conditional ExpressionsGeneral Functions
    Chapter 11Creating Other Schema Objects Index
    传奇程序员John Carmack 访谈实录 (zz.is2120)
    保持简单纪念丹尼斯里奇(Dennis Ritchie) (zz.is2120.BG57IV3)
    王江民:传奇一生 (zz.is2120)
    2011台湾游日月潭
    2011台湾游星云大师的佛光寺
  • 原文地址:https://www.cnblogs.com/hch123/p/hch123.html
Copyright © 2011-2022 走看看