Skip to content

Commit e1fb0d8

Browse files
committed
Added WitAiEntityRecognizer pipe.
1 parent bd91091 commit e1fb0d8

6 files changed

Lines changed: 103 additions & 10 deletions

File tree

BotSharp.Core/Engines/NERs/WitAiEntityRecognizer.cs

Lines changed: 78 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,21 @@
11
using BotSharp.Core.Abstractions;
2+
using BotSharp.Core.Agents;
3+
using BotSharp.MachineLearning.NLP;
4+
using DotNetToolkit;
5+
using Microsoft.Extensions.Configuration;
6+
using Newtonsoft.Json;
7+
using Newtonsoft.Json.Linq;
8+
using Newtonsoft.Json.Serialization;
9+
using RestSharp;
210
using System;
311
using System.Collections.Generic;
12+
using System.Linq;
413
using System.Text;
14+
using System.Threading.Tasks;
515

616
namespace BotSharp.Core.Engines.NERs
717
{
8-
public class WitAiEntityRecognizer : INlpNer
18+
public class WitAiEntityRecognizer : INlpPipeline, INlpNer
919
{
1020
public List<OntologyEnum> Ontologies
1121
{
@@ -18,5 +28,72 @@ public List<OntologyEnum> Ontologies
1828
};
1929
}
2030
}
31+
32+
public IConfiguration Configuration { get; set; }
33+
public PipeSettings Settings { get; set; }
34+
35+
public async Task<bool> Predict(Agent agent, NlpDoc doc, PipeModel meta)
36+
{
37+
var config = (IConfiguration)AppDomain.CurrentDomain.GetData("Configuration");
38+
var client = new RestClient($"{config.GetSection("WitAi:url").Value}");
39+
var request = new RestRequest(config.GetSection("WitAi:resource").Value, Method.GET);
40+
request.AddHeader("Authorization", "Bearer " + config.GetSection("WitAi:serverAccessToken").Value);
41+
request.AddQueryParameter("v", config.GetSection("WitAi:version").Value);
42+
request.AddQueryParameter("q", doc.Sentences[0].Text);
43+
request.AddQueryParameter("verbose", "true");
44+
request.AddQueryParameter("autosuggest", "true");
45+
46+
var result = client.Execute<WitAiResponse>(request);
47+
48+
var entities = result.Data.Entities[0];
49+
if(entities.Datetime != null)
50+
{
51+
doc.Sentences[0].Entities.AddRange(entities.Datetime.Select(x => Map(x)));
52+
}
53+
54+
if(entities.Location != null)
55+
{
56+
doc.Sentences[0].Entities.AddRange(entities.Location.Select(x => Map(x)));
57+
}
58+
59+
return true;
60+
}
61+
62+
private NlpEntity Map(WitAiEntity entity)
63+
{
64+
return new NlpEntity
65+
{
66+
Confidence = entity.Confidence,
67+
Start = entity.Start,
68+
Value = entity.Value,
69+
Entity = entity.Entity
70+
};
71+
}
72+
73+
public async Task<bool> Train(Agent agent, NlpDoc doc, PipeModel meta)
74+
{
75+
return true;
76+
}
77+
78+
private class WitAiResponse
79+
{
80+
public List<WitAiEntityResponse> Entities { get; set; }
81+
}
82+
83+
private class WitAiEntityResponse
84+
{
85+
public List<WitAiEntity> Location { get; set; }
86+
public List<WitAiEntity> Datetime { get; set; }
87+
}
88+
89+
private class WitAiEntity
90+
{
91+
[JsonProperty("_entity")]
92+
public string Entity { get; set; }
93+
[JsonProperty("_start")]
94+
public int Start { get; set; }
95+
public string Value { get; set; }
96+
public decimal Confidence { get; set; }
97+
}
2198
}
2299
}

BotSharp.Core/Engines/Rasa/RasaAi.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,7 @@ public AIResponse TextRequest(AIRequest request)
8484
private IRestResponse<RasaResponse> CallRasa(string projectId, string text, string model)
8585
{
8686
var config = (IConfiguration)AppDomain.CurrentDomain.GetData("Configuration");
87-
var client = new RestClient($"{config.GetSection("Rasa:Nlu").Value}");
87+
var client = new RestClient($"{config.GetSection("RasaNlu:url").Value}");
8888

8989
var rest = new RestRequest("parse", Method.POST);
9090
string json = JsonConvert.SerializeObject(new { Project = projectId, Q = text, Model = model },
@@ -107,7 +107,7 @@ public void Train()
107107

108108
var corpus = GetIntentExpressions();
109109
var config = (IConfiguration)AppDomain.CurrentDomain.GetData("Configuration");
110-
var client = new RestClient($"{config.GetSection("Rasa:Nlu").Value}");
110+
var client = new RestClient($"{config.GetSection("RasaNlu:url").Value}");
111111

112112
var contextHashs = corpus.UserSays
113113
.Select(x => x.ContextHash)

BotSharp.Core/Engines/SpaCy/SpaCyProvider.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ public async Task<bool> Train(Agent agent, NlpDoc doc, PipeModel meta)
2424
var response = client.Execute<Result>(request);
2525

2626
meta.Meta = JObject.FromObject(response.Data);
27-
meta.Meta["models"] = null;
27+
meta.Meta.Remove("models");
2828
meta.Model = response.Data.Models;
2929

3030
return response.IsSuccessful;

BotSharp.MachineLearning/SpaCy/server.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,10 @@
99
nlp = spacy.load('en')
1010
ner = EntityRecognizer(nlp.vocab)
1111

12+
# python -m spacy info
1213
@route('/load')
1314
def load():
14-
pass
15+
return {'version': '2.0.11', 'models': 'en_core_web_md, en', 'python': '3.5.2'}
1516

1617
@route('/tokenizer')
1718
def tokenize():
@@ -201,4 +202,4 @@ def entityrecognizerpredict():
201202

202203

203204

204-
run(host='0.0.0.0', port=5005, debug=True)
205+
run(host='0.0.0.0', port=5005, debug=False)

BotSharp.RestApi/AgentController.cs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,14 @@ public AgentController(IBotPlatform platform)
3232
_platform = platform;
3333
}
3434

35+
[HttpGet]
36+
public ActionResult<List<Agent>> AllAgents()
37+
{
38+
var dc = new DefaultDataContextLoader().GetDefaultDc();
39+
40+
return dc.Table<Agent>().ToList();
41+
}
42+
3543
/// <summary>
3644
/// Restore a agent from a uploaded zip file
3745
/// </summary>

BotSharp.WebHost/Settings/bot.json

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,22 @@
11
{
2-
"Rasa": {
3-
"Nlu": "http://localhost:5000"
2+
"RasaNlu": {
3+
"url": "http://localhost:5000"
4+
},
5+
6+
"WitAi": {
7+
"url": "https://api.wit.ai",
8+
"resource": "message",
9+
"serverAccessToken": "YLLK6SFAPXNYGMKAWLOREQ5MJQSX345L",
10+
"version": "20180811"
411
},
512

613
"BotSharpAi": {
714
"Lang": "en",
815
"Provider": "SpaCyProvider",
916
"SpaCyProvider": {
10-
"Url": "http://10.2.21.200:5005"
17+
"Url": "http://localhost:5005"
1118
},
12-
"Pipe": "SpaCyTokenizer, CRFsuiteEntityRecognizer, FasttextClassifier",
19+
"Pipe": "SpaCyTokenizer, CRFsuiteEntityRecognizer, WitAiEntityRecognizer, FasttextClassifier",
1320
"CRFsuiteEntityRecognizer": {
1421
"fields": "y w pos chk",
1522
"uniFeatures": "w wl pos chk shape shaped type p1 p2 p3 p4 s1 s2 s3 s4 2d 4d d&a d&- d&/ d&, d&. up iu au al ad ao cu cl ca cd cs",

0 commit comments

Comments
 (0)