public SpellerModel(string modelPath, ISegmenter segmenter) { this._segmenter = segmenter; var unigramFilePath = Path.Combine(modelPath, Constants.UnigramFileName); var bigramFilePath = Path.Combine(modelPath, Constants.BigramFileName); LoadModel(unigramFilePath, bigramFilePath); }
/// <summary> /// 加载子分词器 /// </summary> /// <returns></returns> public IList <ISegmenter> LoadSegmenters() { IList <ISegmenter> segmenters = new List <ISegmenter>(); // 通过反射加载所有实现的Segmenter子分词器 foreach (var type in typeof(IKAnalyzer).Assembly.GetTypes()) { if (type.GetInterfaces().Any(t => t == typeof(ISegmenter))) { ConstructorInfo ci = type.GetConstructor(Type.EmptyTypes); ISegmenter segmenter = ci.Invoke(null) as ISegmenter; segmenters.Add(segmenter); } } return(segmenters); }
public ChineseNormalier(string stopwordsFilePath, string spellerModelPath) { _segmenter = new JBSegmenter(); _spellerModel = new SpellerModel(spellerModelPath, _segmenter); var stopWordsSet = new HashSet <string>(); using (var streamReader = new StreamReader(stopwordsFilePath)) { string line = null; while ((line = streamReader.ReadLine()) != null) { stopWordsSet.Add(line.Trim()); } } _stopwords = stopWordsSet; }
private void AddToSegmenter(ISegmenter segmenter, IList <MiddlewareComponent> components, MiddlewareComponent middlewareComponent) { IList <List <string> > nodeDependencies = new List <List <string> >(); IList <string> routeDependencies = new List <string>(); foreach (var dependency in middlewareComponent.Middleware.Dependencies) { if (string.IsNullOrEmpty(dependency.Name)) { if (dependency.DependentType == typeof(IRoute)) { throw new BuilderException( "Route dependencies must specify the name of the route." + " Middleware type " + middlewareComponent.Middleware.GetType().Name + "<" + middlewareComponent.MiddlewareType.Name + "> has a dependency on an unnamed route."); } var dependantNames = components .Where(c => c.MiddlewareType == dependency.DependentType) .Select(c => c.Name) .ToList(); nodeDependencies.Add(dependantNames); } else { if (dependency.DependentType == typeof(IRoute)) { routeDependencies.Add(dependency.Name); } else { var dependantNames = new List <string> { dependency.Name }; if (!dependency.Required) { dependantNames.Add(null); } nodeDependencies.Add(dependantNames); } } } segmenter.AddNode(middlewareComponent.Name, nodeDependencies, routeDependencies); }
/// <summary> /// 释放子分词器对_segmentBuffer的占用 /// </summary> /// <param name="segmenter"></param> public void UnlockBuffer(ISegmenter segmenter) { _bufferLocker.Remove(segmenter); }
/// <summary> /// 设置当前_segmentBuffer为锁定状态 /// 加入正在处理的子分词器,表示此子分词器正在占用_segmentBuffer /// </summary> /// <param name="segmenter"></param> public void LockBuffer(ISegmenter segmenter) { _bufferLocker.Add(segmenter); }
public void SetUp() { _segmenter = new Segmenter(new DependencyGraphFactory()); }
/// <summary> /// 释放子分词器对_segmentBuffer的占用 /// </summary> /// <param name="segmenter"></param> public void UnlockBuffer(ISegmenter segmenter) { _bufferLocker.Remove(segmenter); }
/// <summary> /// 设置当前_segmentBuffer为锁定状态 /// 加入正在处理的子分词器,表示此子分词器正在占用_segmentBuffer /// </summary> /// <param name="segmenter"></param> public void LockBuffer(ISegmenter segmenter) { _bufferLocker.Add(segmenter); }
public void LockBuffer(ISegmenter segmenter) { this.buffLocker.Add(segmenter); }
public void UnlockBuffer(ISegmenter segmenter) { this.buffLocker.Remove(segmenter); }
internal async Task CreateReader(PipelineBuildDefinition def, ISegmenter segmenter) { var context = new { pipeline = def.Pipe.Reader, onError = def.OnReaderError ?? def.OnError, cancellationToken = def.CancellationTokenSource.Token, owner = segmenter, }; var completed = false; while (!context.cancellationToken.IsCancellationRequested) { var result = await context.pipeline.ReadAsync(context.cancellationToken); try { var buffer = result.Buffer; while (!context.cancellationToken.IsCancellationRequested) { var read = await segmenter.TryReadAsync(buffer); buffer = read.RemainingData; if (read.Status == SegmentationStatus.Incomplete) { break; } else if (read.Status == SegmentationStatus.Invalid) { throw new InvalidSegmentationException(); } } // Tell the PipeReader how much of the buffer we have consumed context.pipeline.AdvanceTo(buffer.Start, buffer.End); } catch (Exception ex) { var errorHandling = await context.onError.Handle(context.owner, ex); switch (errorHandling) { case ErrorHandling.Ignore: //Note: do nothing break; case ErrorHandling.Stop: completed = true; break; default: case ErrorHandling.Throw: context.pipeline.Complete(ex); return; } } // Stop reading if there's no more data coming if (result.IsCompleted || completed) { break; } } //Mark the PipeReader as complete context.pipeline.Complete(); }
public void Run(string[] args) { Command c = Recognizer.Command.Help; if (args.Length > 0) { c = (Command)Enum.Parse(typeof(Recognizer.Command), args[0]); } if (c == Recognizer.Command.Help) { Console.WriteLine("Usage:\n\tFinalProject.exe command [filename]\n\nCommands:"); foreach (var cm in Enum.GetNames(typeof(Recognizer.Command))) { Console.WriteLine(cm); } return; } // For CV mCV = new CrossValidation("cvindex.txt"); //////////////////// // CHANGE THESE LINES TO SWAP MODELS //////////////////// mRec = new LogisticRegressionRecognizer(); mSeg = new DumbSegmenter(); Console.WriteLine("Using recognizer {0}", mRec.GetType().ToString()); Console.WriteLine("Using segmenter {0}", mSeg.GetType().ToString()); mRecFilename = "models/" + mRec.GetType().ToString() + ".rec.model"; mSegFilename = "models/" + mSeg.GetType().ToString() + ".seg.model"; mSeg.GestureSegmented += delegate(object sender, EventArgs e) { var segm = ((ISegmenter)sender).LastGesture; var recres = mRec.RecognizeSingleGesture(segm); if (recres.Confidence1 > 0.5f) { Console.WriteLine("Recognized gesture: {0}", recres.Gesture1); } else { Console.WriteLine("Inconclusive"); } }; string filename = "gestures/track_high_kick_01.log"; if (args.Length > 1) { filename = args[1]; } InputGesture gest; Thread visthread; switch (c) { case Command.Train: Train(); break; case Command.TestSingle: LoadModels(); var result = mRec.RecognizeSingleGesture(new InputGesture(new LogFileLoader(filename))); Console.WriteLine(result.ToString()); break; case Command.TestRecognize: LoadModels(); var test_gestures = LoadData(mGestureNames, false); int[] total = Enumerable.Range(0, test_gestures.Count).Select(x => 0).ToArray(), correct = Enumerable.Range(0, test_gestures.Count).Select(x => 0).ToArray(); int i = 0; Console.WriteLine(); foreach (var gn in test_gestures) { foreach (var tg in gn.Value) { total[i]++; var result2 = mRec.RecognizeSingleGesture(tg); if (result2.Gesture1 == gn.Key) { correct[i]++; } } Console.WriteLine("{0}: {1} correct / {2} total = {3}% correct", gn.Key, correct[i], total[i], (float)correct[i] / (float)total[i] * 100.0f); i++; } Console.WriteLine("TEST RESULTS:\n\t{0} correct / {1} total = {2}% correct", correct.Sum(), total.Sum(), (float)correct.Sum() / (float)total.Sum() * 100.0f); Utility.PrintMemoryUsage(); break; case Command.TestRealtime: LoadModels(); gest = new InputGesture(new LogFileLoader(filename)); visthread = new Thread(new ThreadStart(this.StartVisualize)); visthread.Start(); while (true) // Wait for window to get created { lock (this) { if (mVisWindow != null) { break; } } } float lastTime = gest.States[0].Timestamp; foreach (var frame in gest.States) { mSeg.AddState(frame); mVisWindow.CurrState = frame; Thread.Sleep((int)((frame.Timestamp - lastTime) * 750.0f)); lastTime = frame.Timestamp; } mSeg.Finish(); visthread.Abort(); break; case Command.RunRealtime: LoadModels(); visthread = new Thread(new ThreadStart(this.StartVisualize)); visthread.Start(); while (true) // Wait for window to get created { lock (this) { if (mVisWindow != null) { break; } } } var nfl = new NetworkFrameListener(4711); nfl.Start(); Console.WriteLine("Press the 'x' key to quit"); while (true) { var frame = nfl.GetState(); if (frame != null) { var js = JointState.FromRawJointState(frame); mSeg.AddState(js); mVisWindow.CurrState = js; } if (Console.KeyAvailable) { var cki = Console.ReadKey(true); if (cki.Key == ConsoleKey.X) { break; } } if (!nfl.Running) { break; } Thread.Sleep(0); } mSeg.Finish(); nfl.Stop(); visthread.Abort(); break; case Command.PrintFeatures: gest = new InputGesture(new LogFileLoader(filename)); /*foreach ( var f in Features.AllFeatures.SingleGestureFeatures ) { * Console.WriteLine("{0}: {1}", f.ToString(), f.QueryGesture(gest)); * }*/ foreach (var f in Features.AllFeatures.GestureFeatures) { Console.WriteLine("{0}: {1}", f.ToString(), f.QueryGesture(gest)); } break; case Command.BenchmarkRecognize: Benchmarks.BenchmarkRecognize(); break; case Command.CycleCV: mCV.Incr(); mCV.Save(); break; } }
public Trainer(ISegmenter segmenter, string inputFolder, string outputFolder) { _segmenter = segmenter; this.OutputDataFolder = outputFolder; this.InputDataFolder = inputFolder; }
public void Run(string[] args) { Command c = Recognizer.Command.Help; if ( args.Length > 0 ) { c = (Command)Enum.Parse(typeof(Recognizer.Command), args[0]); } if ( c == Recognizer.Command.Help ) { Console.WriteLine("Usage:\n\tFinalProject.exe command [filename]\n\nCommands:"); foreach ( var cm in Enum.GetNames(typeof(Recognizer.Command)) ) Console.WriteLine(cm); return; } // For CV mCV = new CrossValidation("cvindex.txt"); //////////////////// // CHANGE THESE LINES TO SWAP MODELS //////////////////// mRec = new LogisticRegressionRecognizer(); mSeg = new DumbSegmenter(); Console.WriteLine("Using recognizer {0}", mRec.GetType().ToString()); Console.WriteLine("Using segmenter {0}", mSeg.GetType().ToString()); mRecFilename = "models/" + mRec.GetType().ToString() + ".rec.model"; mSegFilename = "models/" + mSeg.GetType().ToString() + ".seg.model"; mSeg.GestureSegmented += delegate(object sender, EventArgs e) { var segm = ((ISegmenter)sender).LastGesture; var recres = mRec.RecognizeSingleGesture(segm); if ( recres.Confidence1 > 0.5f ) { Console.WriteLine("Recognized gesture: {0}", recres.Gesture1); } else { Console.WriteLine("Inconclusive"); } }; string filename = "gestures/track_high_kick_01.log"; if ( args.Length > 1 ) filename = args[1]; InputGesture gest; Thread visthread; switch ( c ) { case Command.Train: Train(); break; case Command.TestSingle: LoadModels(); var result = mRec.RecognizeSingleGesture(new InputGesture(new LogFileLoader(filename))); Console.WriteLine(result.ToString()); break; case Command.TestRecognize: LoadModels(); var test_gestures = LoadData(mGestureNames, false); int[] total = Enumerable.Range(0, test_gestures.Count).Select(x => 0).ToArray(), correct = Enumerable.Range(0, test_gestures.Count).Select(x => 0).ToArray(); int i = 0; Console.WriteLine(); foreach ( var gn in test_gestures ) { foreach ( var tg in gn.Value ) { total[i]++; var result2 = mRec.RecognizeSingleGesture(tg); if ( result2.Gesture1 == gn.Key ) { correct[i]++; } } Console.WriteLine("{0}: {1} correct / {2} total = {3}% correct", gn.Key, correct[i], total[i], (float)correct[i] / (float)total[i] * 100.0f); i++; } Console.WriteLine("TEST RESULTS:\n\t{0} correct / {1} total = {2}% correct", correct.Sum(), total.Sum(), (float)correct.Sum() / (float)total.Sum() * 100.0f); Utility.PrintMemoryUsage(); break; case Command.TestRealtime: LoadModels(); gest = new InputGesture(new LogFileLoader(filename)); visthread = new Thread(new ThreadStart(this.StartVisualize)); visthread.Start(); while ( true ) { // Wait for window to get created lock ( this ) { if ( mVisWindow != null ) break; } } float lastTime = gest.States[0].Timestamp; foreach ( var frame in gest.States ) { mSeg.AddState(frame); mVisWindow.CurrState = frame; Thread.Sleep((int)((frame.Timestamp - lastTime) * 750.0f)); lastTime = frame.Timestamp; } mSeg.Finish(); visthread.Abort(); break; case Command.RunRealtime: LoadModels(); visthread = new Thread(new ThreadStart(this.StartVisualize)); visthread.Start(); while ( true ) { // Wait for window to get created lock ( this ) { if ( mVisWindow != null ) break; } } var nfl = new NetworkFrameListener(4711); nfl.Start(); Console.WriteLine("Press the 'x' key to quit"); while ( true ) { var frame = nfl.GetState(); if ( frame != null ) { var js = JointState.FromRawJointState(frame); mSeg.AddState(js); mVisWindow.CurrState = js; } if ( Console.KeyAvailable ) { var cki = Console.ReadKey(true); if ( cki.Key == ConsoleKey.X ) break; } if ( !nfl.Running ) break; Thread.Sleep(0); } mSeg.Finish(); nfl.Stop(); visthread.Abort(); break; case Command.PrintFeatures: gest = new InputGesture(new LogFileLoader(filename)); /*foreach ( var f in Features.AllFeatures.SingleGestureFeatures ) { Console.WriteLine("{0}: {1}", f.ToString(), f.QueryGesture(gest)); }*/ foreach ( var f in Features.AllFeatures.GestureFeatures ) { Console.WriteLine("{0}: {1}", f.ToString(), f.QueryGesture(gest)); } break; case Command.BenchmarkRecognize: Benchmarks.BenchmarkRecognize(); break; case Command.CycleCV: mCV.Incr(); mCV.Save(); break; } }