美文网首页
Colab WordCount

Colab WordCount

作者: iOSDevLog | 来源:发表于2018-09-12 01:42 被阅读29次
    # -*- coding: utf-8 -*-
    """tf_GUP.ipynb
    
    Automatically generated by Colaboratory.
    
    Original file is located at
        https://colab.research.google.com/drive/1crfzBEkEzf5Y8oGyDepR1PGem5CITnk2
    """
    
    import tensorflow as tf
    device_name = tf.test.gpu_device_name()
    if device_name != '/device:GPU:0':
      raise SystemError('GPU device not found')
    print('Found GPU at: {}'.format(device_name))
    
    !pip install wordcloud matplotlib jieba
    
    # 授权绑定代码
    !apt-get install -y -qq software-properties-common python-software-properties module-init-tools
    !add-apt-repository -y ppa:alessandro-strada/ppa 2>&1 > /dev/null
    !apt-get update -qq 2>&1 > /dev/null
    !apt-get -y install -qq google-drive-ocamlfuse fuse
    from google.colab import auth
    auth.authenticate_user()
    from oauth2client.client import GoogleCredentials
    creds = GoogleCredentials.get_application_default()
    import getpass
    !google-drive-ocamlfuse -headless -id={creds.client_id} -secret={creds.client_secret} < /dev/null 2>&1 | grep URL
    vcode = getpass.getpass()
    !echo {vcode} | google-drive-ocamlfuse -headless -id={creds.client_id} -secret={creds.client_secret}
    
    # 指定Google Drive云端硬盘的根目录,名为drive
    !mkdir -p drive
    !google-drive-ocamlfuse drive
    
    # 指定当前的工作文件夹
    import os
    
    # 此处为google drive中的文件路径,drive为之前指定的工作根目录,要加上
    os.chdir("drive/Colab Notebooks")
    
    !ls
    
    # -*- coding: utf-8 -*-
    
    # %matplotlib inline
    from wordcloud import WordCloud
    import matplotlib.pyplot as plt
    import jieba
    
    # 打开文本
    text = open('xyj.txt').read()
    
    # 中文分词
    text = ' '.join(jieba.cut(text))
    print(text[:100])
    
    # 生成对象
    wc = WordCloud(font_path='Hiragino.ttf', width=800, height=600, mode='RGBA', background_color=None).generate(text)
    
    # 显示词云
    plt.imshow(wc, interpolation='bilinear')
    plt.axis('off')
    plt.show()
    
    # 保存到文件
    wc.to_file('wordcloud.png')
    

    相关文章

      网友评论

          本文标题:Colab WordCount

          本文链接:https://www.haomeiwen.com/subject/ogzpgftx.html