Skip to content

Instantly share code, notes, and snippets.

@ksasao
Last active May 31, 2018 12:46
Show Gist options
  • Save ksasao/16fb0c45c13e182a55f434aa3fd377f5 to your computer and use it in GitHub Desktop.
Save ksasao/16fb0c45c13e182a55f434aa3fd377f5 to your computer and use it in GitHub Desktop.
ML.NETで誰が言ったセリフかを推定する
// ML.NETで誰が言ったセリフかを推定する
//
// ML.NETのセットアップ方法は下記を参照
// https://www.microsoft.com/net/learn/apps/machine-learning-and-ai/ml-dotnet/get-started/windows
using Microsoft.ML;
using Microsoft.ML.Models;
using Microsoft.ML.Runtime.Api;
using Microsoft.ML.Trainers;
using Microsoft.ML.Transforms;
using System;
namespace whoistalking
{
class Script
{
[Column(ordinal: "0")]
public string Character;
[Column(ordinal: "1")]
public string Line;
}
public class ScriptPrediction
{
[ColumnName("PredictedLabel")]
public string Character;
}
class Program
{
static void Main(string[] args)
{
// 話者名\t台詞 を1行としたtsvファイルを用意する
var data = new TextLoader<Script>(@"zero.tsv.data.tsv", useHeader: false, separator: "tab");
var testData = new TextLoader<Script>(@"zero.tsv.test.tsv", useHeader: false, separator: "tab");
// LearningPipelineの作成
var pipeline = new LearningPipeline();
pipeline.Add(data);
pipeline.Add(new Dictionarizer(("Character", "Label")));
pipeline.Add(new TextFeaturizer("Line", "Line"));
pipeline.Add(new ColumnConcatenator("Features", "Line"));
pipeline.Add(new StochasticDualCoordinateAscentClassifier());
pipeline.Add(new PredictedLabelColumnOriginalValueConverter() { PredictedLabelColumn = "PredictedLabel" });
// 学習
PredictionModel<Script, ScriptPrediction> model = pipeline.Train<Script, ScriptPrediction>();
// テスト用データによる評価
var evaluator = new ClassificationEvaluator();
ClassificationMetrics metrics = evaluator.Evaluate(model, testData);
Console.WriteLine("Micro-Accuracy: {0}", metrics.AccuracyMicro);
// キーボードからの入力
while (true)
{
Console.Write("台詞: ");
string test = Console.ReadLine();
Script issue = new Script
{
Character = "",
Line = test
};
ScriptPrediction prediction = model.Predict(issue);
Console.WriteLine("推定話者: " + prediction.Character);
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment