Skip to content

Commit

Permalink
Merge pull request #120 from mkcor/process_text-return-dict
Browse files Browse the repository at this point in the history
Let process_text() return a dict.
  • Loading branch information
amueller committed Apr 11, 2016
2 parents 740b3e0 + dd036d8 commit 1860f7d
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 4 deletions.
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,8 @@ word_cloud
==========

A little word cloud generator in Python. Read more about it on the [blog
post][blog-post] or the [website][website]
post][blog-post] or the [website][website].
The code is Python 2, but Python 3 compatible.

## Installation

Expand Down
20 changes: 20 additions & 0 deletions test/test_wordcloud.py
Original file line number Diff line number Diff line change
Expand Up @@ -169,6 +169,26 @@ def test_single_color_func_grey():
assert_equal(red_function(random_state=random), 'rgb(56, 56, 56)')


def test_process_text():
# test that process function returns a dict
wc = WordCloud(max_words=50)
result = wc.process_text(THIS)

# check for proper return type
assert_true(isinstance(result, dict))


def test_generate_from_frequencies():
# test that generate_from_frequencies() takes input argument of class
# 'dict_items'
wc = WordCloud(max_words=50)
words = wc.process_text(THIS)
items = words.items()
result = wc.generate_from_frequencies(items)

assert_true(isinstance(result, WordCloud))


def check_parameters():
# check that parameters are actually used
pass
13 changes: 10 additions & 3 deletions wordcloud/wordcloud.py
Original file line number Diff line number Diff line change
Expand Up @@ -376,9 +376,12 @@ def process_text(self, text):
Returns
-------
words : list of tuples (string, float)
words : dict (string, int)
Word tokens with associated frequency.
..versionchanged:: 1.2.2
Changed return type from list of tuples to dict.
Notes
-----
There are better ways to do word tokenization, but I don't want to
Expand Down Expand Up @@ -427,19 +430,23 @@ def process_text(self, text):
first = max(d2.items(), key=item1)[0]
d3[first] = sum(d2.values())

return d3.items()
return d3

def generate_from_text(self, text):
"""Generate wordcloud from text.
Calls process_text and generate_from_frequencies.
..versionchanged:: 1.2.2
Argument of generate_from_frequencies() is not return of
process_text() any more.
Returns
-------
self
"""
words = self.process_text(text)
self.generate_from_frequencies(words)
self.generate_from_frequencies(words.items())
return self

def generate(self, text):
Expand Down

0 comments on commit 1860f7d

Please sign in to comment.