From d1a36004ab3f88dc7381bebdf432055a9cd5b2e7 Mon Sep 17 00:00:00 2001 From: TomTom101 Date: Sat, 6 Jul 2024 11:01:32 +0200 Subject: [PATCH 1/4] fix: No more falsely detect a sentence end on "U.S.A", "3:00 a.m." and more --- src/pipecat/services/ai_services.py | 13 ++++++++++--- tests/test_ai_services.py | 20 ++++++++++++++++++-- 2 files changed, 28 insertions(+), 5 deletions(-) diff --git a/src/pipecat/services/ai_services.py b/src/pipecat/services/ai_services.py index 06e4b3ebe..e5ab88258 100644 --- a/src/pipecat/services/ai_services.py +++ b/src/pipecat/services/ai_services.py @@ -28,6 +28,15 @@ from pipecat.processors.async_frame_processor import AsyncFrameProcessor from pipecat.processors.frame_processor import FrameDirection, FrameProcessor from pipecat.utils.audio import calculate_audio_volume from pipecat.utils.utils import exp_smoothing +import re + + +endofsentence_pattern = r"(? bool: + return endofsentence_re.search(text.rstrip()) is not None class AIService(FrameProcessor): @@ -137,9 +146,7 @@ class TTSService(AIService): text = frame.text else: self._current_sentence += frame.text - if self._current_sentence.strip().endswith( - (".", "?", "!")) and not self._current_sentence.strip().endswith( - ("Mr,", "Mrs.", "Ms.", "Dr.")): + if match_endofsentence(self._current_sentence): text = self._current_sentence self._current_sentence = "" diff --git a/tests/test_ai_services.py b/tests/test_ai_services.py index ec44d5625..fb00fc893 100644 --- a/tests/test_ai_services.py +++ b/tests/test_ai_services.py @@ -2,8 +2,8 @@ import unittest from typing import AsyncGenerator -from pipecat.services.ai_services import AIService -from pipecat.pipeline.frames import EndFrame, Frame, TextFrame +from pipecat.services.ai_services import AIService, match_endofsentence +from pipecat.frames.frames import EndFrame, Frame, TextFrame class SimpleAIService(AIService): @@ -27,6 +27,22 @@ class TestBaseAIService(unittest.IsolatedAsyncioTestCase): self.assertEqual(input_frames, output_frames) + async def test_endofsentence(self): + assert match_endofsentence("This is a sentence.") + assert match_endofsentence("This is a sentence! ") + assert match_endofsentence("This is a sentence?") + assert match_endofsentence("This is a sentence:") + assert not match_endofsentence("This is not a sentence") + assert not match_endofsentence("This is not a sentence,") + assert not match_endofsentence("This is not a sentence, ") + assert not match_endofsentence("Ok, Mr. Smith let's ") + assert not match_endofsentence("Dr. Walker, I presume ") + assert not match_endofsentence("Prof. Walker, I presume ") + assert not match_endofsentence("zweitens, und 3.") + assert not match_endofsentence("Heute ist Dienstag, der 3.") # 3. Juli 2024 + assert not match_endofsentence("America, or the U.") # U.S.A. + assert not match_endofsentence("It still early, it's 3:00 a.") # 3:00 a.m. + if __name__ == "__main__": unittest.main() From b23db4a20255731067632d3d35ee213e43ea423d Mon Sep 17 00:00:00 2001 From: TomTom101 Date: Sat, 6 Jul 2024 11:06:52 +0200 Subject: [PATCH 2/4] chore: commented regex --- src/pipecat/services/ai_services.py | 15 +++++++++++++-- 1 file changed, 13 insertions(+), 2 deletions(-) diff --git a/src/pipecat/services/ai_services.py b/src/pipecat/services/ai_services.py index e5ab88258..4482d2b50 100644 --- a/src/pipecat/services/ai_services.py +++ b/src/pipecat/services/ai_services.py @@ -31,8 +31,19 @@ from pipecat.utils.utils import exp_smoothing import re -endofsentence_pattern = r"(? bool: From 327ea9d547303f2b2f3cafd0abc13bb63d867639 Mon Sep 17 00:00:00 2001 From: TomTom101 Date: Sat, 6 Jul 2024 11:08:51 +0200 Subject: [PATCH 3/4] chore: Make it a const --- src/pipecat/services/ai_services.py | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/pipecat/services/ai_services.py b/src/pipecat/services/ai_services.py index 4482d2b50..9e28889c4 100644 --- a/src/pipecat/services/ai_services.py +++ b/src/pipecat/services/ai_services.py @@ -31,7 +31,7 @@ from pipecat.utils.utils import exp_smoothing import re -endofsentence_pattern = r""" +ENDOFSENTENCE_PATTERN_STR = r""" (? bool: - return endofsentence_re.search(text.rstrip()) is not None + return ENDOFSENTENCE_PATTERN.search(text.rstrip()) is not None class AIService(FrameProcessor): From da2082b02527b2641caaff15b715797aaadf508f Mon Sep 17 00:00:00 2001 From: TomTom101 Date: Sat, 6 Jul 2024 11:11:40 +0200 Subject: [PATCH 4/4] chore: Combined combinable lookaheads --- src/pipecat/services/ai_services.py | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/src/pipecat/services/ai_services.py b/src/pipecat/services/ai_services.py index 9e28889c4..46bba673e 100644 --- a/src/pipecat/services/ai_services.py +++ b/src/pipecat/services/ai_services.py @@ -35,9 +35,7 @@ ENDOFSENTENCE_PATTERN_STR = r""" (?