sohojoe commited on
Commit
12f4772
1 Parent(s): 245e7f9

fix issues with _should_we_send_to_voice and add a test

Browse files
streaming_chat_service.py CHANGED
@@ -26,16 +26,30 @@ class StreamingChatService:
26
 
27
  def _should_we_send_to_voice(self, sentence):
28
  sentence_termination_characters = [".", "?", "!"]
 
 
29
  temination_charicter_present = any(c in sentence for c in sentence_termination_characters)
30
- if temination_charicter_present and sentence[-1] not in sentence_termination_characters:
31
- # text_to_speak = sentence up until the last sentence termination character
32
- termination_indices = [sentence.rfind(char) for char in sentence_termination_characters]
33
- last_termination_index = max(termination_indices)
34
- text_to_speak = sentence[:last_termination_index+1]
35
- return text_to_speak
36
- if temination_charicter_present:
37
- return False
38
- return False
 
 
 
 
 
 
 
 
 
 
 
 
39
 
40
  def respond_to(self, prompt):
41
  self._messages.append({"role": "user", "content": prompt})
 
26
 
27
  def _should_we_send_to_voice(self, sentence):
28
  sentence_termination_characters = [".", "?", "!"]
29
+ close_brackets = ['"', ')', ']']
30
+
31
  temination_charicter_present = any(c in sentence for c in sentence_termination_characters)
32
+
33
+ # early exit if we don't have a termination character
34
+ if not temination_charicter_present:
35
+ return None
36
+
37
+ # early exit the last char is a termination character
38
+ if sentence[-1] in sentence_termination_characters:
39
+ return None
40
+
41
+ # early exit the last char is a close bracket
42
+ if sentence[-1] in close_brackets:
43
+ return None
44
+
45
+ termination_indices = [sentence.rfind(char) for char in sentence_termination_characters]
46
+ last_termination_index = max(termination_indices)
47
+ # handle case of close bracket
48
+ while last_termination_index+1 < len(sentence) and sentence[last_termination_index+1] in close_brackets:
49
+ last_termination_index += 1
50
+
51
+ text_to_speak = sentence[:last_termination_index+1]
52
+ return text_to_speak
53
 
54
  def respond_to(self, prompt):
55
  self._messages.append({"role": "user", "content": prompt})
tests/test_streaming_chat_service.py ADDED
@@ -0,0 +1,50 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import unittest
2
+ import sys
3
+ import os
4
+ sys.path.append(os.path.dirname(os.path.dirname(os.path.abspath(__file__))))
5
+
6
+ from streaming_chat_service import StreamingChatService
7
+
8
+
9
+ class TestShouldWeSendToVoice(unittest.TestCase):
10
+ def setUp(self):
11
+ self.chat_service = StreamingChatService()
12
+
13
+ def test_should_we_send_to_voice(self):
14
+ test_cases = [
15
+ ('This is a simple sentence.', None),
16
+ ('This is a simple sentence?', None),
17
+ ('This is a simple sentence!', None),
18
+ ('This is a simple sentence. ', 'This is a simple sentence.'),
19
+ ('This is a simple sentence? ', 'This is a simple sentence?'),
20
+ ('This is a simple sentence! ', 'This is a simple sentence!'),
21
+ ('This is a simple sentence; ', None),
22
+ ('This is a simple sentence: ', None),
23
+
24
+ ('This sentence contains a quote: "Hello, world!".', None),
25
+ ('This sentence contains a quote: "Hello, world!"..', None),
26
+ ('This sentence contains a quote: "Hello, world!"...', None),
27
+ ('This sentence contains a quote: "Hello, world!". ', 'This sentence contains a quote: "Hello, world!".'),
28
+ ('This sentence contains a quote: "Hello, world!".\n', 'This sentence contains a quote: "Hello, world!".'),
29
+ ('This sentence contains a quote with a question: "How are you?"', None),
30
+ ('This sentence contains a quote with a question: "How are you?" Hello', 'This sentence contains a quote with a question: "How are you?"'),
31
+ ('This sentence has nested quotes: "She said, \'Hello, world!\'".', None),
32
+ ('This sentence has nested quotes: "She said, \'Hello, world!\'". Hello', 'This sentence has nested quotes: "She said, \'Hello, world!\'".'),
33
+ ('This sentence has nested parentheses: (This is (a test).)', None),
34
+ ('This sentence has nested parentheses: (This is (a test).) Hello', 'This sentence has nested parentheses: (This is (a test).)'),
35
+ ('This sentence has nested brackets: [This is [a test].]', None),
36
+ ('This sentence has nested brackets: [This is [a test].] Hello', 'This sentence has nested brackets: [This is [a test].]'),
37
+ ('This sentence has multiple consecutive termination characters: "What!?".', None),
38
+ ('This sentence has multiple consecutive termination characters: "What!?". ', 'This sentence has multiple consecutive termination characters: "What!?".'),
39
+ ('This sentence has multiple consecutive termination characters and a quote: "What!?"', None),
40
+ ('This sentence has multiple consecutive termination characters and a quote: "What!?" ', 'This sentence has multiple consecutive termination characters and a quote: "What!?"'),
41
+ ('This sentence has multiple consecutive termination characters and a quote: "What!?" H', 'This sentence has multiple consecutive termination characters and a quote: "What!?"'),
42
+ ]
43
+
44
+ for sentence, expected_output in test_cases:
45
+ with self.subTest(sentence=sentence):
46
+ actual_output = self.chat_service._should_we_send_to_voice(sentence)
47
+ self.assertEqual(actual_output, expected_output)
48
+
49
+ if __name__ == '__main__':
50
+ unittest.main()