Commit 6e46dc54 by Alexander Kryklia Committed by Vasyl Nakvasiuk

correct calculation of percents

parent 23802f87
...@@ -80,12 +80,21 @@ class WordCloudModule(WordCloudFields, XModule): ...@@ -80,12 +80,21 @@ class WordCloudModule(WordCloudFields, XModule):
"""Convert raw word to suitable word.""" """Convert raw word to suitable word."""
return word.strip().lower() return word.strip().lower()
def prepare_words(self, words, total_count):
def prepare_words(self, top_words, total_count):
"""Convert words dictionary for client API.""" """Convert words dictionary for client API."""
return [ list_to_return = []
{'text': word, 'size': count, 'percent': round(100 * (count / float(total_count)))} for percents = 0
word, count in words.iteritems() current_num_top_words = len(top_words)
] for num, word_tuple in enumerate(top_words.iteritems()):
if num == current_num_top_words - 1:
percent = 100 - percents
else:
percent = round(100.0 * word_tuple[1] / total_count)
percents += percent
list_to_return.append({'text': word_tuple[0] , 'size': word_tuple[1], 'percent': percent})
return list_to_return
def top_dict(self, dict_obj, amount): def top_dict(self, dict_obj, amount):
"""Return new dict: top of dict using dict value.""" """Return new dict: top of dict using dict value."""
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment