边界规则微调
parent
6b39723029
commit
629a9217e6
|
|
@ -607,7 +607,7 @@ class Qwen3ASRService:
|
|||
ctx: ConnectionContext,
|
||||
text: str,
|
||||
) -> tuple[str, Optional[int]]:
|
||||
"""Keep punctuation attached to the phrase before an ASR segment cut."""
|
||||
"""Keep revised boundary punctuation on the preceding segment."""
|
||||
current = str(text or "").lstrip()
|
||||
if not current or not ctx.confirmed_segments:
|
||||
return current, None
|
||||
|
|
@ -625,13 +625,65 @@ class Qwen3ASRService:
|
|||
if not previous_text:
|
||||
return current, None
|
||||
leading = current[:punctuation_end]
|
||||
# If the prior pass already closed this phrase, don't stack another mark.
|
||||
if unicodedata.category(previous_text[-1]).startswith("P"):
|
||||
leading = ""
|
||||
if leading:
|
||||
previous["text"] = previous_text + leading
|
||||
# The streaming punctuation cache may revise a prior comma to a full
|
||||
# stop (or revise one terminal mark to another). Replace that suffix
|
||||
# instead of dropping the new decision or stacking punctuation marks.
|
||||
closers = '\"\'”’」』】)》〉〕]]}»'
|
||||
closing_start = len(previous_text)
|
||||
while closing_start > 0 and previous_text[closing_start - 1] in closers:
|
||||
closing_start -= 1
|
||||
body = previous_text[:closing_start]
|
||||
closing_marks = previous_text[closing_start:]
|
||||
punctuation_start = len(body)
|
||||
while punctuation_start > 0:
|
||||
char = body[punctuation_start - 1]
|
||||
if not unicodedata.category(char).startswith("P") or _is_opening_punctuation(char):
|
||||
break
|
||||
punctuation_start -= 1
|
||||
updated_text = body[:punctuation_start] + leading + closing_marks
|
||||
if updated_text != previous_text:
|
||||
previous["text"] = updated_text
|
||||
return current[punctuation_end:].lstrip(), int(previous.get("index", -1))
|
||||
|
||||
@staticmethod
|
||||
def _speaker_boundary_identity(segment: Dict[str, Any]) -> Optional[str]:
|
||||
"""Return a stable public speaker key, ignoring pending/unknown labels."""
|
||||
for key in ("speaker_id", "user_id", "registry_speaker_id", "speaker_name"):
|
||||
value = segment.get(key)
|
||||
if value is None:
|
||||
continue
|
||||
identity = str(value).strip()
|
||||
if not identity or identity in {"-1", "unknown", "说话人不确定"}:
|
||||
continue
|
||||
return f"{key}:{identity}"
|
||||
return None
|
||||
|
||||
@staticmethod
|
||||
def _close_segment_at_speaker_boundary(segment: Dict[str, Any]) -> bool:
|
||||
"""End a speaker turn with a full stop while preserving closing quotes."""
|
||||
text = str(segment.get("text") or "").rstrip()
|
||||
if not text:
|
||||
return False
|
||||
|
||||
closers = '\"\'”’」』】)》〉〕]]}»'
|
||||
closing_start = len(text)
|
||||
while closing_start > 0 and text[closing_start - 1] in closers:
|
||||
closing_start -= 1
|
||||
body = text[:closing_start]
|
||||
closing_marks = text[closing_start:]
|
||||
|
||||
# Keep a model-provided sentence end; turn weak separators into 。.
|
||||
sentence_ends = "。!?.!?…"
|
||||
if body and body[-1] in sentence_ends:
|
||||
return False
|
||||
while body and unicodedata.category(body[-1]).startswith("P"):
|
||||
body = body[:-1]
|
||||
updated = body + "。" + closing_marks
|
||||
if updated == text:
|
||||
return False
|
||||
segment["text"] = updated
|
||||
return True
|
||||
|
||||
async def _send_json_safe(
|
||||
self,
|
||||
websocket: WebSocket,
|
||||
|
|
@ -2808,6 +2860,35 @@ class Qwen3ASRService:
|
|||
segment_end_ms=segment_end_ms,
|
||||
speaker_info=speaker_info,
|
||||
)
|
||||
|
||||
# Speaker changes are definite turn boundaries. Close the previous
|
||||
# bubble only after diarization resolves the current speaker, then
|
||||
# reuse the existing sentences update event to refresh its text.
|
||||
previous_segment = max(
|
||||
(
|
||||
item
|
||||
for item in ctx.confirmed_segments
|
||||
if int(item.get("index", -1)) < int(segment_index)
|
||||
),
|
||||
key=lambda item: int(item.get("index", -1)),
|
||||
default=None,
|
||||
)
|
||||
if previous_segment is not None:
|
||||
previous_identity = self._speaker_boundary_identity(previous_segment)
|
||||
current_identity = self._speaker_boundary_identity(speaker_info)
|
||||
if (
|
||||
previous_identity is not None
|
||||
and current_identity is not None
|
||||
and previous_identity != current_identity
|
||||
and self._close_segment_at_speaker_boundary(previous_segment)
|
||||
):
|
||||
await self._emit_speaker_update(
|
||||
websocket,
|
||||
ctx,
|
||||
task_id,
|
||||
int(previous_segment.get("index", -1)),
|
||||
)
|
||||
|
||||
if before_view != after_view:
|
||||
await self._emit_speaker_update(
|
||||
websocket,
|
||||
|
|
|
|||
Loading…
Reference in New Issue