Skip to content

Commit

Permalink
Refactoring C# and Node code to Zummer
Browse files Browse the repository at this point in the history
  • Loading branch information
mohammed-moussa committed Feb 15, 2017
1 parent 477c1ad commit a6b59f7
Show file tree
Hide file tree
Showing 63 changed files with 843 additions and 1,244 deletions.
Binary file removed CSharp/intelligence-Zummer/Content/binglogo.jpg
Binary file not shown.
57 changes: 11 additions & 46 deletions CSharp/intelligence-Zummer/Dialogs/MainDialog.cs
Original file line number Diff line number Diff line change
@@ -1,11 +1,9 @@
using System;
using System.Text.RegularExpressions;
using System.Threading.Tasks;
using Microsoft.Bot.Builder.Dialogs;
using Microsoft.Bot.Builder.Internals.Fibers;
using Microsoft.Bot.Builder.Luis;
using Microsoft.Bot.Builder.Luis.Models;
using Microsoft.Bot.Builder.Scorables;
using Microsoft.Bot.Connector;
using Zummer.Handlers;

Expand All @@ -15,69 +13,36 @@ namespace Zummer.Dialogs
/// The top-level natural language dialog for sample.
/// </summary>
[Serializable]
[LuisModel("", "")]
internal sealed class MainDialog : DispatchDialog
internal sealed class MainDialog : LuisDialog<object>
{
private readonly IHandlerFactory handlerFactory;
private readonly ILuisService luis;


public MainDialog(ILuisService luis, IHandlerFactory handlerFactory)
: base(luis)
{
SetField.NotNull(out this.handlerFactory, nameof(handlerFactory), handlerFactory);
SetField.NotNull(out this.luis, nameof(luis), luis);
}

/// <summary>
/// This method process the summarization requests from the client by matching regex pattern on message text
/// this request is sent when the user clicks on "Read Summary" button. A messages is submitted with value
/// contains summary keyword at the beginning and url of article to summarize.
/// Note: HeroCard is created in <see cref="Utilities.CardGenerators.ArticleCardGenerator"/>
/// </summary>
/// <param name="context">Dialog context</param>
/// <param name="activity">Message activity containing the message information such as text</param>
/// <param name="result">The regex Match with the Regex pattern</param>
/// <returns>Async Task</returns>
[RegexPattern("(^summary )(.*)")]
[ScorableGroup(0)]
public async Task SummarizationRegexHandlerAsync(IDialogContext context, IMessageActivity activity, Match result)
{
await this.handlerFactory.CreateRegexHandler(ZummerStrings.SummarizeActionName).Respond(activity, result);
context.Wait(this.ActivityReceivedAsync);
}

[LuisIntent(ZummerStrings.GreetingIntentName)]
[ScorableGroup(1)]
public async Task GreetingIntentHandlerAsync(IDialogContext context, IMessageActivity activity, LuisResult result)
public async Task GreetingIntentHandlerAsync(IDialogContext context, IAwaitable<IMessageActivity> activity, LuisResult result)
{
await this.handlerFactory.CreateIntentHandler(ZummerStrings.GreetingIntentName).Respond(activity, result);
context.Wait(this.ActivityReceivedAsync);
context.Wait(this.MessageReceived);
}

[LuisIntent(ZummerStrings.ArticlesIntentName)]
[ScorableGroup(1)]
public async Task FindArticlesIntentHandlerAsync(IDialogContext context, IMessageActivity activity, LuisResult result)
[LuisIntent(ZummerStrings.SearchIntentName)]
public async Task FindArticlesIntentHandlerAsync(IDialogContext context, IAwaitable<IMessageActivity> activity, LuisResult result)
{
await this.handlerFactory.CreateIntentHandler(ZummerStrings.ArticlesIntentName).Respond(activity, result);
context.Wait(this.ActivityReceivedAsync);
await this.handlerFactory.CreateIntentHandler(ZummerStrings.SearchIntentName).Respond(activity, result);
context.Wait(this.MessageReceived);
}

[LuisIntent("")]
[LuisIntent("None")]
[ScorableGroup(1)]
public async Task FallbackIntentHandlerAsync(IDialogContext context, IMessageActivity activity)
public async Task FallbackIntentHandlerAsync(IDialogContext context, LuisResult result)
{
await context.PostAsync(string.Format(Strings.FallbackIntentMessage));
context.Wait(this.ActivityReceivedAsync);
}

protected override ILuisService MakeService(ILuisModel model)
{
return this.luis;
}

protected override Regex MakeRegex(string pattern)
{
return new Regex(pattern, RegexOptions.IgnoreCase);
context.Wait(this.MessageReceived);
}
}
}
94 changes: 0 additions & 94 deletions CSharp/intelligence-Zummer/Handlers/ArticlesIntentHandler.cs

This file was deleted.

20 changes: 4 additions & 16 deletions CSharp/intelligence-Zummer/Handlers/GreetingIntentHandler.cs
Original file line number Diff line number Diff line change
@@ -1,12 +1,9 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using System.Threading.Tasks;
using Microsoft.Bot.Builder.Dialogs;
using Microsoft.Bot.Builder.Dialogs.Internals;
using Microsoft.Bot.Builder.Internals.Fibers;
using Microsoft.Bot.Builder.Luis.Models;
using Microsoft.Bot.Connector;
using Zummer.Utilities.CardGenerators;

namespace Zummer.Handlers
{
Expand All @@ -19,18 +16,9 @@ public GreetingIntentHandler(IBotToUser botToUser)
SetField.NotNull(out this.botToUser, nameof(botToUser), botToUser);
}

public async Task Respond(IMessageActivity activity, LuisResult result)
public async Task Respond(IAwaitable<IMessageActivity> activity, LuisResult result)
{
var reply = this.botToUser.MakeMessage();

reply.Attachments.Add(CardGenerator.GetHeroCard(text: string.Format(Strings.GreetOnDemand)));
reply.Attachments.Add(CardGenerator.GetThumbNailCard(
cardActions: new List<CardAction>
{
new CardAction(ActionTypes.OpenUrl, Strings.BingForMore, value: "https://www.bing.com")
}));

await this.botToUser.PostAsync(reply);
await this.botToUser.PostAsync(Strings.GreetOnDemand);
}
}
}
5 changes: 0 additions & 5 deletions CSharp/intelligence-Zummer/Handlers/HandlerFactory.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,5 @@ public IIntentHandler CreateIntentHandler(string key)
{
return this.scope.ResolveNamed<IIntentHandler>(key);
}

public IRegexHandler CreateRegexHandler(string key)
{
return this.scope.ResolveNamed<IRegexHandler>(key);
}
}
}
2 changes: 0 additions & 2 deletions CSharp/intelligence-Zummer/Handlers/IHandlerFactory.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,5 @@
public interface IHandlerFactory
{
IIntentHandler CreateIntentHandler(string key);

IRegexHandler CreateRegexHandler(string key);
}
}
2 changes: 1 addition & 1 deletion CSharp/intelligence-Zummer/Handlers/IIntentHandler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,6 @@ namespace Zummer.Handlers
{
public interface IIntentHandler
{
Task Respond(IMessageActivity activity, LuisResult result);
Task Respond(IAwaitable<IMessageActivity> activity, LuisResult result);
}
}
12 changes: 0 additions & 12 deletions CSharp/intelligence-Zummer/Handlers/IRegexHandler.cs

This file was deleted.

89 changes: 89 additions & 0 deletions CSharp/intelligence-Zummer/Handlers/SearchIntentHandler.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
using System;
using System.Linq;
using System.Threading.Tasks;
using System.Web;
using Microsoft.Bot.Builder.Dialogs;
using Microsoft.Bot.Builder.Dialogs.Internals;
using Microsoft.Bot.Builder.Internals.Fibers;
using Microsoft.Bot.Builder.Luis;
using Microsoft.Bot.Builder.Luis.Models;
using Microsoft.Bot.Connector;
using Zummer.Models.Search;
using Zummer.Services;

namespace Zummer.Handlers
{
internal sealed class SearchIntentHandler : IIntentHandler
{
private readonly ISearchService bingSearchService;
private readonly ISummarizeService bingSummarizeService;
private readonly IBotToUser botToUser;

public SearchIntentHandler(IBotToUser botToUser, ISearchService bingSearchService, ISummarizeService bingSummarizeService)
{
SetField.NotNull(out this.bingSearchService, nameof(bingSearchService), bingSearchService);
SetField.NotNull(out this.bingSummarizeService, nameof(bingSummarizeService), bingSummarizeService);
SetField.NotNull(out this.botToUser, nameof(botToUser), botToUser);
}

public async Task Respond(IAwaitable<IMessageActivity> activity, LuisResult result)
{
EntityRecommendation entityRecommendation;

var query = result.TryFindEntity(ZummerStrings.ArticlesEntityTopic, out entityRecommendation)
? entityRecommendation.Entity
: result.Query;

await this.botToUser.PostAsync(string.Format(Strings.SearchTopicTypeMessage));

var bingSearch = await this.bingSearchService.FindArticles(query);

var zummerResult = this.PrepareZummerResult(query, bingSearch.webPages.value[0]);

var bingSummary = await this.bingSummarizeService.GetSummary(zummerResult.Url);

if (bingSummary?.Data != null && bingSummary.Data.Length != 0)
{
var summaryText = bingSummary.Data.Aggregate(
$"### [{zummerResult.Tile}]({zummerResult.Url})"
+ "\n" +
$"**{Strings.SummaryString}**"
+ "\n\n",
(current, datum) => current + (datum.Text + "\n\n"));

summaryText +=
$"*{string.Format(Strings.PowerBy, $"[Bing™](https://www.bing.com/search/?q={zummerResult.Query} site:wikipedia.org)")}*";

await this.botToUser.PostAsync(summaryText);
}
else
{
await this.botToUser.PostAsync(Strings.SummaryErrorMessage);
}
}

private ZummerSearchResult PrepareZummerResult(string query, Value page)
{
string url;
var myUri = new Uri(page.url);

if (myUri.Host == "www.bing.com" && myUri.AbsolutePath == "/cr")
{
url = HttpUtility.ParseQueryString(myUri.Query).Get("r");
}
else
{
url = page.url;
}

var zummerResult = new ZummerSearchResult
{
Url = url,
Query = query,
Tile = page.name
};

return zummerResult;
}
}
}
Loading

0 comments on commit a6b59f7

Please sign in to comment.