0

我正在尝试使用pytagcloud在 python 中创建一个词云。用我现在的云,我可以生成一个云,但是所有的词都是一样的大小。如何更改代码以使我的单词大小与其频率相关?

我的文本文件中已经包含了具有各自频率计数的单词,格式类似于“George,44”换行“Harold,77”,换行,“Andrew,22”,换行等。但是,当它显示word,它还显示频率。

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')
4

1 回答 1

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')

在此处输入图像描述

值得一看的变量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)])
于 2015-03-25T19:23:26.750 回答