Skip to content
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

Support prediction regex match by setting the operator as a postproce… #792

Merged
merged 3 commits into from
May 7, 2024
Merged
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
28 changes: 28 additions & 0 deletions prepare/metrics/jaccard_index.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
from unitxt import add_to_catalog
from unitxt.metrics import JaccardIndex
from unitxt.test_utils.metrics import test_metric

metric = JaccardIndex()

predictions = [["A", "B", "C"]]
references = [[["B", "A", "D"]]]

instance_targets = [
{"jaccard_index": 0.5, "score": 0.5, "score_name": "jaccard_index"},
]

global_target = {
"jaccard_index": 0.5,
"score": 0.5,
"score_name": "jaccard_index",
}

outputs = test_metric(
metric=metric,
predictions=predictions,
references=references,
instance_targets=instance_targets,
global_target=global_target,
)

add_to_catalog(metric, "metrics.jaccard_index", overwrite=True)
7 changes: 7 additions & 0 deletions prepare/processors/processors.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
LowerCase,
LowerCaseTillPunc,
MatchClosestOption,
RegexParser,
StanceToProCon,
StringOrNotString,
StrToFloatFormat,
Expand Down Expand Up @@ -329,3 +330,9 @@
"processors.extract_mt_bench_judgment",
overwrite=True,
)

add_to_catalog(
RegexParser(field="prediction", regex=".+", process_every_value=False),
"processors.regex_parser_from_prediction",
overwrite=True,
)
10 changes: 10 additions & 0 deletions prepare/processors/to_list_by_comma.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,3 +12,13 @@
"processors.to_list_by_comma",
overwrite=True,
)

add_to_catalog(
SequentialOperator(
steps=[
ToListByComma(field="references", process_every_value=True),
]
),
"processors.to_list_by_comma_from_references",
overwrite=True,
)
3 changes: 3 additions & 0 deletions src/unitxt/catalog/metrics/jaccard_index.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"type": "jaccard_index"
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"type": "regex_parser",
"field": "prediction",
"regex": ".+",
"process_every_value": false
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
{
"type": "sequential_operator",
"steps": [
{
"type": "to_list_by_comma",
"field": "references",
"process_every_value": true
}
]
}
34 changes: 34 additions & 0 deletions src/unitxt/metrics.py
Original file line number Diff line number Diff line change
Expand Up @@ -977,6 +977,40 @@
return result


class JaccardIndex(InstanceMetric):
reduction_map = {"mean": ["jaccard_index"]}
main_score = "jaccard_index"
ci_scores = ["jaccard_index"]

prediction_type = "Any" # string representation is compared

def compute(
self, references: List[Any], prediction: Any, task_data: List[Dict]
) -> dict:
if not isinstance(prediction, set):
prediction = set(prediction)
references = [set(reference) for reference in references]

Check warning on line 992 in src/unitxt/metrics.py

View check run for this annotation

Codecov / codecov/patch

src/unitxt/metrics.py#L990-L992

Added lines #L990 - L992 were not covered by tests

result = {

Check warning on line 994 in src/unitxt/metrics.py

View check run for this annotation

Codecov / codecov/patch

src/unitxt/metrics.py#L994

Added line #L994 was not covered by tests
self.main_score: max(
[
float(
(len(reference.intersection(prediction)))
/ (
len(reference)
+ len(prediction)
- len(reference.intersection(prediction))
)
)
for reference in references
]
)
}
result["score"] = result[self.main_score]
result["score_name"] = self.main_score
return result

Check warning on line 1011 in src/unitxt/metrics.py

View check run for this annotation

Codecov / codecov/patch

src/unitxt/metrics.py#L1009-L1011

Added lines #L1009 - L1011 were not covered by tests


class MaxAccuracy(Accuracy):
"""Calculate the maximal accuracy over all instances as the global score."""

Expand Down
Loading