Skip to content

Instantly share code, notes, and snippets.

@cjhgo
Last active May 27, 2017 13:56
Show Gist options
  • Save cjhgo/df052f8176895a643cbe1bfd1ae565fc to your computer and use it in GitHub Desktop.
Save cjhgo/df052f8176895a643cbe1bfd1ae565fc to your computer and use it in GitHub Desktop.
import time
import functools
def split_list_into_sized_chunks(l,n):
"""Yield successive n-sized chunks from l."""
for i in range(0, len(l), n):
yield l[i:i + n]
def clock(func):
@functools.wraps(func)
def clocked(*args, **kwargs):
t0 = time.time()
result = func(*args, **kwargs)
elapsed = time.time() - t0
name = func.__name__
arg_lst = []
if args:
arg_lst.append(', '.join(repr(arg) for arg in args))
if kwargs:
pairs = ['%s=%r' % (k, w) for k, w in sorted(kwargs.items())]
arg_lst.append(', '.join(pairs))
arg_str = ', '.join(arg_lst)
print('[%0.8fs] %s(%s) -> %r ' % (elapsed, name, arg_str, result))
return result
return clocked
#打印进度条
def printProgress (iteration, total, prefix = '', suffix = '', decimals = 2, barLength = 100):
"""
Call in a loop to create terminal progress bar
@params:
iteration - Required : current iteration (Int)
total - Required : total iterations (Int)
prefix - Optional : prefix string (Str)
suffix - Optional : suffix string (Str)
decimals - Optional : number of decimals in percent complete (Int)
barLength - Optional : character length of bar (Int)
"""
filledLength = int(round(barLength * iteration / float(total)))
percents = round(100.00 * (iteration / float(total)), decimals)
bar = '#' * filledLength + '-' * (barLength - filledLength)
Sys.stdout.write('%s [%s] %s%s %s\r' % (prefix, bar, percents, '%', suffix)),
Sys.stdout.flush()
if iteration == total:
print("\n")
#生成词云
def gen_wrod_cloud(file)
# error _imagingft C module is not installed
# sudo apt-get install libfreetype6-dev
# pip uninstall pillow
# pip install --no-cache-dir pillow
import jieba
from wordcloud import WordCloud
import matplotlib.pyplot as plt
text_from_file = open(file, 'rb').read()
wordlist = jieba.cut(text_from_file, cut_all=True) # 首先使用 jieba 中文分词工具进行分词
wordlist_space_split = ' '.join(wordlist)
my_wordcloud = WordCloud().generate(wordlist_space_split)
plt.imshow(my_wordcloud)
plt.axis('on')
plt.show()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment