Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add encoding to two examples #577

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion examples/emoji.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion examples/parrot.py
Original file line number Diff line number Diff line change
Expand Up @@ -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")))
Expand Down
17 changes: 11 additions & 6 deletions examples/wordcloud_cn.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

you can do an if __name__ == "__main__" that should fix that, I think?

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
Expand All @@ -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
Expand All @@ -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()
Expand Down