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

92 lines
2.9 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;
using BotSharp.NLP.Tokenize;
2018-07-11 15:36:27 +00:00
using EntityFrameworkCore.BootKit;
2018-06-13 22:17:35 +00:00
using Microsoft.Extensions.Configuration;
using Newtonsoft.Json;
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-09 21:06:40 +00:00
using System.Threading.Tasks;
2018-06-13 22:17:35 +00:00
namespace BotSharp.Core.Engines.SpaCy
{
public class SpaCyTokenizer : INlpTrain, INlpPredict
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
public async Task<bool> Train(Agent agent, NlpDoc doc, 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.POST);
List<List<Token>> tokens = new List<List<Token>>();
2018-07-11 15:36:27 +00:00
Boolean res = true;
var corpus = agent.Corpus;
2018-06-13 22:17:35 +00:00
doc.Sentences = new List<NlpDocSentence>();
List<string> sentencesList = new List<string>();
corpus.UserSays.ForEach(usersay => sentencesList.Add(usersay.Text));
request.RequestFormat = DataFormat.Json;
request.Method = Method.POST;
request.AddHeader("Content-Type", "application/json; charset=utf-8");
request.AddParameter("application/json", JsonConvert.SerializeObject(new Documents(sentencesList)), ParameterType.RequestBody);
var response = client.Execute<Result>(request);
tokens = response.Data.TokensList;
2018-08-09 21:06:40 +00:00
for (int i = 0; i < sentencesList.Count; i++)
{
doc.Sentences.Add(new NlpDocSentence
{
Tokens = tokens[i],
Text = sentencesList[i]
});
}
res = res && response.IsSuccessful;
2018-07-11 15:36:27 +00:00
return res;
2018-06-13 22:17:35 +00:00
}
public async Task<bool> Predict(Agent agent, NlpDoc doc, PipeModel meta)
2018-08-09 21:06:40 +00:00
{
var client = new RestClient(Configuration.GetSection("SpaCyProvider:Url").Value);
var request = new RestRequest("tokenizer", Method.GET);
List<List<Token>> tokens = new List<List<Token>>();
Boolean res = true;
var corpus = agent.Corpus;
request.AddParameter("text", doc.Sentences[0].Text);
var response = client.Execute<Result>(request);
tokens = response.Data.TokensList;
res = res && response.IsSuccessful;
doc.Sentences[0].Tokens = tokens[0];
2018-08-09 21:06:40 +00:00
return true;
}
private class Result
2018-06-13 22:17:35 +00:00
{
public List<List<Token>> TokensList { get; set; }
}
private class Documents
{
public List<string> Sentences { get; set; }
public Documents(List<string> sentences)
{
this.Sentences = sentences;
}
2018-06-13 22:17:35 +00:00
}
}
}