BotSharp/BotSharp.Core/Engines/SpaCy/SpaCyTokenizer.cs

58 lines
1.7 KiB
C#
Raw Normal View History

2018-06-14 15:03:01 +00:00
using BotSharp.Core.Abstractions;
2018-06-15 17:39:24 +00:00
using BotSharp.Core.Agents;
2018-06-14 15:03:01 +00:00
using BotSharp.Core.Models;
2018-07-17 16:22:31 +00:00
using BotSharp.MachineLearning.NLP;
2018-07-11 15:36:27 +00:00
using EntityFrameworkCore.BootKit;
2018-06-13 22:17:35 +00:00
using Microsoft.Extensions.Configuration;
2018-06-14 15:03:01 +00:00
using Newtonsoft.Json.Linq;
2018-06-13 22:17:35 +00:00
using RestSharp;
using System;
using System.Collections.Generic;
2018-07-11 15:36:27 +00:00
using System.Linq;
2018-06-13 22:17:35 +00:00
using System.Text;
2018-08-08 21:39:00 +00:00
using System.Threading.Tasks;
2018-06-13 22:17:35 +00:00
namespace BotSharp.Core.Engines.SpaCy
{
2018-06-14 15:03:01 +00:00
public class SpaCyTokenizer : INlpPipeline
2018-06-13 22:17:35 +00:00
{
public IConfiguration Configuration { get; set; }
public PipeSettings Settings { get; set; }
2018-06-13 22:17:35 +00:00
2018-08-08 21:39:00 +00:00
public async Task<bool> Train(Agent agent, JObject data, PipeModel meta)
2018-06-13 22:17:35 +00:00
{
var client = new RestClient(Configuration.GetSection("SpaCyProvider:Url").Value);
var request = new RestRequest("tokenizer", Method.GET);
2018-07-11 15:36:27 +00:00
List<List<NlpToken>> tokens = new List<List<NlpToken>>();
Boolean res = true;
var dc = new DefaultDataContextLoader().GetDefaultDc();
var corpus = agent.Corpus;
2018-06-13 22:17:35 +00:00
2018-07-11 15:36:27 +00:00
corpus.UserSays.ForEach(usersay => {
Console.WriteLine(usersay.Text);
2018-07-11 15:36:27 +00:00
request.AddParameter("text", usersay.Text);
var response = client.Execute<Result>(request);
2018-07-11 15:36:27 +00:00
tokens.Add(response.Data.Tokens);
2018-07-11 15:36:27 +00:00
res = res && response.IsSuccessful;
});
2018-06-14 15:03:01 +00:00
2018-07-11 15:36:27 +00:00
data.Add("Tokens", JToken.FromObject(tokens));
return res;
2018-06-13 22:17:35 +00:00
}
2018-08-08 21:39:00 +00:00
public async Task<bool> Predict(Agent agent, JObject data, PipeModel meta)
{
return true;
}
private class Result
2018-06-13 22:17:35 +00:00
{
public List<NlpToken> Tokens { get; set; }
}
}
}