wordcloud - 图1
    © Karobben

    由於語法渲染問題而影響閱讀體驗, 請移步博客閱讀~
    本文GitPage地址

    1. from os import path
    2. from PIL import Image
    3. import numpy as np
    4. import matplotlib.pyplot as plt
    5. from wordcloud import WordCloud, STOPWORDS
    6. ## Read the whole text.
    7. text = open('alice.txt').read()
    8. ## read the mask image
    9. ## taken from
    10. ## http://www.stencilry.org/stencils/movies/alice%20in%20wonderland/255fk.jpg
    11. alice_mask = np.array(Image.open("alice_mask.png"))
    12. stopwords = set(STOPWORDS)
    13. stopwords.add("said")
    14. wc = WordCloud(background_color="white",
    15. max_words=200, mask=alice_mask,
    16. max_font_size=200, # 根据你的图片大小定义
    17. stopwords=stopwords)
    18. ## generate word cloud
    19. wc.generate(text)
    20. ## store to file
    21. wc.to_file("alice.png")
    22. ## show
    23. plt.imshow(wc, interpolation='bilinear')
    24. plt.axis("off")
    25. plt.figure()
    26. plt.imshow(alice_mask, cmap=plt.cm.gray, interpolation='bilinear')
    27. plt.axis("off")
    28. plt.show()
    29. ## remove punctuation
    30. Special = "”"
    31. TT = text.translate(str.maketrans(' ', ' ', string.punctuation))
    32. TT = TT.translate(str.maketrans('', '', string.whitespace[1:]))
    33. TT = TT.translate(str.maketrans('', '', string.digits))
    34. TT = TT.lower()
    35. TT = TT.split(" ")
    36. TT = list(set(TT))
    37. for i in string.ascii_lowercase:
    38. try:
    39. TT.remove(i)
    40. except:
    41. print(i)
    42. f = open("list",'a')
    43. for i in TT:
    44. f.write(i+'\n')
    45. f.close()

    https://amueller.github.io/word_cloud/auto_examples/colored.html#colored-py


    Enjoy~

    本文由Python腳本GitHub/語雀自動更新

    由於語法渲染問題而影響閱讀體驗, 請移步博客閱讀~
    本文GitPage地址

    GitHub: Karobben
    Blog:Karobben
    BiliBili:史上最不正經的生物狗