Skip to content

Commit

Permalink
Parse full sentence when recognition is complete
Browse files Browse the repository at this point in the history
  • Loading branch information
ButterscotchV committed Jan 30, 2024
1 parent 04df123 commit 61b180b
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 9 deletions.
13 changes: 12 additions & 1 deletion ButterSTT/SpeechToTextHandler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -147,9 +147,20 @@ private void OnAprilTokens(AprilResultKind result, AprilToken[] tokens)
? EnglishCapitalization.Capitalize(aprilOutput.ToString().Trim())
: "";

messageQueue.CurParagraph = EnglishTextParser.ParseParagraph(aprilOutputString);
if (result == AprilResultKind.FinalRecognition)
{
messageQueue.CurParagraph = EnglishTextParser.ParseParagraph(
aprilOutputString,
wordRegex: EnglishTextParser.WordKeepUrl()
);
messageQueue.FinishCurrentParagraph();
}
else
{
messageQueue.CurParagraph = EnglishTextParser.ParseParagraph(aprilOutputString);
}

Console.WriteLine(messageQueue.GetCurrentMessage());

try
{
Expand Down
4 changes: 2 additions & 2 deletions ButterSTT/TextProcessing/EnglishCapitalization.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,9 @@ namespace ButterSTT.TextProcessing
{
public static partial class EnglishCapitalization
{
public static string Capitalize(string message)
public static string Capitalize(string message, Regex? regex = null)
{
return BasicCapitals().Replace(message.ToLower(), c => c.Value.ToUpper());
return (regex ?? BasicCapitals()).Replace(message.ToLower(), c => c.Value.ToUpper());
}

// Capitalizes starts of sentences and standalone "I"s, must be run on a lowercase string
Expand Down
23 changes: 17 additions & 6 deletions ButterSTT/TextProcessing/EnglishTextParser.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,20 +5,31 @@ namespace ButterSTT.TextProcessing
{
public static partial class EnglishTextParser
{
public static Paragraph ParseParagraph(string text)
public static Paragraph ParseParagraph(
string text,
Regex? regex = null,
Regex? wordRegex = null,
bool addSpaces = true
)
{
Sentence[] sentences = SentenceKeepUrl()
Sentence[] sentences = (regex ?? SentenceKeepUrl())
.Matches(text)
.Select(m => ParseSentence(m.Value))
.Select(m => ParseSentence(m.Value, regex: wordRegex, addSpaces: addSpaces))
.ToArray();
return new Paragraph(sentences);
}

public static Sentence ParseSentence(string text)
public static Sentence ParseSentence(
string text,
Regex? regex = null,
bool addSpaces = true
)
{
Word[] words = WordOnlyCompleteKeepUrl()
Word[] words = (regex ?? WordOnlyCompleteKeepUrl())
.Matches(text)
.Select(m => new Word(m.Value.EndsWith(' ') ? m.Value : m.Value + " "))
.Select(m => new Word(
addSpaces && !m.Value.EndsWith(' ') ? m.Value + " " : m.Value
))
.ToArray();
return new Sentence(words);
}
Expand Down

0 comments on commit 61b180b

Please sign in to comment.