using Accord.Statistics.Models.Markov;
using Accord.Statistics.Models.Markov.Learning;
using Accord.Statistics.Models.Markov.Topology;
using System.Collections.Generic;
enum Tags { 家電愛好者, 愛漂亮的人};
static string [] objs = { "耳機", "行動電源","掃地機", "面膜", "防曬乳","保濕乳", "未知"};
public static void Main()
Log.Logger = new LoggerConfiguration()
string [][] inputs = new string [][]
new string[] { "耳機","行動電源","行動電源", "行動電源", "耳機"},
new string[] { "耳機","耳機","行動電源", "行動電源", "耳機", "耳機"},
new string[] { "耳機","行動電源","行動電源", "行動電源", "行動電源", "耳機"},
new string[] { "面膜","面膜","面膜","面膜", "保濕乳"},
new string[] { "面膜", "面膜","保濕乳","面膜","面膜","面膜"},
new string[] { "面膜", "面膜","保濕乳","面膜","面膜","面膜"},
new string[] { "面膜", "保濕乳","面膜","面膜","面膜","面膜"},
int [] inputlabel = new int[]
ITopology forward = new Forward(states: 3);
HiddenMarkovClassifier classifier = new HiddenMarkovClassifier(classes: 2, topology: forward, symbols: objs.Count());
var teacher = new HiddenMarkovClassifierLearning(classifier,
modelIndex => new BaumWelchLearning(classifier.Models[modelIndex]) {
double likelihood = teacher.Run(inputs.ToCode(objs), inputlabel);
int user1 = classifier.Compute( (new[] { "行動電源","掃地機" ,"耳機", "防曬乳" ,"面膜", "耳機"}).ToCode(objs));
Log.Information("第一位使用者是:{user1}", (Tags)user1);
int user2 = classifier.Compute( (new[] {"面膜","保濕乳" ,"面膜","面膜","面膜", "保濕乳" ,"面膜"}).ToCode(objs));
Log.Information("第二位使用者是:{user2}", (Tags)user2);
public static class Helper
public static int[][] ToCode(this string [][] input, string[] tags)
return input.Select(arr => arr.Select( obj => tags.IndexOf(obj) ).ToArray() ).ToArray();
public static int[] ToCode(this string [] input, string[] tags)
return input.Select( obj => tags.IndexOf(obj) ).ToArray();