CallOpeningDetX / render.py
SRK-CobuildX's picture
CallOpeningDetX v0.1
0dff23d verified
Raw History Blame Contribute Delete
2.33 kB
# CallOpeningDetX v0.1. Licence: CDLA-Permissive-2.0 (see LICENSE).
"""The input string of a call, and the gold evidence spans in its coordinates.
from render import render, gold_spans
text = render(record) # what a system reads; span offsets refer to this string
spans = gold_spans(record) # [(start, end, type)], types AGENT INST NOTICE PURPOSE NEGATED DISTRACTOR
The string is a metadata line followed by one line per turn:
metadata: starts at call start: <yes|no|not given>; automated notice: <text|not given>
Agent: ...
Customer: ...
"""
FIELD_TYPE = {"agent_identification": "AGENT", "institution_identification": "INST",
"recording_notice": "NOTICE", "recording_purpose": "PURPOSE"}
def _layout(r):
m = r.get("meta") or {}
start = {True: "yes", False: "no"}.get(m.get("starts_at_call_start"), "not given")
auto = m.get("automated_notice_played")
head = f"metadata: starts at call start: {start}; automated notice: "
text = head + ("not given" if auto is None else str(auto))
meta_auto = len(head) if auto is not None else None
starts = []
for t in r["turns"]:
text += f"\n{t['speaker_role']}: "
starts.append(len(text))
text += t["text"]
return text, starts, meta_auto
def render(r):
return _layout(r)[0]
def span_type(e):
if e.get("polarity") == "negated":
return "NEGATED"
if e.get("polarity") == "distractor":
return "DISTRACTOR"
return FIELD_TYPE[e["field"]]
def gold_spans(r):
"""Evidence of the opening (late mentions are not spans). Empty for rows without span supervision."""
if r.get("span_supervision") is False:
return []
text, starts, meta_auto = _layout(r)
out = []
for e in r.get("evidence", []):
if e.get("turn_id") is None: # evidence in the automated-notice metadata
if meta_auto is None or not e.get("text"):
continue
i = text.lower().find(e["text"].lower(), meta_auto)
if 0 <= i < (starts[0] if starts else len(text)):
out.append((i, i + len(e["text"]), span_type(e)))
continue
b = starts[e["turn"]]
out.append((b + e["start"], b + e["end"], span_type(e)))
return out