Skip to content

add removing temp file func in Predict() API #4

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 8 commits into from
Aug 10, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions BotSharp.Core/Engines/BotPreditor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -43,9 +43,15 @@ public async Task<string> Predict(Agent agent, AIRequest request)
var settings = new PipeSettings
{
ModelDir = Path.Join(AppDomain.CurrentDomain.GetData("DataPath").ToString(), "ModelFiles", agent.Id),
PredictDir = Path.Join(AppDomain.CurrentDomain.GetData("DataPath").ToString(), "Predicts"),
AlgorithmDir = Path.Join(AppDomain.CurrentDomain.GetData("ContentRootPath").ToString(), "Algorithms")
};

if (!Directory.Exists(settings.PredictDir))
{
Directory.CreateDirectory(settings.PredictDir);
}

// pipe process
meta.Pipeline.ForEach(async pipeMeta =>
{
Expand Down
66 changes: 65 additions & 1 deletion BotSharp.Core/Engines/CRFsuite/CRFsuiteEntityRecognizer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,9 @@ public async Task<bool> Train(Agent agent, JObject data, PipeModel meta)
new MachineLearning.CRFsuite.Ner()
.NerStart(rawTrainingDataFileName, parsedTrainingDataFileName, fields, uniFeatures.Split(" "), biFeatures.Split(" "));

CmdHelper.Run(Path.Join(Settings.AlgorithmDir, "crfsuite"), $"learn -m {modelFileName} {parsedTrainingDataFileName}"); // --split=3 -x
var algorithmDir = Path.Join(AppDomain.CurrentDomain.GetData("ContentRootPath").ToString(), "Algorithms");

CmdHelper.Run(Path.Join(algorithmDir, "crfsuite"), $"learn -m {modelFileName} {parsedTrainingDataFileName}"); // --split=3 -x

Console.WriteLine($"Saved model to {modelFileName}");
meta.Meta = new JObject();
Expand Down Expand Up @@ -134,6 +136,68 @@ public List<TrainingData> Merge(List<NlpToken> tokens, List<TrainingIntentExpres

public async Task<bool> Predict(Agent agent, JObject data, PipeModel meta)
{
List<List<NlpToken>> tokens = data["Tokens"].ToObject<List<List<NlpToken>>>();
var uniFeatures = meta.Meta["uniFeatures"].ToString();
var biFeatures = meta.Meta["biFeatures"].ToString();
string field = meta.Meta["fields"].ToString();
string[] fields = field.Split(" ");


string rawPredictingDataFileName = Path.Join(Settings.PredictDir, "ner-crf.corpus.predict.txt");
string parsedPredictingDataFileName = Path.Join(Settings.PredictDir, "ner-crf.parsed.predict.txt");
string modelFileName = Path.Join(Settings.ModelDir, meta.Model);

using (FileStream fs = new FileStream(rawPredictingDataFileName, FileMode.Create))
{
using (StreamWriter sw = new StreamWriter(fs))
{
List<string> curLine = new List<string>();
foreach (List<NlpToken> tokenList in tokens)
{
foreach (NlpToken token in tokenList)
{
for (int i = 0 ; i < fields.Length; i++)
{
if (fields[i] == "y") {
curLine.Add("");
}
else if (fields[i] == "w") {
curLine.Add(token.Text);
}
else if (fields[i] == "pos") {
curLine.Add(token.Tag);
}
else if (fields[i] == "chk") {
curLine.Add("");
}
}
sw.Write(string.Join(" ", curLine) + "\n");
curLine.Clear();
}
sw.Write("\n");

}
sw.Flush();
}
}
new MachineLearning.CRFsuite.Ner()
.NerStart(rawPredictingDataFileName, parsedPredictingDataFileName, field, uniFeatures.Split(" "), biFeatures.Split(" "));

var output = CmdHelper.Run(Path.Join(Settings.AlgorithmDir, "crfsuite"), $"tag -i -m {modelFileName} {parsedPredictingDataFileName}");

var entities = new List<NlpEntity>();
//


data["entities"] = JObject.FromObject(entities);
if(File.Exists(rawPredictingDataFileName))
{
File.Delete(rawPredictingDataFileName);
}
if(File.Exists(parsedPredictingDataFileName))
{
File.Delete(parsedPredictingDataFileName);
}
return true;
}
}
Expand Down
1 change: 1 addition & 0 deletions BotSharp.Core/Engines/Classifiers/FasttextClassifier.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ namespace BotSharp.Core.Engines.Classifiers
public class FasttextClassifier : INlpPipeline
{
public IConfiguration Configuration { get; set; }

public PipeSettings Settings { get; set; }

public async Task<bool> Predict(Agent agent, JObject data, PipeModel meta)
Expand Down
1 change: 1 addition & 0 deletions BotSharp.Core/Engines/PipeSettings.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,5 +9,6 @@ public class PipeSettings
public string TrainDir { get; set; }
public string ModelDir { get; set; }
public string AlgorithmDir { get; set; }
public string PredictDir { get; set; }
}
}
16 changes: 16 additions & 0 deletions BotSharp.Core/Engines/SpaCy/SpaCyTokenizer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,22 @@ public async Task<bool> Train(Agent agent, JObject data, PipeModel meta)

public async Task<bool> Predict(Agent agent, JObject data, PipeModel meta)
{
var client = new RestClient(Configuration.GetSection("SpaCyProvider:Url").Value);
var request = new RestRequest("tokenizer", Method.GET);
List<List<NlpToken>> tokens = new List<List<NlpToken>>();
Boolean res = true;
var corpus = agent.Corpus;

request.AddParameter("text", data["Text"]);
var response = client.Execute<Result>(request);

tokens.Add(response.Data.Tokens);

res = res && response.IsSuccessful;


data.Add("Tokens", JToken.FromObject(tokens));

return true;
}

Expand Down
2 changes: 1 addition & 1 deletion BotSharp.RestApi/Dialogs/DialogController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ public ActionResult<AIResponse> Query([FromBody] QueryModel request)
var aIResponse = _platform.TextRequest(new AIRequest
{
Timezone = request.Timezone,
Contexts = request.Contexts.Select(x => new AIContext { Name = x }).ToList(),
Contexts = request?.Contexts?.Select(x => new AIContext { Name = x })?.ToList(),
Language = request.Lang,
Query = new String[] { request.Query }
});
Expand Down
Binary file added BotSharp.WebHost/Algorithms/crfsuite
Binary file not shown.
10 changes: 10 additions & 0 deletions BotSharp.WebHost/App_Data/Predicts/ner-crf.corpus.predict.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
when WRB
is VBZ
the DT
next JJ
train NN
in IN
muncher NN
freiheit NN
? .

Loading