Skip to content

Commit 5780511

Browse files
authored
Merge pull request #122 from evan-cao-wb/Add-confidence-threshold
Add confidence threshold
2 parents 1ba2e26 + db61b8b commit 5780511

File tree

4 files changed

+16
-5
lines changed

4 files changed

+16
-5
lines changed

src/Plugins/BotSharp.Plugin.RoutingSpeeder/Providers/IntentClassifier.cs

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -196,7 +196,10 @@ public string[] GetFiles()
196196

197197
public string[] GetLabels()
198198
{
199-
return GetFiles().Select(x => Path.GetFileNameWithoutExtension(x)).ToArray();
199+
var agentService = _services.CreateScope().ServiceProvider.GetRequiredService<IAgentService>();
200+
string rootDirectory = Path.Combine(agentService.GetDataDir(), _settings.RAW_DATA_DIR, _settings.LABEL_FILE_NAME);
201+
var labelText = File.ReadAllLines(rootDirectory);
202+
return labelText.OrderBy(x => x).ToArray();
200203
}
201204

202205
public string TextClean(string text)
@@ -210,19 +213,23 @@ public string TextClean(string text)
210213
return processedText;
211214
}
212215

213-
public string Predict(NDArray vector)
216+
public string Predict(NDArray vector, float confidenceScore = 0.9f)
214217
{
215218
if (!_isModelReady)
216219
{
217220
InitClassifer();
218221
}
219222

220-
var prob = _model.predict(vector);
223+
var prob = _model.predict(vector).numpy();
224+
225+
if (prob[0] < confidenceScore)
226+
{
227+
return string.Empty;
228+
}
229+
221230
var probLabel = tf.arg_max(prob, -1).numpy();
222-
// var prediction = _settings.LabelMappingDict.First(x => x.Value == probLabel[0]).Key;
223231

224232
var prediction = GetLabels()[probLabel[0]];
225-
// var prediction = GetLabels().Where((x, i) => i == probLabel[0]).First();
226233

227234
return prediction;
228235
}

src/Plugins/BotSharp.Plugin.RoutingSpeeder/Settings/classifierSetting.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,4 +15,5 @@ public class ClassifierSetting
1515

1616
public string RAW_DATA_DIR { get; set; } = "raw_data";
1717
public string MODEL_DIR { get; set; } = "models";
18+
public string LABEL_FILE_NAME { get; set; } = "label.txt";
1819
}
Binary file not shown.
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
goodbye
2+
greeting
3+
other

0 commit comments

Comments
 (0)