11using 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 ;
210using System ;
311using System . Collections . Generic ;
12+ using System . Linq ;
413using System . Text ;
14+ using System . Threading . Tasks ;
515
616namespace 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}
0 commit comments