''' Created on Nov 20, 2017 @author: Brett Paufler Copyright Brett Paufler ''' import matplotlib.pyplot as plt import numpy as np amendment_numbers = range(1, 28) amendment_dates = [ 1791, 1791, 1791, 1791, 1791, 1791, 1791, 1791, 1791, 1791, 1795, 1804, 1865, 1868, 1870, 1913, 1913, 1919, 1920, 1933, 1933, 1951, 1961, 1964, 1967, 1971, 1992] #Time Lag Between Amendment Adoption delta = [15] dates = list(amendment_dates) for n in range(1, 27, 1): delta.append(dates[n] - dates[n-1]) #print delta for n in range (1, 27, 1): if delta[n] == 0: delta[n] = delta[n-1] #print delta def graph_amendment_dates(): '''One off, need to manually save.''' plt.figure(figsize=(10,5)) plt.plot(amendment_dates, amendment_numbers) plt.scatter(amendment_dates, amendment_numbers) plt.title('Amendment Dates') plt.xticks(range(1775, 2025, 25)) plt.yticks(range(1, 28, 1)) plt.show() #graph_amendment_dates() def graph_date_delta(): '''One off, showing time lag between amendment adoption, if more than one on same date, both get same delta.''' plt.figure(figsize=(10,5)) plt.bar(amendment_numbers, delta) #plt.plot(delta, [15]*27, 'red') #plt.scatter(amendment_dates, amendment_numbers) plt.title('Years Since Last Amendment') #plt.xticks(range(1775, 2025, 25)) plt.xticks(range(1, 28, 1)) plt.show() #graph_date_delta() def graph_amendments_as_works_in_progress(): '''One off, showing how long amendments have been the last working version, so to speak.''' plt.figure(figsize=(10,5)) param = [(year - d, d) for d, year in zip(delta, amendment_dates)] print param for a, p in enumerate(param): print a+1, p plt.broken_barh([p], (a+0.75, 0.25)) plt.title('Amendments as Works in Progress') plt.yticks(range(1, 28, 1)) plt.show() #graph_amendments_as_works_in_progress() class Amendment(object): def __init__(self, num, amendment): self.num = num self.text = amendment self.char_len = len(self.text) self.word_len = len(self.text.split()) self.ave_word_len = ( float(self.char_len) / self.word_len) #def __str__(self): # return def __repr__(self): text = 'Amendment: %d\n' % self.num text += 'Words: %d\n' % self.word_len text += 'Chars: %d\n' % self.char_len text += 'Ave Word Length: %.02f\n' % ( self.ave_word_len) text += self.text text += '\n' return text def get_amendments(): with open('constitution_amendments.txt', 'r') as f: text = f.read() amendments = text.split('\n') amendment_list = [Amendment(num + 1, text) for num, text in enumerate(amendments)] for a in amendment_list: print a return amendment_list #amendments = get_amendments() def polyfit(x, y): z = np.polyfit(x, y, 1) p = np.poly1d(z) return p(x) def graph_amendment_word_num(): '''One off, need to manually save.''' amendments = get_amendments() num = [a.num for a in amendments] words = [a.word_len for a in amendments] word_lens = [a.ave_word_len for a in amendments] plt.figure(figsize=(10,5)) plt.plot(num, words) plt.scatter(num, words) plt.title('Number of Words per Amendment') plt.plot(num, polyfit(num, words)) plt.xticks(range(1, 28, 1)) plt.show() #graph_amendment_word_num() def graph_amendment_word_length(): '''One off, need to manually save.''' amendments = get_amendments() num = [a.num for a in amendments] words = [a.word_len for a in amendments] word_lens = [a.ave_word_len for a in amendments] plt.figure(figsize=(10,5)) plt.plot(num, word_lens) plt.scatter(num, word_lens) plt.title('Length of Words per Amendment') plt.plot(num, polyfit(num, word_lens)) plt.xticks(range(1, 28, 1)) plt.show() #graph_amendment_word_length() def graph_amendment_length_max_words(n): '''One off, need to manually save.''' def max_len_data(n): '''Returns length of the n longest words.''' nums = [] word_lens = [] for a in get_amendments(): words = a.text.split() words = [len(w) for w in words] words = sorted(words, reverse=True) nums += [a.num] * n word_lens += words[0:n] print words return (nums, word_lens) plt.figure(figsize=(10,5)) num, word_lens = max_len_data(n) print num print word_lens plt.plot(num, word_lens,) plt.scatter(num, word_lens) plt.title('Size of %d Biggest Words' % n) plt.plot(num, polyfit(num, word_lens)) plt.yticks(range(1, 17, 1)) plt.xticks(range(1, 28, 1)) plt.show() #graph_amendment_length_max_words(5) #graph_amendment_length_max_words(10) #graph_amendment_length_max_words(15)