Skip to content

Add politeness_score metricCreate politeness_score.py #676

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
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
32 changes: 32 additions & 0 deletions metrics/politeness_score/politeness_score.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
import evaluate

_DESCRIPTION = """
Politeness Score: Assigns a politeness rating (0 = rude, 1 = neutral, 2 = polite) to each text.
Use for evaluating LLM outputs for tone and user experience.
"""

_CITATION = ""

class PolitenessScore(evaluate.Metric):
def _info(self):
return evaluate.MetricInfo(
description=_DESCRIPTION,
citation=_CITATION,
inputs_description="List of output strings from LLM.",
features=["predictions"]
)

def _compute(self, predictions):
polite_words = ["please", "thank you", "could you", "would you", "appreciate"]
rude_words = ["idiot", "stupid", "hate", "shut up", "dumb"]

results = []
for text in predictions:
t = text.lower()
if any(w in t for w in rude_words):
results.append(0)
elif any(w in t for w in polite_words):
results.append(2)
else:
results.append(1)
return {"politeness_score": results}