diff --git a/LICENSE b/LICENSE new file mode 100644 index 0000000..a2a2ff4 --- /dev/null +++ b/LICENSE @@ -0,0 +1,7 @@ +Copyright 2024 Erik Terpstra + +Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the “Software”), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. diff --git a/README.md b/README.md index 6ee5fea..61dd204 100644 --- a/README.md +++ b/README.md @@ -45,6 +45,10 @@ A lightweight Pol.is-like. - strongly agrees with: Companies should be held liable for emissions +## Run tests + + behave + ## Status I focused on small incremental improvements through separation of concerns. diff --git a/features/polislite.feature b/features/polislite.feature new file mode 100644 index 0000000..52a6bcf --- /dev/null +++ b/features/polislite.feature @@ -0,0 +1,10 @@ +Feature: Opinion Analysis + Scenario: Basic opinion analysis with sample data + Given the following statements and votes: + | statements | user1 | user2 | user3 | user4 | user5 | user6 | + | Statement A | agree | agree | agree | agree | agree | agree | + | Statement B | agree | agree | agree | disagree| disagree| disagree| + When I analyze the opinions + Then the statement "Statement A" should show strong consensus + And the statement "Statement B" should be more divisive + And there should be exactly 2 opinion groups identified diff --git a/features/steps/polislite_steps.py b/features/steps/polislite_steps.py new file mode 100644 index 0000000..4110980 --- /dev/null +++ b/features/steps/polislite_steps.py @@ -0,0 +1,52 @@ +from behave import given, when, then +import numpy as np +from polislite import analyze_opinions + + +@given("the following statements and votes") +def step_impl(context): + context.statements = [] + vote_map = {"agree": 1, "disagree": -1} + votes = [] + + for row in context.table: + if row["statements"] != "statements": # Skip header row + context.statements.append(row["statements"]) + vote_row = [vote_map[row[user]] for user in row.headings[1:]] + votes.append(vote_row) + + context.votes = np.array(votes).T # Transpose to match expected format + + +@when("I analyze the opinions") +def step_impl(context): + context.results = analyze_opinions(context.statements, context.votes) + + +@then('the statement "{statement}" should show strong consensus') +def step_impl(context, statement): + consensus_data = dict( + zip( + context.statements, + [(score, agree) for _, score, agree in context.results["consensus_data"]], + ) + ) + score, agreement_level = consensus_data[statement] + assert score > 0.8, f"{statement} should have high agreement" + assert agreement_level < 0.3, f"{statement} should have low variance" + + +@then('the statement "{statement}" should be more divisive') +def step_impl(context, statement): + divisive_data = dict( + zip( + context.statements, [agree for _, agree in context.results["divisive_data"]] + ) + ) + assert divisive_data[statement] > 0.3, f"{statement} should show higher variance" + + +@then("there should be exactly {count:d} opinion groups identified") +def step_impl(context, count): + actual_groups = len(context.results["group_data"]) + assert actual_groups == count, f"Expected {count} groups but found {actual_groups}" diff --git a/polislite.py b/polislite.py index f9544dc..a198485 100644 --- a/polislite.py +++ b/polislite.py @@ -25,13 +25,17 @@ def generate_report(template_path, analysis_results): ) +def analyze_opinions(statements, votes): + analyzer = OpinionAnalyzer() + return analyzer.analyze(votes, statements) + + def main(yaml_file): # Load and prepare data statements, votes = load_from_yaml(yaml_file) # Analyze opinions - analyzer = OpinionAnalyzer() - results = analyzer.analyze(votes, statements) + results = analyze_opinions(statements, votes) # Generate and print report template_path = Path(__file__).parent / "report_template.j2"