Skip to content

Instantly share code, notes, and snippets.

@epwalsh
Created June 5, 2016 21:41
Show Gist options
  • Save epwalsh/20d27e31dfe9b88f33d25cd049131d8f to your computer and use it in GitHub Desktop.
Save epwalsh/20d27e31dfe9b88f33d25cd049131d8f to your computer and use it in GitHub Desktop.
Python + R libraries to WordCloud
find ~/ISU-DMC/dmc2016 -name '*.h' -o -name '*.R' > RFILES
find ~/ISU-DMC/dmc2016 -name '*.h' -o -name '*.py' > PYFILES
import re
import matplotlib.pyplot as plt
from PIL import Image
from wordcloud import WordCloud
with open('./PYFILES', 'r') as f:
pyfiles = f.readlines()
with open('./RFILES', 'r') as f:
rfiles = f.readlines()
RLIB = re.compile(r"^library\(\"?\'?([a-zA-Z0-9]*)\"?\'?\)")
PYLIB = re.compile(r"^(from|import)\s([a-zA-Z0-9]*).*")
def check_for_package(line, packages, ft='py'):
if ft == 'py':
res = PYLIB.search(line)
if res:
packages.append(res.group(2))
if ft == 'r':
res = RLIB.search(line)
if res:
packages.append(res.group(1))
def get_packages(files, ft='py'):
packages = []
for item in files:
with open(item[:-1], 'r') as f:
lines = f.readlines()
for line in lines:
check_for_package(line, packages, ft)
return packages
rpack = get_packages(rfiles, ft='r'q
pypack = get_packages(pyfiles, ft='py')
text = rpack + pypack
text = ' '.join(text)
wordcloud = WordCloud(background_color='white',
max_font_size=200,
relative_scaling=.5,
width=1000,
height=700).generate(text)
plt.figure()
plt.imshow(wordcloud)
plt.axis("off")
plt.show()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment