Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions LICENSE
Original file line number Diff line number Diff line change
@@ -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.
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,10 @@ A lightweight Pol.is-like.
- strongly agrees with: Companies should be held liable for emissions
</details>

## Run tests

behave

## Status

I focused on small incremental improvements through separation of concerns.
Expand Down
10 changes: 10 additions & 0 deletions features/polislite.feature
Original file line number Diff line number Diff line change
@@ -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
52 changes: 52 additions & 0 deletions features/steps/polislite_steps.py
Original file line number Diff line number Diff line change
@@ -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}"
8 changes: 6 additions & 2 deletions polislite.py
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down