Repository navigation
Expand file tree
/
Copy pathBotSharpTokenizer.cs
More file actions
76 lines (62 loc) · 2.03 KB
/
Copy pathBotSharpTokenizer.cs
File metadata and controls
76 lines (62 loc) · 2.03 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
using BotSharp.Core.Abstractions;
using CherubNLP;
using CherubNLP.Tokenize;
using BotSharp.Platform.Models;
using BotSharp.Platform.Models.AiResponse;
using BotSharp.Platform.Models.MachineLearning;
using Microsoft.Extensions.Configuration;
using System;
using System.Collections.Generic;
using System.Text;
using System.Threading.Tasks;
namespace BotSharp.Core.Engines.BotSharp
{
public class BotSharpTokenizer : INlpTrain, INlpPredict
{
public IConfiguration Configuration { get; set; }
public PipeSettings Settings { get; set; }
private TokenizerFactory _tokenizer;
public BotSharpTokenizer()
{
}
public async Task<bool> Predict(AgentBase agent, NlpDoc doc, PipeModel meta)
{
Init();
doc.Tokenizer = this;
// same as train
doc.Sentences.ForEach(snt =>
{
snt.Tokens = _tokenizer.Tokenize(snt.Text);
});
return true;
}
public async Task<bool> Train(AgentBase agent, NlpDoc doc, PipeModel meta)
{
Init();
doc.Tokenizer = this;
doc.Sentences = new List<NlpDocSentence>();
agent.Corpus.UserSays.ForEach(say =>
{
doc.Sentences.Add(new NlpDocSentence
{
Tokens = _tokenizer.Tokenize(say.Text),
Text = say.Text,
Intent = new TextClassificationResult { Label = say.Intent }
});
});
return true;
}
private void Init()
{
if(_tokenizer == null)
{
_tokenizer = new TokenizerFactory(new TokenizationOptions
{
Pattern = Configuration.GetValue<String>("options:pattern")
}, SupportedLanguage.English);
string tokenizerName = Configuration.GetValue<String>($"tokenizer");
_tokenizer.GetTokenizer(tokenizerName);
}
}
}
}