Home > other >  Be urgent! New Python problem solving.
Be urgent! New Python problem solving.

Time:11-30

 import re 
Import the collections
The import numpy as np
The import jieba
The from PIL import Image
The import wordcloud
The import matplotlib. Pylab as PLT
# to read text
With the open (" the romance of The Three Kingdoms. TXT ", "r") as fp:
Words=fp. Read ()
# text preprocessing
Pattern=re.com from running (u '| | \ t \ n \ | - | : |. (\ | | \ \)? | | "" |! | the | | ') # define the regular expression matching mode
Words=re. Sub (pattern, ' ', words)
# text participle
Swords=jieba. The cut (words, cut_all=False)
# block words
Okwords=[]
# second method nywords=[u ' 'u' is' u 'and' u 'is' u' in ', u 'a' u 'and' u 'I', u 'and' u 'and' u 'yue', u, 'the' u 'in the' u 'and u' no ', u 'I', u 'to' u 'to' u 'and u' is', u 'people' u 'and' u 'see' u 'to' u 'will', u 'a', u 'yi', u, 'then' u 'and', u 'and u', u ',]
For the word in swords:
If len (word)==1:
The continue
Okwords. Append (word)
# the second method for word in swords:
# if word not in nywords:
# okwords. Append (word)

# concept-semantic
Word_counts=collections. Counter (okwords)
Print (word_counts)
Word_counts_top10=word_counts. Most_common (10)
Print (word_counts_top10)
# word show
Mask=np. Array (Image. Open (' aixing. PNG '))
Love=wordcloud. Wordcloud (
Font_path='C:/Windows/Fonts/simhei. The vera.ttf',
Background_color="white",
Mask=mask,
Max_words=20,
Max_font_size=100
)
Love. Generate_from_frequencies (word_counts)
Image_colors=wordcloud. ImageColorGenerator (mask)
Love. Recolor (color_func=image_colors)
PLT. Imshow (love)
PLT. Axis (' off ')
PLT. The show ()




run after this problem, a great god help solve, thank you!!!!
Have created a aixing. PNG this file to run again, but still the same,

CodePudding user response:

The aixing. PNG into an absolute path, such as C:/aixing. PNG
  • Related