-
Notifications
You must be signed in to change notification settings - Fork 504
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
voiceassistant: keep punctuations when sending agent transcription #648
Merged
Merged
Changes from 11 commits
Commits
Show all changes
12 commits
Select commit
Hold shift + click to select a range
b5cd194
wip
theomonnom bfa0c0f
wip
theomonnom 0a95ab4
Update basic.py
theomonnom da007ed
Create quick-owls-relax.md
theomonnom 20e4123
Update livekit-agents/livekit/agents/tokenize/_basic_sent.py
theomonnom 1246510
fix min_sentence_len
theomonnom e013dd3
Merge branch 'theo/better-interim-transript' of https://github.com/li…
theomonnom 0b721c1
Merge branch 'main' into theo/better-interim-transript
theomonnom 5cffbcd
Update test_tokenizer.py
theomonnom 0027af6
Update token_stream.py
theomonnom 9cde2fa
Update token_stream.py
theomonnom 900c828
tts_forwarder: remove trailing punctuation on non-final transcripts
theomonnom File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
--- | ||
"livekit-agents": patch | ||
--- | ||
|
||
voiceassistant: keep punctuations when sending agent transcription |
30 changes: 18 additions & 12 deletions
30
livekit-agents/livekit/agents/tokenize/_basic_paragraph.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,12 +1,18 @@ | ||
def split_paragraphs(text: str) -> list[str]: | ||
sep = "\n\n" | ||
|
||
paragraphs = text.split(sep) | ||
new_paragraphs = [] | ||
for p in paragraphs: | ||
p = p.strip() | ||
if not p: | ||
continue | ||
new_paragraphs.append(p) | ||
|
||
return new_paragraphs | ||
import re | ||
|
||
|
||
def split_paragraphs(text: str) -> list[tuple[str, int, int]]: | ||
""" | ||
Split the text into paragraphs. | ||
Returns a list of paragraphs with their start and end indices of the original text. | ||
""" | ||
matches = re.finditer(r"\n{2,}", text) | ||
paragraphs = [] | ||
|
||
for match in matches: | ||
paragraph = match.group(0) | ||
start_pos = match.start() | ||
end_pos = match.end() | ||
paragraphs.append((paragraph.strip(), start_pos, end_pos)) | ||
|
||
return paragraphs |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,22 +1,35 @@ | ||
import re | ||
|
||
|
||
def split_words(text: str, ignore_punctuation: bool = True) -> list[str]: | ||
def split_words( | ||
text: str, ignore_punctuation: bool = True | ||
) -> list[tuple[str, int, int]]: | ||
""" | ||
Split the text into words. | ||
Returns a list of words with their start and end indices of the original text. | ||
""" | ||
# fmt: off | ||
punctuations = [".", ",", "!", "?", ";", ":", "'", '"', "(", ")", "[", "]", "{", "}", "<", ">", | ||
"—"] | ||
punctuations = ['!', '"', '#', '$', '%', '&', "'", '(', ')', '*', '+', ',', '-', '.', '/', ':', ';', '<', '=', '>', | ||
'?', '@', '[', '\\', ']', '^', '_', '`', '{', '|', '}', '~', '±', '—', '‘', '’', '“', '”', '…'] | ||
|
||
# fmt: on | ||
|
||
if ignore_punctuation: | ||
for p in punctuations: | ||
# TODO(theomonnom): Ignore acronyms | ||
text = text.replace(p, "") | ||
matches = re.finditer(r"\S+", text) | ||
words: list[tuple[str, int, int]] = [] | ||
|
||
for match in matches: | ||
word = match.group(0) | ||
start_pos = match.start() | ||
end_pos = match.end() | ||
|
||
if ignore_punctuation: | ||
# TODO(theomonnom): acronyms passthrough | ||
translation_table = str.maketrans("", "", "".join(punctuations)) | ||
word = word.translate(translation_table) | ||
|
||
if not word: | ||
continue | ||
|
||
words = re.split("[ \n]+", text) | ||
new_words = [] | ||
for word in words: | ||
if not word: | ||
continue # ignore empty | ||
new_words.append(word) | ||
words.append((word, start_pos, end_pos)) | ||
|
||
return new_words | ||
return words |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
maybe overkill but for completeness' sake maybe we should take a slice of the unicode punctuations section?
instead of checking for each of the characters we just take its code and check if it's
0x2000 <-> 0x206F
would be shorter too
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
not sure how I can make this work with str.maketrans, not a big deal, let's keep stuff explicit :)