Esempio n. 1
0
        /// <summary>
        /// Returns the emission probability of a particular byte. This is used to ensure we
        /// don't look further than defined by the assoicated length state. If we're beyond
        /// the length return ALMOST_ZERO, else return the existing base probability.
        /// </summary>
        /// <param name="value">List of byte outputs from the State.</param>
        /// <param name="index">Index of the byte in values, whose emission probability,
        /// given the BcdDigitState State is required.</param>
        /// <returns>The emission probability of an output byte, given the BcdDigitState state.</returns>
        public override double GetValueProbability(byte[] values, int index, Viterbi viterbi)
        {
            var baseProb = base.GetValueProbability(values, index, viterbi);

            if (baseProb == ALMOST_ZERO) return baseProb;

            var path = viterbi.FindPath(index - 1, viterbi.FromState, 24);

            int lengthIndex = -1;
            int byteDistance = 0;

            for (int i = path.Count - 1; i >= 0; i--) {
                byteDistance++;
                if (path[i] == LengthState) {
                    lengthIndex = index - byteDistance;
                    break;
                }
            }

            if (lengthIndex == -1) return ALMOST_ZERO;
            // Subtract 1 from the distance to allow for the Type of Address (TOA)
            // byte between the length and start of BCD phone number.
            int phoneNumLength = values[lengthIndex];
            if (((phoneNumLength + 1) / 2) < (byteDistance - 1)) {
                return ALMOST_ZERO;
            }

            return baseProb;
        }
        public override double GetValueProbability(byte[] values, int index, Viterbi viterbi)
        {
            var baseProb = base.GetValueProbability(values, index, viterbi);

            if (baseProb == ALMOST_ZERO)
                return baseProb;

            var path = viterbi.FindPath(index - 1, viterbi.FromState, index - 1);

            int lengthIndex = -1;
            int distance = 0;

            for (int i = path.Count - 1; i >= 0; i--)
            {
                distance++;
                if (path[i] == LengthState)
                {
                    lengthIndex = index - distance;
                    break;
                }
            }

            if (lengthIndex == -1)
                return ALMOST_ZERO;

            int length = values[lengthIndex];

            if (length < distance + 1)
                return ALMOST_ZERO;

            return base.GetValueProbability(values, index, viterbi);
        }
Esempio n. 3
0
        /// <summary>
        /// Returns the emission probability of a particular byte. If the probability is explicitly defined it returns
        /// that value. Otherwise it returns ALMOST_ZERO.
        /// </summary>
        /// <param name="values">List of byte outputs from SevenBit State.</param>
        /// <param name="index">Index of the byte in values, whose emission probability, given the SevenBit State is required.</param>
        /// <param name="viterbi"></param>
        /// <returns>The emission probability of an output byte, given the SevenBit state.</returns>
        public override double GetValueProbability(byte[] values, int index, Viterbi viterbi)
        {
            var baseProb = base.GetValueProbability(values, index, viterbi);

            if (baseProb == ALMOST_ZERO)
                return baseProb;

            var path = viterbi.FindPath(index - 1, viterbi.FromState, 141);

            int lengthIndex = -1;
            int byteDistance = 0;

            for (int i = path.Count - 1; i >= 0; i--) {
                byteDistance++;
                if (path[i] == LengthState) {
                    lengthIndex = index - byteDistance;
                    break;
                }
            }

            if (lengthIndex == -1)
                return ALMOST_ZERO;

            int septetLength = values[lengthIndex];

            //Convert from number of septets to number of bytes
            int byteLength = (int)Math.Ceiling((double)septetLength * 7 / 8);

            if (!IsEnd) {
                if (byteLength < byteDistance) {
                    return ALMOST_ZERO;
                } else if (byteLength > byteDistance) {
                    //Perform a partial decode of the bytes. If there are any invalid characters we want to quit now. This (should) prevent problems of long fields subsuming smaller fields.
                    if (!IsValid(byteDistance, index, values, byteDistance)) {
                        return ALMOST_ZERO;
                    }
                }
            } else {
                if (byteLength != byteDistance) {
                    return ALMOST_ZERO;
                } else {
                    if (!IsValid(byteLength, index, values, septetLength)) {
                        return ALMOST_ZERO;
                    }
                }
            }

            return base.GetValueProbability(values, index, viterbi);
        }
Esempio n. 4
0
        /// <summary>
        /// Returns the emission probability of a particular byte. This is used to ensure we
        /// don't look further than defined by the assoicated length state. If we're beyond
        /// the length return ALMOST_ZERO, else return the existing base probability.
        /// </summary>
        /// <param name="value">List of byte outputs from the State.</param>
        /// <param name="index">Index of the byte in values, whose emission probability,
        /// given the BcdDigitState State is required.</param>
        /// <returns>The emission probability of an output byte, given the BcdDigitState state.</returns>
        public override double GetValueProbability(byte[] values, int index, Viterbi viterbi)
        {
            var baseProb = base.GetValueProbability(values, index, viterbi);

            if (baseProb == ALMOST_ZERO)
            {
                return(baseProb);
            }

            var path = viterbi.FindPath(index - 1, viterbi.FromState, 24);

            int lengthIndex  = -1;
            int byteDistance = 0;

            for (int i = path.Count - 1; i >= 0; i--)
            {
                byteDistance++;
                if (path[i] == LengthState)
                {
                    lengthIndex = index - byteDistance;
                    break;
                }
            }

            if (lengthIndex == -1)
            {
                return(ALMOST_ZERO);
            }
            // Subtract 1 from the distance to allow for the Type of Address (TOA)
            // byte between the length and start of BCD phone number.
            int phoneNumLength = values[lengthIndex];

            if (((phoneNumLength + 1) / 2) < (byteDistance - 1))
            {
                return(ALMOST_ZERO);
            }

            return(baseProb);
        }
        public override double GetValueProbability(byte[] values, int index, Viterbi viterbi)
        {
            var baseProb = base.GetValueProbability(values, index, viterbi);

            if (baseProb == ALMOST_ZERO)
            {
                return(baseProb);
            }

            var path = viterbi.FindPath(index - 1, viterbi.FromState, index - 1);

            int lengthIndex = -1;
            int distance    = 0;

            for (int i = path.Count - 1; i >= 0; i--)
            {
                distance++;
                if (path[i] == LengthState)
                {
                    lengthIndex = index - distance;
                    break;
                }
            }

            if (lengthIndex == -1)
            {
                return(ALMOST_ZERO);
            }

            int length = values[lengthIndex];

            if (length < distance + 1)
            {
                return(ALMOST_ZERO);
            }

            return(base.GetValueProbability(values, index, viterbi));
        }
        public override double GetValueProbability(byte[] values, int index, Viterbi viterbi)
        {
            var baseProb = base.GetValueProbability(values, index, viterbi);

            if (baseProb == ALMOST_ZERO)
            {
                return(baseProb);
            }

            var path = viterbi.FindPath(index - 1, viterbi.FromState, index - 1);

            int lengthIndex = -1;
            int distance    = 0;

            for (int i = path.Count - 1; i >= 0; i--)
            {
                distance++;
                if (path[i] == LengthState)
                {
                    lengthIndex = index - distance;
                    break;
                }
            }

            if (lengthIndex == -1)
            {
                return(ALMOST_ZERO);
            }

            int headerLength = values[lengthIndex];

            if (headerLength >= (distance))
            {
                return(ALMOST_ZERO);
            }

            var serialTypes = new List <int>();
            var varint      = new List <byte>();

            //Grab the varints that sum to the length of the record size (sans header)
            for (int i = 0; i < headerLength - 1; i++)
            {
                byte b = values[lengthIndex + 1 + i];
                varint.Add(b);

                if (b < 0x80 || varint.Count() == 9)
                {
                    serialTypes.Add(ParseVarint(varint.ToArray()));
                    varint.Clear();
                }
            }

            int recordLength;

            if (_lengthDict.ContainsKey(lengthIndex))
            {
                recordLength = _lengthDict[lengthIndex];
            }
            else
            {
                recordLength = CalculateRecordLength(serialTypes);
                _lengthDict.Add(lengthIndex, recordLength);
            }

            if (recordLength != (distance + 1 - headerLength))
            {
                return(ALMOST_ZERO);
            }

            return(base.GetValueProbability(values, index, viterbi));
        }
Esempio n. 7
0
        /// <summary>
        /// Returns the emission probability of a particular byte. If the probability is explicitly defined it returns
        /// that value. Otherwise it returns ALMOST_ZERO.
        /// </summary>
        /// <param name="values">List of byte outputs from SevenBit State.</param>
        /// <param name="index">Index of the byte in values, whose emission probability, given the SevenBit State is required.</param>
        /// <param name="viterbi"></param>
        /// <returns>The emission probability of an output byte, given the SevenBit state.</returns>
        public override double GetValueProbability(byte[] values, int index, Viterbi viterbi)
        {
            var baseProb = base.GetValueProbability(values, index, viterbi);

            if (baseProb == ALMOST_ZERO)
            {
                return(baseProb);
            }

            var path = viterbi.FindPath(index - 1, viterbi.FromState, 141);

            int lengthIndex  = -1;
            int byteDistance = 0;

            for (int i = path.Count - 1; i >= 0; i--)
            {
                byteDistance++;
                if (path[i] == LengthState)
                {
                    lengthIndex = index - byteDistance;
                    break;
                }
            }

            if (lengthIndex == -1)
            {
                return(ALMOST_ZERO);
            }

            int septetLength = values[lengthIndex];

            //Convert from number of septets to number of bytes
            int byteLength = (int)Math.Ceiling((double)septetLength * 7 / 8);

            if (!IsEnd)
            {
                if (byteLength < byteDistance)
                {
                    return(ALMOST_ZERO);
                }
                else if (byteLength > byteDistance)
                {
                    //Perform a partial decode of the bytes. If there are any invalid characters we want to quit now. This (should) prevent problems of long fields subsuming smaller fields.
                    if (!IsValid(byteDistance, index, values, byteDistance))
                    {
                        return(ALMOST_ZERO);
                    }
                }
            }
            else
            {
                if (byteLength != byteDistance)
                {
                    return(ALMOST_ZERO);
                }
                else
                {
                    if (!IsValid(byteLength, index, values, septetLength))
                    {
                        return(ALMOST_ZERO);
                    }
                }
            }

            return(base.GetValueProbability(values, index, viterbi));
        }
        public override double GetValueProbability(byte[] values, int index, Viterbi viterbi)
        {
            var baseProb = base.GetValueProbability(values, index, viterbi);

            if (baseProb == ALMOST_ZERO)
                return baseProb;

            var path = viterbi.FindPath(index - 1, viterbi.FromState, index - 1);

            int lengthIndex = -1;
            int distance = 0;

            for (int i = path.Count - 1; i >= 0; i--)
            {
                distance++;
                if (path[i] == LengthState)
                {
                    lengthIndex = index - distance;
                    break;
                }
            }

            if (lengthIndex == -1)
                return ALMOST_ZERO;

            int headerLength = values[lengthIndex];

            if (headerLength >= (distance))
                return ALMOST_ZERO;

            var serialTypes = new List<int>();
            var varint = new List<byte>();

            //Grab the varints that sum to the length of the record size (sans header)
            for (int i = 0; i < headerLength-1; i++ )
            {
                byte b = values[lengthIndex + 1 + i];
                varint.Add(b);

                if( b < 0x80 || varint.Count() == 9)
                {
                    serialTypes.Add(ParseVarint(varint.ToArray()));
                    varint.Clear();
                }

            }

            int recordLength;

            if (_lengthDict.ContainsKey(lengthIndex))
            {
                recordLength = _lengthDict[lengthIndex];
            }
            else
            {
                recordLength = CalculateRecordLength(serialTypes);
                _lengthDict.Add(lengthIndex, recordLength);
            }

            if (recordLength != (distance + 1 - headerLength))
                return ALMOST_ZERO;

            return base.GetValueProbability(values, index, viterbi);
        }