From fd0014b5dccaeceedef88191babcde1f783b3d83 Mon Sep 17 00:00:00 2001 From: DIvan2000 Date: Mon, 17 Mar 2025 17:25:31 +0400 Subject: [PATCH] basic functionality --- main.py | 84 +++++++++++++++++++++++++++++++++++++++++++++++++++-- phi_star.py | 17 ++++++++++- 2 files changed, 98 insertions(+), 3 deletions(-) diff --git a/main.py b/main.py index b383033..eaa5cc7 100644 --- a/main.py +++ b/main.py @@ -1,9 +1,89 @@ import phi_star import csv +from docx import Document +from docx.shared import RGBColor + + +COLUMNS_TRIM = 3 + +CRIT_LOW = 1.62 +CRIT_HIGH = 2.33 if __name__ == '__main__': + males = 0 + fems = 0 + + fem_responses = [] + male_responses = [] + with open("responses.csv", encoding="utf-8") as file: reader = csv.reader(file) + headers = next(reader) + q_count = len(headers) + + for i in range(COLUMNS_TRIM, q_count): + fem_responses.append({}) + male_responses.append({}) + for row in reader: - print(row) - print(phi_star.calculate_phi_star([[1, 1], [1, 1]])) \ No newline at end of file + is_male = False + if row[1] == "мужской": + males+=1 + is_male = True + else: + fems+=1 + for i in range(COLUMNS_TRIM, q_count): + answer_array = row[i].split(";") + for answer in answer_array: + if answer == "": + answer = "не могу дать точный ответ" + if is_male: + male_responses[i - COLUMNS_TRIM][answer] = male_responses[i - COLUMNS_TRIM].get(answer, 0) + 1 + else: + fem_responses[i - COLUMNS_TRIM][answer] = fem_responses[i - COLUMNS_TRIM].get(answer, 0) + 1 + + doc = Document() + + doc.add_heading("Гендерные стереотипы") + + doc.add_paragraph("Кол-во мужчин: " + str(males) + "\nКол-во женщин: " + str(fems) + "\nВсего опрошено: " + str(males + fems)) + + for i in range(0, q_count - COLUMNS_TRIM): + + doc.add_paragraph("\n\n"+str(headers[i+COLUMNS_TRIM])) + + all_keys = set(fem_responses[i].keys()) | set(male_responses[i].keys()) + + doc_table = doc.add_table(len(all_keys) + 1, 4) + doc_table.style = 'Table Grid' + + doc_table.cell(0, 0).text = "Ответ" + doc_table.cell(0, 1).text = "Женщины" + doc_table.cell(0, 2).text = "Мужчины" + doc_table.cell(0, 3).text = "φ*эмп" + + row = 1 + + for answer in all_keys: + male_count = male_responses[i].get(answer, 0) + fem_count = fem_responses[i].get(answer, 0) + + table = [[fem_count, fems - fem_count], + [male_count, males-male_count]] + + phisher = phi_star.calculate(table) + + doc_table.cell(row, 0).text = answer + doc_table.cell(row, 1).text = str(fem_count) + doc_table.cell(row, 2).text = str(male_count) + doc_table.cell(row, 3).text = str(f"{phisher:.3f}") + + if phisher >= CRIT_HIGH: color = RGBColor(0, 128, 0) + elif phisher <= CRIT_LOW: color = RGBColor(255, 0, 0) + else: color = RGBColor(128, 128, 0) + + for cell in doc_table.rows[row].cells: cell.paragraphs[0].runs[0].font.color.rgb = color + + row+=1 + + doc.save("output.docx") diff --git a/phi_star.py b/phi_star.py index 53f6c6f..c231f39 100644 --- a/phi_star.py +++ b/phi_star.py @@ -1,6 +1,6 @@ import math -def calculate_phi_star(table): +def calculate(table): """ Вычисляет эмпирическое значение φ* по 2x2 таблице сопряжённости. @@ -44,3 +44,18 @@ def calculate_phi_star(table): # Вычисляем φ* return math.sqrt(chi_squared_1 + chi_squared_2) + +def validate(table): + group1_ef = table[0][0] + group1_ne = table[0][1] + group2_ef = table[1][0] + group2_ne = table[1][1] + + total_group1 = group1_ef + group1_ne + total_group2 = group2_ef + group2_ne + + if min(total_group1,total_group2) >= 5: return True + if min(total_group1,total_group2) == 2 and max(total_group1,total_group2)>=30: return True + if min(total_group1,total_group2) == 3 and max(total_group1,total_group2)>=7: return True + if min(total_group1,total_group2) == 4 and max(total_group1,total_group2)>=5: return True + return False