Skip to content

Commit

Permalink
Moved text normalization logic earlier and amended markdown handling (#…
Browse files Browse the repository at this point in the history
…212)

* Moved logic for text normalization earlier in the pipeline into the MergeActivities method.

* Modified markdown renderer to use period instead of new line. Amended list rendering. Added tests. Changed default to markdown.
  • Loading branch information
garypretty authored Mar 25, 2020
1 parent 7cd58ff commit 572851b
Show file tree
Hide file tree
Showing 3 changed files with 75 additions and 21 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ public SkillResponse ActivityToResponse(Activity activity, SkillRequest alexaReq
Response = new ResponseBody()
};

if (activity == null || alexaRequest.Request is SessionEndedRequest)
if (activity == null || activity.Type != ActivityTypes.Message || alexaRequest.Request is SessionEndedRequest)
{
response.Response.ShouldEndSession = true;
response.Response.OutputSpeech = new PlainTextOutputSpeech
Expand All @@ -99,7 +99,7 @@ public SkillResponse ActivityToResponse(Activity activity, SkillRequest alexaReq
}
else
{
response.Response.OutputSpeech = new PlainTextOutputSpeech(NormalizeActivityText(activity.TextFormat, activity.Text));
response.Response.OutputSpeech = new PlainTextOutputSpeech(activity.Text);
}

ProcessActivityAttachments(activity, response);
Expand All @@ -113,7 +113,7 @@ public SkillResponse ActivityToResponse(Activity activity, SkillRequest alexaReq
break;
case InputHints.ExpectingInput:
response.Response.ShouldEndSession = false;
response.Response.Reprompt = new Reprompt(NormalizeActivityText(activity.TextFormat, activity.Text));
response.Response.Reprompt = new Reprompt(activity.Text);
break;
default:
response.Response.ShouldEndSession = _options.ShouldEndSessionByDefault;
Expand All @@ -136,25 +136,27 @@ public SkillResponse ActivityToResponse(Activity activity, SkillRequest alexaReq
/// <returns>Activity</returns>
public Activity MergeActivities(IList<Activity> activities)
{
if (activities == null || activities.Count == 0)
if (activities == null || activities.All(a => a.Type != ActivityTypes.Message))
{
return null;
}

var activity = activities.Last();
var messageActivities = activities.Where(a => a.Type == ActivityTypes.Message).ToList();

if (activities.Any(a => !string.IsNullOrEmpty(a.Speak)))
var activity = messageActivities.Last();

if (messageActivities.Any(a => !string.IsNullOrEmpty(a.Speak)))
{
var speakText = string.Join("<break strength=\"strong\"/>", activities
.Select(a => !string.IsNullOrEmpty(a.Speak) ? StripSpeakTag(a.Speak) : a.Text)
var speakText = string.Join("<break strength=\"strong\"/>", messageActivities
.Select(a => !string.IsNullOrEmpty(a.Speak) ? StripSpeakTag(a.Speak) : NormalizeActivityText(a.TextFormat, a.Text))
.Where(s => !string.IsNullOrEmpty(s))
.Select(s => s));

activity.Speak = $"<speak>{speakText}</speak>";
}

activity.Text = string.Join(". ", activities
.Select(a => a.Text)
activity.Text = string.Join(". ", messageActivities
.Select(a => NormalizeActivityText(a.TextFormat, a.Text))
.Where(s => !string.IsNullOrEmpty(s))
.Select(s => s.Trim(new char[] { ' ', '.' })));

Expand Down Expand Up @@ -284,10 +286,10 @@ private string NormalizeActivityText(string textFormat, string text)
return string.Empty;
}

// Default to plain text if it isn't specified.
// Default to markdown if it isn't specified.
if (textFormat == null)
{
textFormat = TextFormatTypes.Plain;
textFormat = TextFormatTypes.Markdown;
}

string plainText;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,14 +16,14 @@ private class RemoveMarkupRenderer : MarkdownRenderer
{
private const string ListItemMarker = "$$ListItemMarker$$";

public override string Blockquote(string quote) => string.Concat(Environment.NewLine, quote, Environment.NewLine);
public override string Br() => Environment.NewLine;
public override string Blockquote(string quote) => string.Concat(quote, ". ");
public override string Br() => ". ";
public override string Code(string code, string lang, bool escaped) => code;
public override string Codespan(string text) => text;
public override string Del(string text) => text;
public override string Em(string text) => text;
public override string Heading(string text, int level, string raw) => string.Concat(Environment.NewLine, text, Environment.NewLine);
public override string Hr() => Environment.NewLine;
public override string Heading(string text, int level, string raw) => string.Concat(text, ". ");
public override string Hr() => ". ";
public override string Html(string html) => string.Empty;
public override string Image(string href, string title, string text) => title ?? text;
public override string Link(string href, string title, string text) => $"{title ?? text} {href}";
Expand All @@ -33,17 +33,17 @@ public override string List(string body, bool ordered, int start)
{
for (int marker = start, markerIndex = body.IndexOf(ListItemMarker); markerIndex >= 0; markerIndex = body.IndexOf(ListItemMarker), ++marker)
{
body = body.Substring(0, markerIndex) + Environment.NewLine + marker + " " + body.Substring(markerIndex + ListItemMarker.Length);
body = body.Substring(0, markerIndex) + marker + ". " + body.Substring(markerIndex + ListItemMarker.Length);
}
}
else
{
body = body.Replace(ListItemMarker, Environment.NewLine);
body = body.Replace(ListItemMarker, string.Empty);
}
return body;
return $"{body.Trim().TrimEnd(',')}. ";
}
public override string ListItem(string text) => $"{ListItemMarker}{text}";
public override string Paragraph(string text) => string.Concat(Environment.NewLine, text, Environment.NewLine);
public override string ListItem(string text) => $"{ListItemMarker}{text}, ";
public override string Paragraph(string text) => string.Concat(text.TrimEnd('.'), ". ");
public override string Strong(string text) => text;
public override string Table(string header, string body) => string.Empty;
public override string TableCell(string content, TableCellFlags flags) => string.Empty;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
using Microsoft.Extensions.Logging;
using Moq;
using System.Collections.Generic;
using System.Text;
using Alexa.NET.Response.Directive;
using Alexa.NET.Response.Directive.Templates;
using Alexa.NET.Response.Directive.Templates.Types;
Expand Down Expand Up @@ -96,6 +97,21 @@ public void MergeActivitiesReturnsCorrectlyJoinedText()
Assert.Equal("This is the first activity. This is the second activity", processActivityResult.Text);
}

[Fact]
public void MergeActivitiesIgnoresNonMessageActivities()
{
var alexaAdapter = new AlexaRequestMapper();

var firstActivity = MessageFactory.Text("This is the first activity.");
var traceActivity = Activity.CreateTraceActivity("This is a trace") as Activity;
var secondActivity = MessageFactory.Text("This is the second activity");
var typingActivity = Activity.CreateTypingActivity() as Activity;

var processActivityResult = alexaAdapter.MergeActivities(new List<Activity>() { firstActivity, traceActivity, secondActivity, typingActivity });

Assert.Equal("This is the first activity. This is the second activity", processActivityResult.Text);
}

[Fact]
public void MergeActivitiesReturnsCorrectlyJoinedSpeakWithSsml()
{
Expand All @@ -113,6 +129,42 @@ public void MergeActivitiesReturnsCorrectlyJoinedSpeakWithSsml()
processActivityResult.Speak);
}

[Fact]
public void MergeActivitiesCorrectlyConvertsMarkdownToPlainText()
{
var alexaAdapter = new AlexaRequestMapper();

// Note: The input activities deliberately have an activity where the speak tag
// is included and one activity where it is not, to ensure the stripping / wrapping
// of the speak tag is handled correctly.

var message = new StringBuilder();
message.AppendLine("**This** ~~is~~ ***the*** first activity.");
message.AppendLine("# Heading 1");
message.AppendLine("This is another paragraph.");
message.AppendLine("- Item 1");
message.AppendLine("- Item 2");
message.AppendLine("- Item 3");
message.AppendLine("");
message.AppendLine("## Heading 2");
message.AppendLine("1. Item 1");
message.AppendLine("2. Item 2");
message.AppendLine("3. Item 3");
message.AppendLine("");
message.AppendLine("More info [visit our web site](www.microsoft.com)");

var firstActivity = MessageFactory.Text(message.ToString(), "This is<break strength=\"strong\"/>the first activity SSML");
var secondActivity = MessageFactory.Text("This is the second activity.", "<speak>This is the second activity SSML</speak>");

var processActivityResult = alexaAdapter.MergeActivities(new List<Activity>() { firstActivity, secondActivity });

Assert.Equal("This is the first activity. Heading 1. This is another paragraph. Item 1, Item 2, Item 3. Heading 2. 1. Item 1, 2. Item 2, 3. Item 3. More info visit our web site www.microsoft.com. This is the second activity",
processActivityResult.Text);

Assert.Equal("<speak>This is<break strength=\"strong\"/>the first activity SSML<break strength=\"strong\"/>This is the second activity SSML</speak>",
processActivityResult.Speak);
}

[Fact]
public void PlainTextMessageActivityConverted()
{
Expand Down

0 comments on commit 572851b

Please sign in to comment.