Example #1
0
 /// <summary>
 /// Gets the state machine for a user-defined phone number.
 /// </summary>
 /// <param name="userState">Object representing the user-defined state machine.</param>
 /// <param name="weight"></param>
 /// <returns>The state machine.</returns>
 public static StateMachine GetPhoneNumber_UserDefined(UserState userState, int weight)
 {
     StateMachine phoneNumber = new StateMachine { Name = userState.MachineType, _weight = weight };
     State prevState = null;
     // Have a minimum of 2 bytes.
     for (int n = 0; n < userState.Bytes.Count - 1; n++)
     {
         string nm = String.Format("UserPhoneNumberByte{0}", n);
         State state = new State { Name = nm, ParentStateMachine = phoneNumber };
         phoneNumber.AddState(state);
         if (n == 0)
         {
             phoneNumber.StartingStates.Add(state);
         }
         else
         {
             AddTransition(prevState, state, 1d);
         }
         prevState = state;
         UserDefinedByteProbabilities(state, userState.Bytes[n]);
     }
     UserDefinedState endState = new UserDefinedState(userState) { Name = "EndUserPhoneNumberByte", ParentStateMachine = phoneNumber };
     phoneNumber.AddState(endState);
     UserDefinedByteProbabilities(endState, userState.Bytes[userState.Bytes.Count - 1]);
     AddTransition(prevState, endState, 1d);
     phoneNumber.EndingStates.Add(endState);
     return phoneNumber;
 }
Example #2
0
 /// <summary>
 /// Gets the state machine for a user-defined text field.
 /// </summary>
 /// <param name="userState">Object representing the user-defined state machine.</param>
 /// <param name="weight"></param>
 /// <returns>The state machine.</returns>
 public static StateMachine GetText_UserDefined(UserState userState, int weight)
 {
     StateMachine textState = new StateMachine { Name = userState.MachineType, _weight = weight };
     State prevState = null;
     // Have a minimum of 2 bytes.
     for (int n = 0; n < userState.Bytes.Count - 1; n++)
     {
         State state = new State { Name = String.Format("UserTextByte{0}", n), ParentStateMachine = textState };
         textState.AddState(state);
         if (n == 0)
         {
             textState.StartingStates.Add(state);
         }
         else
         {
             AddTransition(prevState, state, 1d);
         }
         prevState = state;
         UserDefinedByteProbabilities(state, userState.Bytes[n]);
     }
     UserDefinedState endState = new UserDefinedState(userState) { Name = "EndUserTextByte", ParentStateMachine = textState };
     textState.AddState(endState);
     UserDefinedByteProbabilities(endState, userState.Bytes[userState.Bytes.Count - 1]);
     AddTransition(prevState, endState, 1d);
     textState.EndingStates.Add(endState);
     return textState;
 }
Example #3
0
        /// <summary>
        ///
        /// </summary>
        /// <param name="path"></param>
        /// <param name="index"></param>
        /// <returns>The first index after the end of the field</returns>
        private ViterbiField ExtractField(List <State> path, int index)
        {
            int         startIndex  = index;
            MachineList machineName = path[index].ParentStateMachine.Name;
            string      fieldHex    = "0x";
            string      fieldAscii  = "";
            List <byte> fieldBytes  = new List <byte>();

            while (index < path.Count && machineName == path[index].ParentStateMachine.Name)
            {
                fieldBytes.Add(_observations[index]);

                //If this is an anchor run, no need to print fields
                if (!_isAnchor)
                {
                    string observation = Convert.ToString(_observations[index], 16).PadLeft(2, '0');

                    fieldHex   += observation;
                    fieldAscii += (char)_observations[index];

#if PRINT_ALL
                    Console.WriteLine("{0}\t:\t{1}\t{2}", path[index], observation, (char)_observations[index]);
#endif
                }
                index++;

                //Allows us to distinguish between adjacent fields of the same type
                if (path[index - 1].IsSplitState)
                {
                    break;
                }
            }

            string fieldString;
            if (!_isAnchor && ((machineName == MachineList.PhoneNumber_User) ||
                               (machineName == MachineList.TimeStamp_User) ||
                               (machineName == MachineList.TimeStamp_User)))
            {
                // If we're printing a user-defined field then we need to use the user-defined
                // methods.
                try {
                    UserDefinedState udState = path[index - 1] as UserDefinedState;
                    fieldString = Printer.GetUserField(machineName, fieldBytes.ToArray(), udState.UserStateObj);
                } catch {
                    fieldString = "???";
                }
            }
            else
            {
                fieldString = _isAnchor ? "" : Printer.GetField(machineName, fieldBytes.ToArray());
            }

            var field = new ViterbiField
            {
                OffsetPath  = startIndex,
                OffsetFile  = _fileOffset + startIndex,
                HexString   = fieldHex,
                AsciiString = fieldAscii,
                FieldString = fieldString,
                MachineName = machineName,
                Raw         = fieldBytes.ToArray()
            };

            _textList.Add(fieldString);
            _fieldList.Add(field);

            #if PRINT_FIELD
            Console.WriteLine(field);
            #endif

            return(field);
        }