用Python创建Word云---使单词大小不同?

时间:2015-03-25 19:07:23

标签: python pytagcloud

我正在尝试使用pytagcloud在python中创建一个词云。使用我当前的云,我可以生成一个云,但所有单词的大小都相同。 我怎样才能改变代码,以便我的话语能够改变。尺寸与其频率有关吗?

我的文本文件中已经包含各自频率计数的单词,格式类似于" George,44" newline" Harold,77",newline," Andrew,22",换行等。但是,当它显示该单词时,它还会显示该单词的频率。

with open ("MyText.txt", "r") as file:
   Data =file.read().replace('\n', '')

tags = make_tags(get_tag_counts(Data), maxsize=150)

create_tag_image(tags, 'Sample.png', size=(1200, 1200),background=(0, 0, 0, 255),  fontname='Lobstero', rectangular=True)

import webbrowser
webbrowser.open('Sample.png')

1 个答案:

答案 0 :(得分:2)

您需要将结果转换为元组。使用您的问题作为输入文本,我们得到预期的结果:

from pytagcloud import create_tag_image, make_tags
from pytagcloud.lang.counter import get_tag_counts

TEXT = '''I am trying to create a word cloud in python. With my current cloud, I can generate a cloud, but the words all are the same size. How can I alter the code so that my words' sizes appear in relation to their frequency?'''

counts = get_tag_counts(TEXT)
tags = make_tags(counts, maxsize=120)
create_tag_image(tags, 'cloud_large.png', size=(900, 600), fontname='Lobster')

enter image description here

值得查看变量counts

[('cloud', 3), 
('words', 2), 
('code', 1), 
('word', 1), 
('appear', 1), ...

这只是一个元组列表。由于输入文本文件包含元组列表,因此您只需将该信息传递到make_tags

编辑:您可以阅读此类文件

counts = []
with open("tag_file.txt") as FIN:
   for line in FIN:
       # Assume lines look like: word, number
       word,n = line.strip().split()
       word = word.replace(',','')
       counts.append([word,int(n)])