Created
June 26, 2020 20:17
-
-
Save villares/d75fc9ace88efe3740e7d88d54a123ec to your computer and use it in GitHub Desktop.
Uma primeira tentativa de nuvem de palavras com Turtle Graphics no Python :)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| # Sugestão: uso o IDE thonny.org | |
| # precisa de um arquivo texto.txt 'ao lado' deste script | |
| from turtle import * | |
| from collections import Counter | |
| from random import randrange | |
| def fala(texto, tamanho=18, estilo="normal"): | |
| write(texto, align='center', font=("Open Sans", | |
| tamanho, estilo)) | |
| with open('texto.txt', 'r') as arquivo: | |
| linhas_brutas = arquivo.readlines() | |
| palavras = [] | |
| for linha in linhas_brutas: | |
| linha = linha.strip() # remove espaços finais, iniciais \n | |
| linha = linha.replace('.', '') | |
| linha = linha.replace(',', '') | |
| linha = linha.replace('!', '') | |
| linha = linha.replace('?', '') | |
| linha = linha.replace(';', '') | |
| linha = linha.replace(':', '') | |
| palavras_linha = linha.split() # separa no espaços | |
| for palavra in palavras_linha: | |
| if len(palavra) > 4: | |
| if palavra.endswith('s'): | |
| palavra = palavra[:-1] | |
| palavras.append(palavra) | |
| contador = Counter(palavras) | |
| speed(0) | |
| for p in contador: | |
| up() | |
| x = randrange(-240, 240) | |
| y = randrange(-240, 240) | |
| setpos(x, y) | |
| frequencia = contador[p] | |
| fala(p, tamanho=8+frequencia*2) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment