Esempio n. 1
0
        public override HiddenModel createModel(IEnumerable <BaseTrajectory> traces, StrokeMap strokeMap)
        {
            var strokes      = traces.ToArray();
            var symbolTraces = strokes.Select(s => strokeMap.getSymbolTrace(s)).ToArray();

            var transitionDatas = strokeMap.Areas.Select((a, i) => transitionSetup.createTransitionDataToArea(i, "S" + strokeMap.ID + "_A" + (i + 1))).ToArray();

            foreach (var symTrace in symbolTraces)
            {
                var prevT = symTrace[0].Time;
                for (int i = 1; i < symTrace.Length; i++)
                {
                    var dT = (int)(symTrace[i].Time - prevT);
                    prevT = symTrace[i].Time;
                    transitionDatas[i - 1].addData(dT);
                }
            }

            var   startState = new StartState();
            State curState   = startState;
            State nextState;

            foreach (var transData in transitionDatas)
            {
                nextState = new State();
                var transition = transData.createTransition(nextState);
                curState.addTransition(transition);
                curState = nextState;
            }

            var model = new HiddenModel(startState, curState);

            return(model);
        }
        /// <summary>
        /// determines whether the given trace fits the gesture representation
        /// </summary>
        /// <param name="trace"></param>
        /// <returns>an arbitrary double value giving a measure for the similarity; 0 states that the given trace is no fitting example of the gesture </returns>
        public double validateGestureTrace(BaseTrajectory trace)
        {
            var O = strokeMap.getSymbolTrace(trace);

            if (O == null)
            {
                return(0);
            }

            //insert dummy symbol
            //O = new Observation[] { new Observation("Dummy", O[0].Time) }.Concat(O).ToArray();
            //also check time dynamics
            var eval = model.evaluate(O, true);

            //normalize
            return(eval /*/ model.MaxEvaluation*/);
        }
        /// <summary>
        /// determines whether the given trace fits the gesture representation
        /// </summary>
        /// <param name="trace"></param>
        /// <returns>an arbitrary double value giving a measure for the similarity; 0 states that the given trace is no fitting example of the gesture </returns>
        public double validateGestureTrace(BaseTrajectory trace)
        {
            var O = strokeMap.getSymbolTrace(trace);

            foreach (var symbol_idx in O)
            {
                if (symbol_idx == null)
                {
                    return(0);
                }
            }
            O = O.Where(w => w.Symbol != "GestureStart").ToArray();
            //return 1;

            //insert dummy symbol
            //O = new Observation[] { new Observation("Dummy", O[0].Time) }.Concat(O).ToArray();
            //also check time dynamics
            var eval = model.evaluate(O, true);

            //normalize
            return(eval /*/ model.MaxEvaluation*/);
        }
        public override CHnMModel createModel(IEnumerable <BaseTrajectory> traces, StrokeMap strokeMap)
        {
            var strokes      = traces.ToArray();
            var symbolTraces = strokes.Select(s => strokeMap.getSymbolTrace(s)).ToArray();

            var transitionDatas = strokeMap.Areas.Select((a, i) => transitionSetup.createTransitionDataToArea(i, "S" + strokeMap.ID + "_A" + (i + 1))).ToArray();

            foreach (var symTrace in symbolTraces)
            {
                if (symTrace != null)
                {
                    var has_empty = symTrace.Where(w => w == null).ToArray();
                    if (has_empty.Length == 0)
                    {
                        var symTrace_clean = symTrace.Where(w => w.Symbol != "GestureStart").ToArray();
                        var prevT          = symTrace[0].Time;
                        for (int i = 0; i < symTrace_clean.Length; i++)
                        {
                            var dT = (int)(symTrace_clean[i].Time - prevT);
                            prevT = symTrace_clean[i].Time;
                            transitionDatas[i].addData(dT);;
                        }
                    }
                    else
                    {
                        //handle the empty datas here
                    }
                }
            }

            var   startState = new StartState();
            State curState   = startState;
            State nextState;

            int ai = 0;

            foreach (var transData in transitionDatas)
            {
                nextState = new State();
                var transition = transData.createTransition(nextState, strokeMap.Areas[ai++]);
                curState.addTransition(transition);
                curState = nextState;
            }

            var model = new CHnMModel(startState, curState);

            return(model);
        }
        public override CHnMModel createModel(IEnumerable <BaseTrajectory> traces, StrokeMap strokeMap)
        {
            var strokes      = traces.ToArray();
            var symbolTraces = strokes.Select(s => strokeMap.getSymbolTrace(s)).ToArray();

            var transitionDatas = strokeMap.Areas.Select((a, i) => transitionSetup.createTransitionDataToArea("S" + strokeMap.ID + "_A" + (i + 1))).ToArray();

            ////number of areas -> transitiondata to endState
            //var endTransitions = new Dictionary<int,TransitionData>(15);

            var endTransitionData = transitionSetup.createTransitionDataToEnd();


            foreach (var symTrace in symbolTraces)
            {
                var prevT = symTrace[0].Time;             //GestureStart is dummy symbol marking the beginning
                for (int i = 1; i < symTrace.Length; i++) //go through all symbols of a trace (except GestureEnd)
                {
                    var dT = (int)(symTrace[i].Time - prevT);
                    prevT = symTrace[i].Time;

                    if (i == symTrace.Length - 1)
                    {
                        endTransitionData.addData(dT);
                        //TransitionData endTransition;
                        //if(endTransitions.TryGetValue(i, out endTransition))
                        //{
                        //    endTransition.addData(dT);
                        //}
                        //else
                        //{
                        //    Debug.Assert(symTrace[i].Symbol == "GestureEnd");
                        //    endTransition = transitionSetup.createTransitionDataToEnd(); //use endtransition
                        //    endTransition.addData(dT);
                        //    endTransitions[i] = endTransition;
                        //}
                    }
                    else
                    {
                        transitionDatas[i - 1].addData(dT);
                    }
                }
            }

            //var endTransPositions = endTransitions.Select(e => e.Key).OrderBy(x => x).ToArray();
            //Debug.Assert(endTransPositions.Last() - endTransPositions.First() == endTransitions.Count-1);

            var   startState = new StartState();
            var   areaStates = new State[strokeMap.Areas.Length];
            var   endState   = new State();
            State curState   = startState;
            State nextState;

            int curAreaState = 0;

            foreach (var transData in transitionDatas)
            {
                nextState = new State();
                areaStates[curAreaState++] = nextState;
                var transition = transData.createTransition(nextState, null);
                curState.addTransition(transition);
                curState = nextState;
            }

            curState.addTransition(endTransitionData.createTransition(endState, null));

            //add end transitions
            //foreach(var end in endTransitions)
            //{
            //    var prevAreaState = areaStates[end.Key - 2];
            //    prevAreaState.addTransition(end.Value.createTransition(endState));
            //}

            var model = new CHnMModel(startState, endState);

            return(model);
        }