Skip to content

Commit 8a03c95

Browse files
fix: properly count CJK words in should_discard_conversation
Use unicodedata to detect CJK-dominant text and use character count / 2 as a proxy for word count, instead of whitespace split() which returns 1 for all CJK text. Fixes issue #7065
1 parent 62d8bcd commit 8a03c95

1 file changed

Lines changed: 12 additions & 2 deletions

File tree

backend/utils/llm/conversation_processing.py

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import unicodedata
12
from datetime import datetime, timedelta, timezone
23
from typing import List, Optional, Tuple
34

@@ -159,15 +160,24 @@ class SpeakerIdMatch(BaseModel):
159160
speaker_id: int = Field(description="The speaker id assigned to the segment")
160161

161162

163+
def _word_count(text: str) -> int:
164+
if not text:
165+
return 0
166+
cjk_chars = sum(1 for c in text if unicodedata.east_asian_width(c) in ('W', 'F'))
167+
if cjk_chars > len(text) * 0.3:
168+
return cjk_chars // 2
169+
return len(text.split())
170+
171+
162172
def should_discard_conversation(
163173
transcript: str, photos: List[ConversationPhoto] = None, duration_seconds: Optional[float] = None
164174
) -> bool:
165175
# If there's a long transcript, it's very unlikely we want to discard it.
166176
# This is a performance optimization to avoid unnecessary LLM calls.
167-
if transcript and len(transcript.split(' ')) > 100:
177+
if transcript and _word_count(transcript) > 100:
168178
return False
169179

170-
word_count = len(transcript.split()) if transcript and transcript.strip() else 0
180+
word_count = _word_count(transcript) if transcript and transcript.strip() else 0
171181
has_photos = photos and ConversationPhoto.photos_as_string(photos) != 'None'
172182

173183
context_parts = []

0 commit comments

Comments
 (0)