Commit 9f7da49a authored by John Angileri's avatar John Angileri

script complete

parents
File added
'''
MATH 216 Final Project - John Angileri
Main file that will access the Question and Respondent classes
and perform all the CSV analysis
'''
import csv
from Question import Question
from Respondent import Respondent
def main(dataSet, questionsFile):
questionsList = createQuestionsList(questionsFile)
ideologyAnalysis(dataSet, questionsList)
'''
Takes a csv file that contains information about each of the pertinent questions
i.e., question index, the left leaning response, the right leaning response,
and how much value to add to the ideology score for each question.
Creates a list of Question objects (Question.py) with that information.
Cite: https://pythonprogramming.net/reading-csv-files-python-3/
'''
def createQuestionsList(questionsFile):
questionsList = []
csvfile = open(questionsFile)
readCSV = csv.reader(csvfile, delimiter=',')
#takes in the field names
fields = next(readCSV)
print("Question parameters:")
for row in readCSV:
print(row)
questionIndex = int(row[0])
leftLeaningResponseList = [row[1],row[2]]
rightLeaningResponseList = [row[3],row[4]]
valueAdded = float(row[5])
questionsList.append(Question(questionIndex, leftLeaningResponseList, rightLeaningResponseList, valueAdded))
csvfile.close()
return questionsList
'''
Takes in our csv data set and the questions list created by createQuestionsList function.
Appends ideology score to each individual row and writes a new csv file.
'''
def ideologyAnalysis(dataSet, questionsList):
newCSV = open('Polarization 2014 public (with appended Ideology scores).csv', mode='w')
writer = csv.writer(newCSV)
csvfile = open(dataSet)
readCSV = csv.reader(csvfile, delimiter=',')
#takes in the field names
indexes = next(readCSV)
writer.writerow(indexes)
headers = next(readCSV)
writer.writerow(headers)
printList = []
for row in readCSV:
new_row = row
respondent = Respondent(row)
for question in questionsList:
respondent.checkQuestion(question)
if respondent.ideology_denominator != 0.0:
respondent.calculateIdeologyScore()
#language to print results to shell for every 100 results
printList.append(str(respondent.ideology_score))
if len(printList) == 100:
print(printList)
printList = []
#print("resulting ideology score: " + str(respondent.ideology_score))
new_row.append(respondent.ideology_score)
writer.writerow(new_row)
newCSV.close()
csvfile.close()
main('Polarization 2014 public.csv','question parameters.csv')
This diff is collapsed.
This diff is collapsed.
'''
MATH 216 Final Project - John Angileri
Question class
'''
class Question:
def __init__(self, questionIndex, leftLeaningResponse, rightLeaningResponse, valueAdded):
self.questionIndex = questionIndex
self.leftLeaningResponseList = leftLeaningResponse #list
self.rightLeaningResponseList = rightLeaningResponse #list
self.valueAdded = valueAdded
'''
MATH 216 Final Project - John Angileri
Respondent class
'''
class Respondent:
def __init__(self, responses):
self.responses = responses
self.ideology_denominator = 0.0
self.ideology_numerator = 0.0
self.ideology_score = 0.0
def checkQuestion(self, Question):
answer = self.responses[Question.questionIndex]
if answer in Question.rightLeaningResponseList:
self.ideology_denominator += Question.valueAdded
self.ideology_numerator += Question.valueAdded
#print("right leaning response (+" + str(Question.valueAdded) + ")")
if answer in Question.leftLeaningResponseList:
self.ideology_denominator += Question.valueAdded
self.ideology_numerator -= Question.valueAdded
#print("left leaning response (-" + str(Question.valueAdded) + ")")
def calculateIdeologyScore(self):
self.ideology_score = self.ideology_numerator / self.ideology_denominator
Index,Left Leaning Response A,Left Leaning Response B,Right Leaning Response A,Right Leaning Response B,Value Added
25,1,,2,,1
36,3,4,1,2,1
43,1,,,,1
37,1,2,3,4,1
44,,,1,,1
39,1,2,3,4,1
40,3,4,1,2,1
42,1,2,3,4,1
46,2,,1,,1
47,1,,2,,1
48,2,,1,,1
49,1,,2,,1
50,1,,2,,1
51,1,,2,,1
52,2,,1,,1
53,2,,1,,1
54,1,,2,,1
55,2,,1,,1
58,1,,2,,1
61,1,,2,,1
77,2,,1,,1
78,1,,2,,1
81,1,,2,,1
82,2,,1,,1
83,2,,1,,1
84,3,,2,,1
85,1,,2,,1
86,2,,1,,1
93,1,,2,,1
94,2,,1,,1
95,1,,2,,1
96,2,,1,,1
97,1,,2,,1
98,2,,1,,1
99,1,,2,,1
100,2,,1,,1
101,2,,1,,1
103,2,,1,,1
104,2,,1,,1
105,2,,1,,1
108,1,,2,,1
109,2,,1,,1
110,2,,1,,1
114,2,,1,,1
115,1,,2,,1
117,2,,1,,1
122,2,,1,,1
124,2,3,1,,1
125,2,3,1,,1
139,1,2,3,4,1
145,1,,2,,1
148,1,,2,,1
149,1,,,,1
150,2,,1,,1
151,2,,1,,1
154,2,,1,,1
155,,,2,,1
156,2,,,,1
157,1,,2,,1
158,2,,,,1
159,,,2,,1
166,1,,2,,1
167,2,,1,,1
207,,,1,,1
\ No newline at end of file
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment