This repository was archived by the owner on May 23, 2025. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathkeyword_parser.py
More file actions
46 lines (38 loc) · 1.44 KB
/
keyword_parser.py
File metadata and controls
46 lines (38 loc) · 1.44 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
from typing import Any
from .default_parser import DefaultParser
class KeywordParser(DefaultParser):
def __init__(
self, none_value: str | None, case_sensitive: bool, multi_y: bool,
labels: list[str], **kwargs: Any,
) -> None:
super().__init__(
none_value=none_value, case_sensitive=case_sensitive,
multi_y=multi_y, **kwargs,
)
self.labels = [lbl.strip() for lbl in labels]
if not self.case_sensitive:
self.labels = [lbl.lower() for lbl in labels]
# self.punctuations = re.compile(r'\W|\s')
def parse(
self, prompt: str,
output: str | list[str],
) -> str | None | list[str]:
"""Prase LLM response for keywords that can be used for evaluation."""
if isinstance(output, list):
output = [out for out in output if out]
if not output:
return [] if self.multi_y else self.none_value
assert len(
output,
) == 1, f'Multiclass classification not tested. Got: {output}'
output = output[0]
if not self.case_sensitive:
output = output.lower()
# response = self.punctuations.sub(' ', output)
response = output
if self.multi_y:
return [label for label in self.labels if label in response]
for label in self.labels:
if label in response:
return label
return self.none_value