Spaces:
Sleeping
Sleeping
File size: 13,917 Bytes
33b0df2 |
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 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 |
#!/usr/bin/env python
"""
Event Constructor for Voting Event Extraction.
This module constructs structured voting events from BIO-tagged predictions.
"""
import logging
from typing import List, Dict, Any, Optional
logger = logging.getLogger(__name__)
class EventConstructor:
"""
Constructs structured voting events from BIO-tagged predictions: stage 2 of the VotIE pipeline.
Event Structure (PAPER-ALIGNED FORMAT):
{
'id': str,
'has_voting_event': bool,
'event': {
'subject': str or None, # SINGULAR: |S| = 1 (first one if multiple)
'participants': [{'text': str, 'position': str}], # LIST: |P| ≥ 0 (multiple allowed)
'counting': [{'text': str, 'type': str}], # LIST: |C| ≥ 0 (multiple allowed)
'voting_expressions': [str], # LIST: |V| ≥ 1 (at least one required)
'outcome': str or None # SINGULAR: O ∈ {Approved, Rejected} or None
}
}
Cardinality Constraints (from paper):
- |S| = 1: Exactly one subject per voting event
- |V| ≥ 1: At least one voting expression required
- |C| ≥ 0: Zero or more counting expressions
- |P| ≥ 0: Zero or more participants
- O: Deterministically inferred outcome
"""
def __init__(self):
"""Initialize the event constructor."""
pass
def construct_event(
self,
tokens: List[str],
labels: List[str],
example_id: str
) -> Dict[str, Any]:
"""
Construct structured event from BIO-tagged sequence.
Args:
tokens: List of word tokens
labels: List of BIO labels (e.g., 'B-SUBJECT', 'I-SUBJECT', 'O')
example_id: Unique identifier for this example
Returns:
Event dictionary with has_voting_event and structured event
"""
# Check if this segment contains a voting event
has_voting = self._has_voting_event(labels)
if not has_voting:
return {
'id': example_id,
'has_voting_event': False,
'event': None
}
# Extract all entity spans
subject = self._extract_subject(tokens, labels)
participants = self._extract_participants(tokens, labels)
counting = self._extract_counting_list(tokens, labels) # Returns LIST
voting_expressions = self._extract_voting_expressions_list(tokens, labels) # Returns LIST
# Infer outcome deterministically from voting expressions, counting, and participants
outcome = self._infer_outcome(voting_expressions, counting, participants)
return {
'id': example_id,
'has_voting_event': True,
'event': {
'subject': subject,
'participants': participants,
'counting': counting,
'voting_expressions': voting_expressions,
'outcome': outcome
}
}
def construct_events(
self,
examples: List[Dict[str, Any]],
predictions: List[List[str]]
) -> List[Dict[str, Any]]:
"""
Construct events for multiple examples.
Args:
examples: List of examples with 'tokens' and 'id' fields
predictions: List of predicted label sequences
Returns:
List of constructed events
"""
events = []
for example, pred_labels in zip(examples, predictions):
event = self.construct_event(
example['tokens'],
pred_labels,
example['id']
)
events.append(event)
return events
def _has_voting_event(self, labels: List[str]) -> bool:
"""
Check if labels contain a voting event.
According to paper: A voting event is defined by |V| ≥ 1 (at least one voting expression).
The presence of a VOTING entity (B-VOTING tag) is the sole criterion for determining
whether a segment contains a voting event.
Returns:
True if at least one B-VOTING tag is found, False otherwise
"""
return any(label.startswith('B-VOTING') for label in labels)
def _extract_subject(
self,
tokens: List[str],
labels: List[str]
) -> Optional[str]:
"""
Extract subject span (single string).
If multiple SUBJECT spans exist, takes the first one (or could concatenate).
Args:
tokens: Word tokens
labels: BIO labels
Returns:
Subject text string or None
"""
spans = self._extract_spans_by_type(tokens, labels, 'SUBJECT')
if not spans:
return None
# Take first subject span
subject_text = ' '.join(spans[0])
return subject_text
def _extract_participants(
self,
tokens: List[str],
labels: List[str]
) -> List[Dict[str, str]]:
"""
Extract participant spans with positions.
Parses VOTER-* labels to extract position (FAVOR, AGAINST, ABSTENTION, ABSENT).
Args:
tokens: Word tokens
labels: BIO labels
Returns:
List of {'text': str, 'position': str} dictionaries
"""
participants = []
# Find all VOTER-* spans
i = 0
while i < len(labels):
label = labels[i]
if label.startswith('B-VOTER-'):
# Extract position from label (e.g., B-VOTER-FAVOR -> FAVOR)
span_type = label[2:] # Remove 'B-' prefix (e.g., 'VOTER-FAVOR')
position = span_type.split('-', 1)[1] # Extract position part
# Collect span tokens
span_tokens = [tokens[i]]
j = i + 1
# Continue with I-VOTER-* tags
while j < len(labels) and labels[j] == f'I-{span_type}':
span_tokens.append(tokens[j])
j += 1
participant_text = ' '.join(span_tokens)
participants.append({
'text': participant_text,
'position': position.capitalize()
})
i = j
else:
i += 1
return participants
def _extract_counting_list(
self,
tokens: List[str],
labels: List[str]
) -> List[Dict[str, str]]:
"""
Extract all counting spans with aggregation types (LIST of dicts).
Parses COUNTING-* labels to extract type (UNANIMITY, MAJORITY).
Returns all counting expressions found.
Args:
tokens: Word tokens
labels: BIO labels
Returns:
List of {'text': str, 'type': str} dictionaries (empty list if none found)
"""
counting_list = []
i = 0
while i < len(labels):
label = labels[i]
if label.startswith('B-COUNTING-'):
# Extract counting type (e.g., B-COUNTING-UNANIMITY -> UNANIMITY)
span_type = label[2:] # Remove 'B-' prefix
counting_type = span_type.split('-', 1)[1] # Extract type part
# Collect span tokens
span_tokens = [tokens[i]]
j = i + 1
# Continue with I-COUNTING-* tags
while j < len(labels) and labels[j] == f'I-{span_type}':
span_tokens.append(tokens[j])
j += 1
counting_text = ' '.join(span_tokens)
counting_list.append({
'text': counting_text,
'type': counting_type.capitalize()
})
i = j
else:
i += 1
return counting_list
def _extract_voting_expressions_list(
self,
tokens: List[str],
labels: List[str]
) -> List[str]:
"""
Extract all voting expression spans (LIST of strings).
Returns all VOTING spans found.
Args:
tokens: Word tokens
labels: BIO labels
Returns:
List of voting expression text strings (empty list if none found)
"""
spans = self._extract_spans_by_type(tokens, labels, 'VOTING')
# Convert all spans to strings and return as list
return [' '.join(span) for span in spans]
def _extract_spans_by_type(
self,
tokens: List[str],
labels: List[str],
span_type: str
) -> List[List[str]]:
"""
Extract all spans of a given type.
Args:
tokens: Word tokens
labels: BIO labels
span_type: Type to extract (e.g., 'SUBJECT', 'VOTING')
Returns:
List of token lists, one per span
"""
spans = []
i = 0
while i < len(labels):
label = labels[i]
if label == f'B-{span_type}':
# Start of span
span_tokens = [tokens[i]]
j = i + 1
# Collect continuation tokens
while j < len(labels) and labels[j] == f'I-{span_type}':
span_tokens.append(tokens[j])
j += 1
spans.append(span_tokens)
i = j
else:
i += 1
return spans
def _infer_outcome(
self,
voting_expressions: List[str],
counting: List[Dict[str, str]],
participants: List[Dict[str, str]]
) -> Optional[str]:
"""
Deterministically infer voting outcome from expressions, counting, and participants.
As per paper: O ∈ {Approved, Rejected} is inferred from V, C, and P using rule-based heuristics.
Heuristic rules (Portuguese municipal context):
1. If Câmara/Executivo Municipal votes in favor, outcome is Approved
2. Look for approval keywords in voting expressions
3. Consider counting type (unanimity often implies approval)
4. Consider participant positions (more favor than against suggests approval)
Args:
voting_expressions: List of voting expression texts
counting: List of counting dicts with 'text' and 'type'
participants: List of participant dicts with 'text' and 'position'
Returns:
'Approved', 'Rejected', or None if outcome cannot be determined
"""
if not voting_expressions and not counting and not participants:
return None
# Rule 1: If Câmara or Executivo Municipal votes in favor, outcome is Approved
import re
for participant in participants:
if participant.get('position') == 'Favor':
text_lower = participant.get('text', '').lower()
# Match patterns like "a câmara", "câmara municipal", "o executivo municipal", etc.
# Pattern allows optional word between "câmara" and other words, or "executivo" and "municipal"
if re.search(r'\bcâmara\b', text_lower) or \
re.search(r'\bexecutivo\s+(\w+\s+)?municipal\b', text_lower):
return 'Approved'
# Approval keywords (Portuguese)
approval_keywords = [
'aprovad', 'deliberad', 'deferido', 'autorizado', 'ratificad',
'homologad', 'sancionad', 'concordad'
]
# Rejection keywords (Portuguese)
rejection_keywords = [
'rejeitad', 'indeferido', 'recusad', 'negad', 'chumbad',
'não aprovad', 'não deferido'
]
# Check voting expressions for outcome keywords
for expr in voting_expressions:
expr_lower = expr.lower()
# Check for rejection first (more specific)
if any(keyword in expr_lower for keyword in rejection_keywords):
return 'Rejected'
# Check for approval
if any(keyword in expr_lower for keyword in approval_keywords):
return 'Approved'
# If unanimity counting exists, likely approved (Portuguese municipal convention)
if any(c.get('type', '').lower() in ['unanimity', 'unanimidade'] for c in counting):
return 'Approved'
# Count participant positions
if participants:
favor_count = sum(1 for p in participants if p.get('position') == 'Favor')
against_count = sum(1 for p in participants if p.get('position') == 'Against')
if favor_count > against_count:
return 'Approved'
elif against_count > favor_count:
return 'Rejected'
# Cannot determine outcome
return None
# Convenience function
def construct_events_from_predictions(
examples: List[Dict[str, Any]],
predictions: List[List[str]]
) -> List[Dict[str, Any]]:
"""
Construct events from BIO predictions.
Args:
examples: List of examples with 'tokens' and 'id'
predictions: List of predicted BIO label sequences
Returns:
List of structured event dictionaries
"""
constructor = EventConstructor()
return constructor.construct_events(examples, predictions) |