• python 词云


    【学习笔记】wordCloud的基本使用
    https://blog.csdn.net/cskywit/article/details/79285988

           最近研究NLP,结果用wordCloud展示效果很好,学习了一下,其实很简单,github上有英文词云的实例可以参考,详见:https://amueller.github.io/word_cloud/ 。下面分Wordcloud类的使用,github上的英文词云例子,自行做的中文词云例子分别记录一下:

    一、WordCloud类

    1.  

      class wordcloud.WordCloud(
    2.  
      font_path=None,
    3.  
      width=400,
    4.  
      height=200,
    5.  
      margin=2,
    6.  
      ranks_only=None,
    7.  
      prefer_horizontal=0.9,
    8.  
      mask=None, scale=1,
    9.  
      color_func=None,
    10.  
      max_words=200,
    11.  
      min_font_size=4,
    12.  
      stopwords=None,
    13.  
      random_state=None,
    14.  
      background_color='black',
    15.  
      max_font_size=None,
    16.  
      font_step=1,
    17.  
      mode='RGB',
    18.  
      relative_scaling=0.5,
    19.  
      regexp=None,
    20.  
      collocations=True,
    21.  
      colormap=None,
    22.  
      normalize_plurals=True)
    23.  
       
    24.  
      ##参数含义如下:
    25.  
      font_path : string //字体路径,需要展现什么字体就把该字体路径+后缀名写上,如:font_path = '黑体.ttf'
    26.  
      width : int (default=400) //输出的画布宽度,默认为400像素
    27.  
      height : int (default=200) //输出的画布高度,默认为200像素
    28.  
      prefer_horizontal : float (default=0.90) //词语水平方向排版出现的频率,默认 0.9 (所以词语垂直方向排版出现频率为 0.1 )
    29.  
      mask : nd-array or None (default=None) //如果参数为空,则使用二维遮罩绘制词云。如果 mask 非空,设置的宽高值将被忽略,遮罩形状被 mask 取代。除全白(#FFFFFF)的部分将不会绘制,其余部分会用于绘制词云。如:bg_pic = imread('读取一张图片.png'),背景图片的画布一定要设置为白色(#FFFFFF),然后显示的形状为不是白色的其他颜色。可以用ps工具将自己要显示的形状复制到一个纯白色的画布上再保存,就ok了。
    30.  
      scale : float (default=1) //按照比例进行放大画布,如设置为1.5,则长和宽都是原来画布的1.5倍。
    31.  
      min_font_size : int (default=4) //显示的最小的字体大小
    32.  
      font_step : int (default=1) //字体步长,如果步长大于1,会加快运算但是可能导致结果出现较大的误差。
    33.  
      max_words : number (default=200) //要显示的词的最大个数
    34.  
      stopwords : set of strings or None //设置需要屏蔽的词,如果为空,则使用内置的STOPWORDS
    35.  
      background_color : color value (default=”black”) //背景颜色,如background_color='white',背景颜色为白色。
    36.  
      max_font_size : int or None (default=None) //显示的最大的字体大小
    37.  
      mode : string (default=”RGB”) //当参数为“RGBA”并且background_color不为空时,背景为透明。
    38.  
      relative_scaling : float (default=.5) //词频和字体大小的关联性
    39.  
      color_func : callable, default=None //生成新颜色的函数,如果为空,则使用 self.color_func
    40.  
      regexp : string or None (optional) //使用正则表达式分隔输入的文本
    41.  
      collocations : bool, default=True //是否包括两个词的搭配
    42.  
      colormap : string or matplotlib colormap, default=”viridis” //给每个单词随机分配颜色,若指定color_func,则忽略该方法。
    #方法:
    fit_words(frequencies)  //根据词频生成词云
    generate(text)  //根据文本生成词云
    1.  
      generate_from_frequencies(frequencies[, ...]) //根据词频生成词云
    2.  
      generate_from_text(text) //根据文本生成词云
    3.  
      process_text(text) //将长文本分词并去除屏蔽词(此处指英语,中文分词还是需要自己用别的库先行实现,使用上面的 fit_words(frequencies) )
    4.  
      recolor([random_state, color_func, colormap]) //对现有输出重新着色。重新上色会比重新生成整个词云快很多。
    5.  
      to_array() //转化为 numpy array
    6.  
      to_file(filename) //输出到文件

    二、GitHub上的英文词云例子

    1、简单例子

    1.  
      from wordcloud import WordCloud
    2.  
      import matplotlib.pyplot as plt
    3.  
       
    4.  
      f = open('../txt/alice.txt', 'r').read()
    5.  
      wordcloud = WordCloud(background_color="white",width=1000,height=860,margin=2).generate(f)
    6.  
      plt.imshow(wordcloud)
    7.  
      plt.axis("off")
    8.  
      plt.show()
    9.  
      wordcloud.to_file('../picture/example1.png')

    运行效果:



    2.设置字体颜色例子

    1.  
      # -*- encoding:utf-8 -*-
    2.  
      #设置字体颜色例子
    3.  
      """
    4.  
      Colored by Group Example
    5.  
      ========================
    6.  
       
    7.  
      Generating a word cloud that assigns colors to words based on
    8.  
      a predefined mapping from colors to words
    9.  
      """
    10.  
       
    11.  
      from wordcloud import (WordCloud, get_single_color_func)
    12.  
      import matplotlib.pyplot as plt
    13.  
       
    14.  
       
    15.  
      class SimpleGroupedColorFunc(object):
    16.  
      """Create a color function object which assigns EXACT colors
    17.  
      to certain words based on the color to words mapping
    18.  
       
    19.  
      Parameters
    20.  
      ----------
    21.  
      color_to_words : dict(str -> list(str))
    22.  
      A dictionary that maps a color to the list of words.
    23.  
       
    24.  
      default_color : str
    25.  
      Color that will be assigned to a word that's not a member
    26.  
      of any value from color_to_words.
    27.  
      """
    28.  
       
    29.  
      def __init__(self, color_to_words, default_color):
    30.  
      self.word_to_color = {word: color
    31.  
      for (color, words) in color_to_words.items()
    32.  
      for word in words}
    33.  
       
    34.  
      self.default_color = default_color
    35.  
       
    36.  
      def __call__(self, word, **kwargs):
    37.  
      return self.word_to_color.get(word, self.default_color)
    38.  
       
    39.  
       
    40.  
      class GroupedColorFunc(object):
    41.  
      """Create a color function object which assigns DIFFERENT SHADES of
    42.  
      specified colors to certain words based on the color to words mapping.
    43.  
       
    44.  
      Uses wordcloud.get_single_color_func
    45.  
       
    46.  
      Parameters
    47.  
      ----------
    48.  
      color_to_words : dict(str -> list(str))
    49.  
      A dictionary that maps a color to the list of words.
    50.  
       
    51.  
      default_color : str
    52.  
      Color that will be assigned to a word that's not a member
    53.  
      of any value from color_to_words.
    54.  
      """
    55.  
       
    56.  
      def __init__(self, color_to_words, default_color):
    57.  
      self.color_func_to_words = [
    58.  
      (get_single_color_func(color), set(words))
    59.  
      for (color, words) in color_to_words.items()]
    60.  
       
    61.  
      self.default_color_func = get_single_color_func(default_color)
    62.  
       
    63.  
      def get_color_func(self, word):
    64.  
      """Returns a single_color_func associated with the word"""
    65.  
      try:
    66.  
      color_func = next(
    67.  
      color_func for (color_func, words) in self.color_func_to_words
    68.  
      if word in words)
    69.  
      except StopIteration:
    70.  
      color_func = self.default_color_func
    71.  
       
    72.  
      return color_func
    73.  
       
    74.  
      def __call__(self, word, **kwargs):
    75.  
      return self.get_color_func(word)(word, **kwargs)
    76.  
       
    77.  
       
    78.  
      text = """The Zen of Python, by Tim Peters
    79.  
      Beautiful is better than ugly.
    80.  
      Explicit is better than implicit.
    81.  
      Simple is better than complex.
    82.  
      Complex is better than complicated.
    83.  
      Flat is better than nested.
    84.  
      Sparse is better than dense.
    85.  
      Readability counts.
    86.  
      Special cases aren't special enough to break the rules.
    87.  
      Although practicality beats purity.
    88.  
      Errors should never pass silently.
    89.  
      Unless explicitly silenced.
    90.  
      In the face of ambiguity, refuse the temptation to guess.
    91.  
      There should be one-- and preferably only one --obvious way to do it.
    92.  
      Although that way may not be obvious at first unless you're Dutch.
    93.  
      Now is better than never.
    94.  
      Although never is often better than *right* now.
    95.  
      If the implementation is hard to explain, it's a bad idea.
    96.  
      If the implementation is easy to explain, it may be a good idea.
    97.  
      Namespaces are one honking great idea -- let's do more of those!"""
    98.  
       
    99.  
      # Since the text is small collocations are turned off and text is lower-cased
    100.  
      wc = WordCloud(collocations=False).generate(text.lower())
    101.  
       
    102.  
      color_to_words = {
    103.  
      # words below will be colored with a green single color function
    104.  
      '#00ff00': ['beautiful', 'explicit', 'simple', 'sparse',
    105.  
      'readability', 'rules', 'practicality',
    106.  
      'explicitly', 'one', 'now', 'easy', 'obvious', 'better'],
    107.  
      # will be colored with a red single color function
    108.  
      'red': ['ugly', 'implicit', 'complex', 'complicated', 'nested',
    109.  
      'dense', 'special', 'errors', 'silently', 'ambiguity',
    110.  
      'guess', 'hard']
    111.  
      }
    112.  
       
    113.  
      # Words that are not in any of the color_to_words values
    114.  
      # will be colored with a grey single color function
    115.  
      default_color = 'grey'
    116.  
       
    117.  
      # Create a color function with single tone
    118.  
      # grouped_color_func = SimpleGroupedColorFunc(color_to_words, default_color)
    119.  
       
    120.  
      # Create a color function with multiple tones
    121.  
      grouped_color_func = GroupedColorFunc(color_to_words, default_color)
    122.  
       
    123.  
      # Apply our color function
    124.  
      wc.recolor(color_func=grouped_color_func)
    125.  
      wc.to_file('../picture/example2_colorChanged.png')
    126.  
      # Plot
    127.  
      plt.figure()
    128.  
      plt.imshow(wc, interpolation="bilinear")
    129.  
      plt.axis("off")
    130.  
      plt.show()

    3.利用背景图片生成词云,设置停用词

    1.  
      # -*- encoding:utf-8 -*-.
    2.  
      #利用背景图片生成词云,设置停用词
    3.  
      """
    4.  
      Image-colored wordcloud
    5.  
      =======================
    6.  
       
    7.  
      You can color a word-cloud by using an image-based coloring strategy
    8.  
      implemented in ImageColorGenerator. It uses the average color of the region
    9.  
      occupied by the word in a source image. You can combine this with masking -
    10.  
      pure-white will be interpreted as 'don't occupy' by the WordCloud object when
    11.  
      passed as mask.
    12.  
      If you want white as a legal color, you can just pass a different image to
    13.  
      "mask", but make sure the image shapes line up.
    14.  
      """
    15.  
       
    16.  
      from os import path
    17.  
      from PIL import Image
    18.  
      import numpy as np
    19.  
      import matplotlib.pyplot as plt
    20.  
       
    21.  
      from wordcloud import WordCloud, STOPWORDS, ImageColorGenerator
    22.  
       
    23.  
      #源码所在目录
    24.  
      d = path.dirname(__file__)
    25.  
       
    26.  
      # Read the whole text.
    27.  
      text = open(path.join(d, '../txt/alice.txt')).read()
    28.  
       
    29.  
      # read the mask / color image taken from
    30.  
      # http://jirkavinse.deviantart.com/art/quot-Real-Life-quot-Alice-282261010
    31.  
      alice_coloring = np.array(Image.open(path.join(d, "../bgPic/alice_color.png")))
    32.  
      stopwords = set(STOPWORDS)
    33.  
      stopwords.add("said")
    34.  
       
    35.  
      wc = WordCloud(background_color="white", max_words=2000, mask=alice_coloring,
    36.  
      stopwords=stopwords, max_font_size=40, random_state=42)
    37.  
      # generate word cloud
    38.  
      wc.generate(text)
    39.  
       
    40.  
      # create coloring from image
    41.  
      image_colors = ImageColorGenerator(alice_coloring)
    42.  
       
    43.  
      # show
    44.  
      plt.imshow(wc, interpolation="bilinear")
    45.  
      plt.axis("off")
    46.  
      plt.figure()
    47.  
      wc.to_file(path.join(d, '../picture/alice_colored1.png'))
    48.  
      # recolor wordcloud and show
    49.  
      # we could also give color_func=image_colors directly in the constructor
    50.  
      plt.imshow(wc.recolor(color_func=image_colors), interpolation="bilinear")
    51.  
      wc.to_file(path.join(d, '../picture/alice_colored2.png'))
    52.  
      plt.axis("off")
    53.  
      plt.figure()
    54.  
      plt.imshow(alice_coloring, cmap="gray", interpolation="bilinear")
    55.  
      plt.axis("off")
    56.  
      wc.to_file(path.join(d, '../picture/alice_colored3.png'))
    57.  
      plt.show()

    运行效果:

    4.使用遮罩,生成任意形状的词云

    1.  
      # -*- encoding:utf-8 -*-.
    2.  
      """
    3.  
      Masked wordcloud
    4.  
      ================
    5.  
       
    6.  
      Using a mask you can generate wordclouds in arbitrary shapes.
    7.  
      """
    8.  
       
    9.  
      from os import path
    10.  
      from PIL import Image
    11.  
      import numpy as np
    12.  
      import matplotlib.pyplot as plt
    13.  
       
    14.  
      from wordcloud import WordCloud, STOPWORDS
    15.  
       
    16.  
      d = path.dirname(__file__)
    17.  
       
    18.  
      # Read the whole text.
    19.  
      text = open(path.join(d, '../txt/alice.txt')).read()
    20.  
       
    21.  
      # read the mask image
    22.  
      # taken from
    23.  
      # http://www.stencilry.org/stencils/movies/alice%20in%20wonderland/255fk.jpg
    24.  
      alice_mask = np.array(Image.open(path.join(d, "../bgPic/alice_mask.png")))
    25.  
       
    26.  
      stopwords = set(STOPWORDS)
    27.  
      stopwords.add("said")
    28.  
       
    29.  
      wc = WordCloud(background_color="white", max_words=2000, mask=alice_mask,
    30.  
      stopwords=stopwords)
    31.  
      # generate word cloud
    32.  
      wc.generate(text)
    33.  
       
    34.  
      # store to file
    35.  
      wc.to_file(path.join(d, "../picture/alice.png"))
    36.  
       
    37.  
      # show
    38.  
      plt.imshow(wc, interpolation='bilinear')
    39.  
      plt.axis("off")
    40.  
      plt.figure()
    41.  
      plt.imshow(alice_mask, cmap="gray", interpolation='bilinear')
    42.  
      plt.axis("off")
    43.  
      plt.show()

    运行效果:

    5.根据词频生成词云

    1.  
      import time
    2.  
      import multidict as multidict
    3.  
      import numpy as np
    4.  
      import re
    5.  
      from PIL import Image
    6.  
      from os import path
    7.  
      from wordcloud import WordCloud
    8.  
      import matplotlib.pyplot as plt
    9.  
       
    10.  
      def getFrequencyDictForText(sentence):
    11.  
      fullTermsDict = multidict.MultiDict()
    12.  
      tmpDict = {}
    13.  
       
    14.  
      # making dict for counting frequencies
    15.  
      for text in sentence.split(" "):
    16.  
      if re.match("a|the|an|the|to|in|for|of|or|by|with|is|on|that|be",text):
    17.  
      continue
    18.  
      val = tmpDict.get(text,0)
    19.  
      tmpDict[text.lower()] = val+1
    20.  
      for key in tmpDict:
    21.  
      fullTermsDict.add(key,tmpDict[key])
    22.  
      return fullTermsDict
    23.  
       
    24.  
       
    25.  
       
    26.  
      def makeImage(text):
    27.  
      alice_mask = np.array(Image.open("../bgPic/alice_mask.png"))
    28.  
       
    29.  
       
    30.  
      wc = WordCloud(background_color="white", max_words=1000, mask=alice_mask)
    31.  
      # generate word cloud
    32.  
      wc.generate_from_frequencies(text)
    33.  
      wc.to_file("../picture/freq.png")
    34.  
      # show
    35.  
      plt.imshow(wc, interpolation="bilinear")
    36.  
      plt.axis("off")
    37.  
      plt.show()
    38.  
       
    39.  
      d = path.dirname(__file__)
    40.  
       
    41.  
      text = open(path.join(d, '../txt/alice.txt'),encoding='utf-8')
    42.  
      text = text.read()
    43.  
      makeImage(getFrequencyDictForText(text))

    运行效果:

    6.使用正则

    1.  
      # -*- encoding:utf-8 -*-.
    2.  
      """
    3.  
      Emoji Example
    4.  
      ===============
    5.  
      A simple example that shows how to include emoji. Note that this example does not seem to work on OS X, but does
    6.  
      work correctly in Ubuntu.
    7.  
       
    8.  
      There are 3 important steps to follow to include emoji:
    9.  
      1) Read the text input with io.open instead of the built in open. This ensures that it is loaded as UTF-8
    10.  
      2) Override the regular expression used by word cloud to parse the text into words. The default expression
    11.  
      will only match ascii words
    12.  
      3) Override the default font to something that supports emoji. The included Symbola font includes black and
    13.  
      white outlines for most emoji. There are currently issues with the PIL/Pillow library that seem to prevent
    14.  
      it from functioning correctly on OS X (https://github.com/python-pillow/Pillow/issues/1774), so try this
    15.  
      on ubuntu if you are having problems.
    16.  
      """
    17.  
      import io
    18.  
      import string
    19.  
      from os import path
    20.  
      from wordcloud import WordCloud
    21.  
       
    22.  
      d = path.dirname(__file__)
    23.  
       
    24.  
      # It is important to use io.open to correctly load the file as UTF-8
    25.  
      text = io.open(path.join(d, '../txt/happy-emoji.txt'),encoding='utf-8').read()
    26.  
       
    27.  
      # the regex used to detect words is a combination of normal words, ascii art, and emojis
    28.  
      # 2+ consecutive letters (also include apostrophes), e.x It's
    29.  
      normal_word = r"(?:w[w']+)"
    30.  
      # 2+ consecutive punctuations, e.x. :)
    31.  
      ascii_art = r"(?:[{punctuation}][{punctuation}]+)".format(punctuation=string.punctuation)
    32.  
      # a single character that is not alpha_numeric or other ascii printable
    33.  
      emoji = r"(?:[^s])(?<![w{ascii_printable}])".format(ascii_printable=string.printable)
    34.  
      regexp = r"{normal_word}|{ascii_art}|{emoji}".format(normal_word=normal_word, ascii_art=ascii_art,
    35.  
      emoji=emoji)
    36.  
       
    37.  
      # Generate a word cloud image
    38.  
      # The Symbola font includes most emoji
    39.  
      font_path = path.join(d, '../fonts', 'Symbola', 'Symbola.ttf')
    40.  
      wordcloud = WordCloud(font_path=font_path, regexp=regexp).generate(text)
    41.  
       
    42.  
      # Display the generated image:
    43.  
      # the matplotlib way:
    44.  
      import matplotlib.pyplot as plt
    45.  
      plt.imshow(wordcloud)
    46.  
      plt.axis("off")
    47.  
      plt.show()
    48.  
      wordcloud.to_file('../picture/emojj.png')

    运行效果:

    7.使用recolor方法定制 coloring functions

    1.  
      """
    2.  
      Using custom colors
    3.  
      ===================
    4.  
       
    5.  
      Using the recolor method and custom coloring functions.
    6.  
      """
    7.  
       
    8.  
      import numpy as np
    9.  
      from PIL import Image
    10.  
      from os import path
    11.  
      import matplotlib.pyplot as plt
    12.  
      import random
    13.  
       
    14.  
      from wordcloud import WordCloud, STOPWORDS
    15.  
       
    16.  
       
    17.  
      def grey_color_func(word, font_size, position, orientation, random_state=None,
    18.  
      **kwargs):
    19.  
      return "hsl(0, 0%%, %d%%)" % random.randint(60, 100)
    20.  
       
    21.  
      d = path.dirname(__file__)
    22.  
       
    23.  
      # read the mask image
    24.  
      # taken from
    25.  
      # http://www.stencilry.org/stencils/movies/star%20wars/storm-trooper.gif
    26.  
      mask = np.array(Image.open(path.join(d, "../bgPic/stormtrooper_mask.png")))
    27.  
       
    28.  
      # movie script of "a new hope"
    29.  
      # http://www.imsdb.com/scripts/Star-Wars-A-New-Hope.html
    30.  
      # May the lawyers deem this fair use.
    31.  
      text = open(path.join(d, '../txt/a_new_hope.txt')).read()
    32.  
       
    33.  
      # preprocessing the text a little bit
    34.  
      text = text.replace("HAN", "Han")
    35.  
      text = text.replace("LUKE'S", "Luke")
    36.  
       
    37.  
      # adding movie script specific stopwords
    38.  
      stopwords = set(STOPWORDS)
    39.  
      stopwords.add("int")
    40.  
      stopwords.add("ext")
    41.  
       
    42.  
      wc = WordCloud(max_words=1000, mask=mask, stopwords=stopwords, margin=10,
    43.  
      random_state=1).generate(text)
    44.  
      # store default colored image
    45.  
      default_colors = wc.to_array()
    46.  
      wc.to_file("default_hope.png")
    47.  
      plt.title("Custom colors")
    48.  
      plt.imshow(wc.recolor(color_func=grey_color_func, random_state=3),
    49.  
      interpolation="bilinear")
    50.  
      wc.to_file("a_new_hope.png")
    51.  
      plt.axis("off")
    52.  
      plt.figure()
    53.  
      plt.title("Default colors")
    54.  
      plt.imshow(default_colors, interpolation="bilinear")
    55.  
      plt.axis("off")
    56.  
      plt.show()

    运行效果:

    三、中文词云

        中文词云需要使用jieba分词先预处理,这里我写了一段小代码作为学习词云的练习:

    1.  
      # -*- encoding:utf-8 -*-
    2.  
      #Programmed by Mr.Cun
    3.  
      #Time:Feb.8.2018
    4.  
       
    5.  
      from os import path
    6.  
      from scipy.misc import imread
    7.  
      import matplotlib.pyplot as plt
    8.  
      import jieba
    9.  
      import multidict as multidict
    10.  
      import numpy as np
    11.  
      from PIL import Image
    12.  
      import re
    13.  
       
    14.  
      #如果加载用户词典,jieba词典变为第二词典
    15.  
      # jieba.load_userdict("txtuserdict.txt")
    16.  
      from wordcloud import WordCloud, ImageColorGenerator
    17.  
       
    18.  
      #获取词频
    19.  
      def getFrequencyDictForText(sentence):
    20.  
      fullTermsDict = multidict.MultiDict()
    21.  
      tmpDict = {}
    22.  
       
    23.  
      # making dict for counting frequencies
    24.  
      for text in sentence.split(" "):
    25.  
      val = tmpDict.get(text,0)
    26.  
      tmpDict[text.lower()] = val+1
    27.  
      for key in tmpDict:
    28.  
      fullTermsDict.add(key,tmpDict[key])
    29.  
      return fullTermsDict
    30.  
       
    31.  
      #添加用户自定义词
    32.  
      def add_word(list):
    33.  
      for items in list:
    34.  
      jieba.add_word(items)
    35.  
       
    36.  
       
    37.  
       
    38.  
      d = path.dirname(__file__)
    39.  
      stopwords = {}
    40.  
      back_coloring_path = "../bgPic/mask.jpg" #遮罩图片
    41.  
      text_path = '../txt/sanshengsanshi.txt'
    42.  
      font_path = '../fonts/STFANGSO.ttf' #
    43.  
      stopwords_path = '../stopwords/stopworsZh1893.txt' # 网上下载的中文停用词表
    44.  
      my_words_list = ['白浅','离镜','叠风','少辛','小素锦','二师兄长衫',
    45.  
      '离怨','天君','夜华','墨渊','照歌','白真','连宋',
    46.  
      '子阑','瑶光','火麒麟','十师兄','乐胥','白凤九','折颜',
    47.  
      '素锦','父神','桑籍','擎苍','令羽','司命','天枢','东华',
    48.  
      '玄女','阿离','伽昀','玉铛','成玉','央错','金猊兽'] #自定义的词
    49.  
      back_coloring = imread(path.join(d, back_coloring_path))#
    50.  
       
    51.  
      #定义词云属性
    52.  
      wc = WordCloud(font_path=font_path,
    53.  
      background_color="white",
    54.  
      max_words=2000,
    55.  
      mask=back_coloring,
    56.  
      max_font_size=100,
    57.  
      random_state=42,
    58.  
      width=1000, height=860, margin=2,
    59.  
      )
    60.  
       
    61.  
      def jiebaclearText(text):
    62.  
      mywordlist = []
    63.  
      seg_list = jieba.cut(text, cut_all=False)
    64.  
      liststr="/ ".join(seg_list)
    65.  
      f_stop_text = open(stopwords_path,'r',encoding='utf-8').read()
    66.  
      # f_stop = open(stopwords_path,'r',encoding='utf-8')
    67.  
      # # try:
    68.  
      # # f_stop_text = f_stop.read()
    69.  
      # # f_stop_text=f_stop_text.encode('utf-8')
    70.  
      # # finally:
    71.  
      # # f_stop.close( )
    72.  
      f_stop_seg_list=f_stop_text.split(' ')
    73.  
      for myword in liststr.split('/'):
    74.  
      if not(myword.strip() in f_stop_seg_list) and len(myword.strip())>1:
    75.  
      mywordlist.append(myword)
    76.  
      return ''.join(mywordlist)
    77.  
       
    78.  
       
    79.  
      add_word(my_words_list)
    80.  
      text = open(path.join(d, text_path)).read()
    81.  
      text = jiebaclearText(text)
    82.  
       
    83.  
      #生成文本词云
    84.  
      wc.generate(text)
    85.  
      plt.figure()
    86.  
      plt.imshow(wc)
    87.  
      plt.axis("off")
    88.  
      wc.to_file(path.join(d, '../picture/sansheng1.png'))
    89.  
       
    90.  
      #使词云复合遮罩
    91.  
      image_colors = ImageColorGenerator(back_coloring)
    92.  
      plt.imshow(wc.recolor(color_func=image_colors))
    93.  
      plt.axis("off")
    94.  
      plt.imshow(back_coloring, cmap="gray")
    95.  
      plt.axis("off")
    96.  
      wc.to_file(path.join(d,'../picture/sansheng2.png'))
    97.  
      #词频词云
    98.  
      wc.generate_from_frequencies(getFrequencyDictForText(text))
    99.  
      wc.to_file("../picture/sansheng3.png")
    100.  
      plt.imshow(wc, interpolation="bilinear")
    101.  
      plt.axis("off")
    102.  
      plt.show()

    运行效果如下:

    原始词云

    遮罩词云:

    词频词云:

  • 相关阅读:
    ExtJS 使用点滴 四 XTemplate使用方法
    ExtJS 使用点滴 三 TreeGrid 单击事件侦听例子
    VS2008 引用App_Code下的类文件问题解决方法
    C# 文件操作类大全(转摘)
    SqlParameter数组
    ExtJS 使用点滴 二 如何使用XTemplate基于同行的其他列的值,改变当前列的显示样式
    ScriptManager.RegisterStartupScript方法
    ExtJS 使用点滴 一(XTemlpate)
    Jquery 远程调用 webService报错,500错误
    C# 调用数据库函数 转摘
  • 原文地址:https://www.cnblogs.com/wenqiang-leo/p/14217703.html
Copyright © 2020-2023  润新知