Skip to content

Instantly share code, notes, and snippets.

@danaabs
Created April 8, 2016 19:11
Show Gist options
  • Save danaabs/908cb56ffe8c6c45de93e71944778a20 to your computer and use it in GitHub Desktop.
Save danaabs/908cb56ffe8c6c45de93e71944778a20 to your computer and use it in GitHub Desktop.
Sui Generis Bloque_Functions
from __future__ import division
from random import shuffle
import sys
import string # to find punctuation
import re
import random
#put punctuation in a set
punctuation_set = set(string.punctuation)
#open text via command line. strip text, remove punct and put all lines into 1 giant line.
def read_text():
my_giant_line = ''
for line in sys.stdin:
line = line.strip()
line = remove_punct(line, punctuation_set)
my_giant_line = my_giant_line + line
return my_giant_line
#function to remove punctuation
def remove_punct(line, punctuation_set):
for punctuation in punctuation_set:
line = line.replace(punctuation, '')
line = line.replace("'", "")
line = line.replace("'", "")
line = line.replace('\"','')
line = line.replace("\n", "")
line = line.replace("\r", "")
line = line.replace("\t", "")
line = line.replace('"','')
return line
#go through lsit of words and count the frequency, placing them in a dictionary
def word_dictionary(list_of_words):
my_dictionary = dict()
for word in list_of_words:
if word in my_dictionary:
my_dictionary[word] += 1
else:
my_dictionary[word] = 1
return my_dictionary
#go through the dictionary and put the unique words in a list
def unique_list(word_dictionary):
return [word.upper() for word, count in word_dictionary.iteritems() if count == 1]
#go through unique list of words, shuffle and randomly choose 1
def shuffle_sample(list_of_unique_words):
randomWord = random.sample(list_of_unique_words, 1)
return randomWord
#go through dictionary and put the frequency #s of all the words in a list
def all_word_num(word_dictionary):
return [count for word, count in word_dictionary.iteritems()]
#go through dictionary and find the word associated with the max value in this list
def common_Word(word_dictionary, number_list):
return [word for word, count in word_dictionary.iteritems() if count == max(number_list)]
#find the ratio of unique words to all words
def get_ratio(unique_word_list, total_word_list):
return float(len(unique_word_list)/len(total_word_list))
#compile elements of background: most common word, percentage of unique words represented as symbol and insert
## a randomly chosen unique word
def make_background(common_word_list, ratio, symbol, unique_word_list):
my_list = ()
unique_percent = 1000 * ratio
common_percent = 1000 * ratio
my_list = common_word_list * int(common_percent/2) + [symbol] * int(unique_percent) + common_word_list * int(common_percent/2)
my_list.insert(int(unique_percent/2 + common_percent/2), unique_word_list)
return my_list
#create giant line of stripped text
my_giant_line = read_text()
#split the line by spaces and put in a list
words = my_giant_line.split()
#assign words to a dictionary with word frequency counts
counts = word_dictionary(words)
#put all words with a frequency of 1 into a list
unique_words = unique_list(counts)
#shuffle the unique word list and randomly select 1 word
uniqueWord = shuffle_sample(unique_words)
#put all associated frequency numbers from the dict in a list
all_numbers = all_word_num(counts)
#put most common word in a list
most_word = common_Word(counts, all_numbers)
#create a ratio of unique to total words
unique_ratio = get_ratio(unique_words, all_numbers)
#use the unique ratio to create a list of all elements in the background
background_list = make_background(most_word, unique_ratio, "+", uniqueWord)
#turn all elements to a string and join without spaces
s = ""
print (s.join(map(str, background_list)).center(50))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment