From 1daab919eac39f2f303277e8698a33c42e8b67bc Mon Sep 17 00:00:00 2001 From: Samuel Sloniker Date: Sun, 8 Jan 2023 08:45:22 -0800 Subject: [PATCH] Add analyses --- analyses/constitutional_amendments.py | 70 ++++++++++++++++++++++ analyses/states.py | 85 +++++++++++++++++++++++++++ 2 files changed, 155 insertions(+) create mode 100644 analyses/constitutional_amendments.py create mode 100644 analyses/states.py diff --git a/analyses/constitutional_amendments.py b/analyses/constitutional_amendments.py new file mode 100644 index 0000000..7bbeb31 --- /dev/null +++ b/analyses/constitutional_amendments.py @@ -0,0 +1,70 @@ +import gptc + +amendments = [ + ("1st", "First"), + ("2nd", "Second"), + ("3rd", "Third"), + ("4th", "Fourth"), + ("5th", "Fifth"), + ("6th", "Sixth"), + ("7th", "Seventh"), + ("8th", "Eighth"), + ("9th", "Ninth"), + ("10th", "Tenth"), + ("11th", "Eleventh"), + ("12th", "Twelfth"), + ("13th", "Thirteenth"), + ("14th", "Fourteenth"), + ("15th", "Fifteenth"), + ("16th", "Sixteenth"), + ("17th", "Seventeenth"), + ("18th", "Eighteenth"), + ("19th", "Nineteenth"), + ("20th", "Twentieth"), + ("21st", "Twenty-first"), + ("22nd", "Twenty-second"), + ("23rd", "Twenty-third"), + ("24th", "Twenty-fourth"), + ("25th", "Twenty-fifth"), + ("26th", "Twenty-sixth"), + ("27th", "Twenty-seventh"), +] + +with open("model.gptc", "rb") as f: + model = gptc.deserialize(f) + +data = {} + +for number, name in amendments: + number_data = model.get(number + " Amendment") + name_data = model.get(name + " Amendment") + + if number_data and not name_data: + data[name] = number_data + elif name_data and not number_data: + data[name] = name_data + elif number_data and name_data: + data[name] = { + key: (number_data[key] + name_data[key]) / 2 + for key in number_data.keys() + } + +classified_amendments = sorted(data.items(), key=lambda x: x[1]["left"]) + +print("# Constitutional Amendment Analysis") +print() +print("""This is an analysis of which amendments to the U.S. Constitution are mentioned +more in right- or left-leaning American news sources. Data do not necessarily +correlate with support or opposition for the amendment among right- or +left-leaning Americans.""") +print() +print("| Amendment | Left | Right |") +print("+----------------+-------+-------+") +for amendment, data in classified_amendments: + percent_right = f"{data['right']*100:>4.1f}%" + percent_left = f"{data['left']*100:>4.1f}%" + + amendment_padding = " "*(14 - len(amendment)) + print(f"| {amendment}{amendment_padding} | {percent_left} | {percent_right} |") +print("+----------------+-------+-------+") +print("| Amendment | Left | Right |") diff --git a/analyses/states.py b/analyses/states.py new file mode 100644 index 0000000..9485c4e --- /dev/null +++ b/analyses/states.py @@ -0,0 +1,85 @@ +import gptc + +states = [ + "Alabama", + "Alaska", + "Arizona", + "Arkansas", + "California", + "Colorado", + "Connecticut", + "Delaware", + "Florida", + "Georgia", + "Hawaii", + "Idaho", + "Illinois", + "Indiana", + "Iowa", + "Kansas", + "Kentucky", + "Louisiana", + "Maine", + "Maryland", + "Massachusetts", + "Michigan", + "Minnesota", + "Mississippi", + "Missouri", + "Montana", + "Nebraska", + "Nevada", + "New Hampshire", + "New Jersey", + "New Mexico", + "New York", + "North Carolina", + "North Dakota", + "Ohio", + "Oklahoma", + "Oregon", + "Pennsylvania", + "Rhode Island", + "South Carolina", + "South Dakota", + "Tennessee", + "Texas", + "Utah", + "Vermont", + "Virginia", + "Washington", + "West Virginia", + "Wisconsin", + "Wyoming", +] + +with open("model.gptc", "rb") as f: + model = gptc.deserialize(f) + +classified_states = [] + +for state in states: + classified_states.append((state, model.get(state),)) + +classified_states.sort(key=lambda x: x[1]["left"]) + +longest = max([len(state) for state in states]) + +print("# State Analysis") +print() +print("""This is an analysis of which states are mentioned more in right- or left- +leaning American news sources. Results do not necessarily correlate with the +political views of residents of the states; for example, the predominantly +liberal state of Oregon is mentioned more in right-leaning sources than in +left-leaning ones.""") +print() +print("| State | Left | Right |") +print("+----------------+-------+-------+") +for state, data in classified_states: + percent_right = f"{round(data['right']*1000)/10}%" + percent_left = f"{round(data['left']*1000)/10}%" + + state_padding = " "*(longest - len(state)) + print(f"| {state}{state_padding} | {percent_left} | {percent_right} |") +print("+----------------+-------+-------+") +print("| State | Left | Right |")