1
|
|
|
#!/usr/bin/env python |
2
|
|
|
# -*- coding: utf-8 -*- |
3
|
|
|
|
4
|
|
|
""" |
5
|
|
|
flask_jsondash.data_utils.wordcloud |
6
|
|
|
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ |
7
|
|
|
|
8
|
|
|
Utilities for working with wordcloud formatted data. |
9
|
|
|
|
10
|
|
|
:copyright: (c) 2016 by Chris Tabor. |
11
|
|
|
:license: MIT, see LICENSE for more details. |
12
|
|
|
""" |
13
|
|
|
|
14
|
|
|
from collections import Counter |
15
|
|
|
|
16
|
|
|
# Py2/3 compat. |
17
|
|
|
try: |
18
|
|
|
_unicode = unicode |
19
|
|
|
except NameError: |
20
|
|
|
_unicode = str |
21
|
|
|
|
22
|
|
|
|
23
|
|
|
# NLTK stopwords |
24
|
|
|
stopwords = [ |
25
|
|
|
'i', 'me', 'my', 'myself', 'we', 'our', 'ours', 'ourselves', 'you', 'your', |
26
|
|
|
'yours', 'yourself', 'yourselves', 'he', 'him', 'his', 'himself', 'she', |
27
|
|
|
'her', 'hers', 'herself', 'it', 'its', 'itself', 'they', 'them', 'their', |
28
|
|
|
'theirs', 'themselves', 'what', 'which', 'who', 'whom', 'this', 'that', |
29
|
|
|
'these', 'those', 'am', 'is', 'are', 'was', 'were', 'be', 'been', 'being', |
30
|
|
|
'have', 'has', 'had', 'having', 'do', 'does', 'did', 'doing', 'a', 'an', |
31
|
|
|
'the', 'and', 'but', 'if', 'or', 'because', 'as', 'until', 'while', 'of', |
32
|
|
|
'at', 'by', 'for', 'with', 'about', 'against', 'between', 'into', |
33
|
|
|
'through', 'during', 'before', 'after', 'above', 'below', 'to', 'from', |
34
|
|
|
'up', 'down', 'in', 'out', 'on', 'off', 'over', 'under', 'again', |
35
|
|
|
'further', 'then', 'once', 'here', 'there', 'when', 'where', 'why', |
36
|
|
|
'how', 'all', 'any', 'both', 'each', 'few', 'more', 'most', 'other', |
37
|
|
|
'some', 'such', 'no', 'nor', 'not', 'only', 'own', 'same', 'so', 'than', |
38
|
|
|
'too', 'very', 's', 't', 'can', 'will', 'just', 'don', 'should', 'now', |
39
|
|
|
] |
40
|
|
|
|
41
|
|
|
|
42
|
|
|
def get_word_freq_distribution(words): |
43
|
|
|
"""Get the counted word frequency distribution of all words. |
44
|
|
|
|
45
|
|
|
Arg: |
46
|
|
|
words (list): A list of strings indicating words. |
47
|
|
|
|
48
|
|
|
Returns: |
49
|
|
|
collections.Counter: The Counter object with word frequencies. |
50
|
|
|
""" |
51
|
|
|
return Counter([w for w in words if w not in stopwords]) |
52
|
|
|
|
53
|
|
|
|
54
|
|
|
def format_4_wordcloud(words, size_multiplier=2): |
55
|
|
|
"""Format words in a way suitable for wordcloud plugin. |
56
|
|
|
|
57
|
|
|
Args: |
58
|
|
|
words (list): A list of strings indicating words. |
59
|
|
|
size_multiplier (int, optional): The size multiplier to scale |
60
|
|
|
word sizing. Can improve visual display of word cloud. |
61
|
|
|
""" |
62
|
|
|
return [ |
63
|
|
|
{'text': word, 'size': size * size_multiplier} |
64
|
|
|
for (word, size) in words if word |
65
|
|
|
] |
66
|
|
|
|