Skip to content

Instantly share code, notes, and snippets.

@rob-derosa
Last active September 18, 2019 21:40
Show Gist options
  • Save rob-derosa/833b6d9e694d5ba0c68f9dd5c7e98b79 to your computer and use it in GitHub Desktop.
Save rob-derosa/833b6d9e694d5ba0c68f9dd5c7e98b79 to your computer and use it in GitHub Desktop.
//Microsoft 2019
//Rob DeRosa
using System;
using System.IO;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Azure.WebJobs;
using Microsoft.Azure.WebJobs.Extensions.Http;
using Microsoft.AspNetCore.Http;
using Microsoft.Extensions.Logging;
using Newtonsoft.Json;
using System.Linq;
using System.Collections.Generic;
using System.Net.Http;
using System.Net.Http.Headers;
using System.Text;
namespace CustomSkill.Functions
{
public static class CountWords
{
[FunctionName(nameof(CountWords))]
public static async Task<IActionResult> Run([HttpTrigger(AuthorizationLevel.Anonymous, "post", Route = null)] HttpRequest req, ILogger log)
{
log.LogInformation("C# HTTP trigger function processed a request.");
string body = await req.ReadAsStringAsync();
var request = JsonConvert.DeserializeObject<WebApiSkillRequest>(body);
var input = request.Values.First().Data.First().Value.ToString();
input = input.Replace(".", "").Replace("?", "").Replace(",", "").Replace(":", "").Replace("•", "");
var words = input.Split(' ');
var stopWords = "a,able,about,across,after,all,almost,also,am,among,an,and,any,are,as,at,be,because,been,but,by,can,cannot,could,dear,did,do,does,either,else,ever,every,for,from,get,got,had,has,have,he,her,hers,him,his,how,however,i,if,in,into,is,it,its,just,least,let,like,likely,may,me,might,most,must,my,neither,no,nor,not,of,off,often,on,only,or,other,our,own,rather,said,say,says,she,should,since,so,some,than,that,the,their,them,then,there,these,they,this,tis,to,too,twas,us,wants,was,we,were,what,when,where,which,while,who,whom,why,will,with,would,yet,you,your";
var stopArray = stopWords.Split(',');
var coll = new Dictionary<string, int>();
foreach (var word in words)
{
var trimmedWord = word.Trim().ToLower();
if (stopArray.Contains(trimmedWord))
continue;
if (coll.ContainsKey(trimmedWord))
{
coll[trimmedWord] += 1;
}
else
{
coll.Add(trimmedWord, 0);
}
}
var topWords = coll.OrderByDescending(kvp => kvp.Value).Select(kvp => kvp.Key).Take(10);
WebApiResponseRecord output = new WebApiResponseRecord();
output.RecordId = request.Values.First().RecordId;
output.Data["text"] = topWords;
WebApiSkillResponse resp = new WebApiSkillResponse();
resp.Values = new List<WebApiResponseRecord>();
resp.Values.Add(output);
return (ActionResult)new OkObjectResult(resp);
}
}
public class WebApiSkillRequest
{
public List<WebApiRequestRecord> Values { get; set; } = new List<WebApiRequestRecord>();
}
public class WebApiSkillResponse
{
public List<WebApiResponseRecord> Values { get; set; } = new List<WebApiResponseRecord>();
}
public class WebApiRequestRecord
{
public string RecordId { get; set; }
public Dictionary<string, object> Data { get; set; } = new Dictionary<string, object>();
}
public class WebApiResponseRecord
{
public string RecordId { get; set; }
public Dictionary<string, object> Data { get; set; } = new Dictionary<string, object>();
public List<WebApiErrorWarningContract> Errors { get; set; } = new List<WebApiErrorWarningContract>();
public List<WebApiErrorWarningContract> Warnings { get; set; } = new List<WebApiErrorWarningContract>();
}
public class WebApiErrorWarningContract
{
public string Message { get; set; }
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment