diff --git a/examples/emoji.py b/examples/emoji.py index ab949cabd..66e7d9838 100755 --- a/examples/emoji.py +++ b/examples/emoji.py @@ -24,7 +24,7 @@ d = path.dirname(__file__) if "__file__" in locals() else os.getcwd() # It is important to use io.open to correctly load the file as UTF-8 -text = io.open(path.join(d, 'happy-emoji.txt')).read() +text = io.open(path.join(d, 'happy-emoji.txt'), encoding='utf-8').read() # the regex used to detect words is a combination of normal words, ascii art, and emojis # 2+ consecutive letters (also include apostrophes), e.x It's diff --git a/examples/parrot.py b/examples/parrot.py index 5bf317830..fc25b2629 100644 --- a/examples/parrot.py +++ b/examples/parrot.py @@ -19,7 +19,7 @@ d = os.path.dirname(__file__) if "__file__" in locals() else os.getcwd() # load wikipedia text on rainbow -text = open(os.path.join(d, 'wiki_rainbow.txt')).read() +text = open(os.path.join(d, 'wiki_rainbow.txt'), encoding='utf-8').read() # load image. This has been modified in gimp to be brighter and have more saturation. parrot_color = np.array(Image.open(os.path.join(d, "parrot-by-jose-mari-gimenez2.jpg"))) diff --git a/examples/wordcloud_cn.py b/examples/wordcloud_cn.py index c898727ba..d7388cf65 100644 --- a/examples/wordcloud_cn.py +++ b/examples/wordcloud_cn.py @@ -12,9 +12,14 @@ at the same time using wordcloud with jieba very convenient """ -import jieba -jieba.enable_parallel(4) -# Setting up parallel processes :4 ,but unable to run on Windows +import sys +try: + import jieba +except ImportError: + sys.exit('For this example you need to pip install jieba') +if not sys.platform == 'win32': + jieba.enable_parallel(4) + # Setting up parallel processes :4 ,but unable to run on Windows from os import path from imageio import imread import matplotlib.pyplot as plt @@ -24,7 +29,7 @@ from wordcloud import WordCloud, ImageColorGenerator # get data directory (using getcwd() is needed to support running example in generated IPython notebook) -d = path.dirname(__file__) if "__file__" in locals() else os.getcwd() +d = path.dirname(path.abspath(__file__)) if "__file__" in locals() else path.abspath(os.getcwd()) stopwords_path = d + '/wc_cn/stopwords_cn_en.txt' # Chinese fonts must be set @@ -34,10 +39,10 @@ imgname1 = d + '/wc_cn/LuXun.jpg' imgname2 = d + '/wc_cn/LuXun_colored.jpg' # read the mask / color image taken from -back_coloring = imread(path.join(d, d + '/wc_cn/LuXun_color.jpg')) +back_coloring = imread(imgname2) # Read the whole text. -text = open(path.join(d, d + '/wc_cn/CalltoArms.txt')).read() +text = open(path.join(d, d + '/wc_cn/CalltoArms.txt'), encoding='utf-8').read() # if you want use wordCloud,you need it # add userdict by add_word()