Spaces:
Sleeping
Sleeping
HalteroXHunter
commited on
Commit
·
14fb1c1
1
Parent(s):
b4d43c1
bug fix
Browse files- absa_evaluator.py +32 -52
absa_evaluator.py
CHANGED
@@ -171,29 +171,42 @@ class AbsaEvaluator(evaluate.Metric):
|
|
171 |
"relevant": relevant,
|
172 |
}
|
173 |
|
174 |
-
def adjust_predictions(
|
|
|
|
|
175 |
"""Adjust predictions to match the length of references with either a special token or random choice."""
|
176 |
adjusted_preds = []
|
177 |
for ref, pred in zip(refs, preds):
|
178 |
if len(pred) < len(ref):
|
179 |
missing_count = len(ref) - len(pred)
|
180 |
pred.extend([choice(choices) for _ in range(missing_count)])
|
|
|
|
|
181 |
adjusted_preds.append(pred)
|
182 |
return adjusted_preds
|
183 |
|
184 |
|
185 |
-
def extract_aspects(
|
|
|
|
|
186 |
"""Extracts and returns a list of specified aspect details from the nested 'aspects' data."""
|
187 |
return [item[specific_key][specific_val] for item in data]
|
188 |
|
189 |
|
190 |
-
def absa_term_preprocess(
|
|
|
|
|
|
|
|
|
|
|
191 |
"""
|
192 |
Preprocess the terms and polarities for aspect-based sentiment analysis.
|
193 |
|
194 |
Args:
|
195 |
references (List[Dict]): A list of dictionaries containing the actual terms and polarities under 'aspects'.
|
196 |
predictions (List[Dict]): A list of dictionaries containing predicted aspect categories to terms and their sentiments.
|
|
|
|
|
197 |
|
198 |
Returns:
|
199 |
Tuple[List[str], List[str], List[str], List[str]]: A tuple containing lists of true aspect terms,
|
@@ -208,12 +221,7 @@ def absa_term_preprocess(references, predictions, subtask_key, subtask_value):
|
|
208 |
|
209 |
# Define adjustment parameters
|
210 |
special_token = "NONE" # For missing aspect terms
|
211 |
-
sentiment_choices =
|
212 |
-
"positive",
|
213 |
-
"negative",
|
214 |
-
"neutral",
|
215 |
-
"conflict",
|
216 |
-
] # For missing polarities
|
217 |
|
218 |
# Adjust the predictions to match the length of references
|
219 |
adjusted_pred_terms = adjust_predictions(
|
@@ -235,48 +243,20 @@ def flatten_list(nested_list):
|
|
235 |
"""Flatten a nested list into a single-level list."""
|
236 |
return list(chain.from_iterable(nested_list))
|
237 |
|
|
|
|
|
|
|
238 |
|
239 |
-
|
240 |
-
|
241 |
-
) -> List[List]:
|
242 |
-
"""Extract and organize predicted terms from the sentiment analysis results."""
|
243 |
-
pred_aspect_terms = []
|
244 |
-
for pred in all_predictions:
|
245 |
-
terms = [term for cat in pred.values() for term in cat.keys()]
|
246 |
-
pred_aspect_terms.append(terms)
|
247 |
-
return pred_aspect_terms
|
248 |
-
|
249 |
-
|
250 |
-
def merge_aspects_and_categories(aspects, categories):
|
251 |
-
result = []
|
252 |
-
|
253 |
-
# Assuming both lists are of the same length and corresponding indices match
|
254 |
-
for aspect, category in zip(aspects, categories):
|
255 |
-
combined_entry = {
|
256 |
-
"aspects": {"term": [], "polarity": []},
|
257 |
-
"category": {"category": [], "polarity": []},
|
258 |
-
}
|
259 |
-
|
260 |
-
# Process aspect entries
|
261 |
-
for cat_key, terms_dict in aspect.items():
|
262 |
-
for term, polarity in terms_dict.items():
|
263 |
-
combined_entry["aspects"]["term"].append(term)
|
264 |
-
combined_entry["aspects"]["polarity"].append(polarity)
|
265 |
-
|
266 |
-
# Add category details based on the aspect's key if available in categories
|
267 |
-
if cat_key in category:
|
268 |
-
combined_entry["category"]["category"].append(cat_key)
|
269 |
-
combined_entry["category"]["polarity"].append(
|
270 |
-
category[cat_key]
|
271 |
-
)
|
272 |
-
|
273 |
-
# Ensure all keys in category are accounted for
|
274 |
-
for cat_key, polarity in category.items():
|
275 |
-
if cat_key not in combined_entry["category"]["category"]:
|
276 |
-
combined_entry["category"]["category"].append(cat_key)
|
277 |
-
combined_entry["category"]["polarity"].append(polarity)
|
278 |
-
|
279 |
-
result.append(combined_entry)
|
280 |
-
|
281 |
-
return result
|
282 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
171 |
"relevant": relevant,
|
172 |
}
|
173 |
|
174 |
+
def adjust_predictions(
|
175 |
+
refs: List[List[Any]], preds: List[List[Any]], choices: List[Any]
|
176 |
+
) -> List[List[Any]]:
|
177 |
"""Adjust predictions to match the length of references with either a special token or random choice."""
|
178 |
adjusted_preds = []
|
179 |
for ref, pred in zip(refs, preds):
|
180 |
if len(pred) < len(ref):
|
181 |
missing_count = len(ref) - len(pred)
|
182 |
pred.extend([choice(choices) for _ in range(missing_count)])
|
183 |
+
elif len(pred) > len(ref):
|
184 |
+
pred = pred[:len(ref)]
|
185 |
adjusted_preds.append(pred)
|
186 |
return adjusted_preds
|
187 |
|
188 |
|
189 |
+
def extract_aspects(
|
190 |
+
data: List[Dict[str, Dict[str, Any]]], specific_key: str, specific_val: str
|
191 |
+
) -> List[List[Any]]:
|
192 |
"""Extracts and returns a list of specified aspect details from the nested 'aspects' data."""
|
193 |
return [item[specific_key][specific_val] for item in data]
|
194 |
|
195 |
|
196 |
+
def absa_term_preprocess(
|
197 |
+
references: List[Dict[str, Any]],
|
198 |
+
predictions: List[Dict[str, Any]],
|
199 |
+
subtask_key: str,
|
200 |
+
subtask_value: str,
|
201 |
+
) -> Tuple[List[str], List[str], List[str], List[str]]:
|
202 |
"""
|
203 |
Preprocess the terms and polarities for aspect-based sentiment analysis.
|
204 |
|
205 |
Args:
|
206 |
references (List[Dict]): A list of dictionaries containing the actual terms and polarities under 'aspects'.
|
207 |
predictions (List[Dict]): A list of dictionaries containing predicted aspect categories to terms and their sentiments.
|
208 |
+
subtask_key (str): The key under which aspects are stored.
|
209 |
+
subtask_value (str): The specific aspect value to extract.
|
210 |
|
211 |
Returns:
|
212 |
Tuple[List[str], List[str], List[str], List[str]]: A tuple containing lists of true aspect terms,
|
|
|
221 |
|
222 |
# Define adjustment parameters
|
223 |
special_token = "NONE" # For missing aspect terms
|
224 |
+
sentiment_choices = unique_strings(flatten_list(truth_polarities))
|
|
|
|
|
|
|
|
|
|
|
225 |
|
226 |
# Adjust the predictions to match the length of references
|
227 |
adjusted_pred_terms = adjust_predictions(
|
|
|
243 |
"""Flatten a nested list into a single-level list."""
|
244 |
return list(chain.from_iterable(nested_list))
|
245 |
|
246 |
+
def unique_strings(strings: List[str]) -> List[str]:
|
247 |
+
"""
|
248 |
+
Convert a list of strings to a list of unique strings, preserving the original order.
|
249 |
|
250 |
+
Args:
|
251 |
+
strings (List[str]): The input list of strings.
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
252 |
|
253 |
+
Returns:
|
254 |
+
List[str]: A list of unique strings in the order of their first occurrence.
|
255 |
+
"""
|
256 |
+
seen = set()
|
257 |
+
unique_list = []
|
258 |
+
for string in strings:
|
259 |
+
if string not in seen:
|
260 |
+
seen.add(string)
|
261 |
+
unique_list.append(string)
|
262 |
+
return unique_list
|