Esempio n. 1
0
        public byte[] Hash(string text)
        {
            var words  = text.Split();
            var result = new int[_byteCount * 8];

            foreach (var grams in words.GetNGrams(_gramCount))
            {
                var gram = string.Join(' ', grams);
                var hash = new BitArray(HashTools.GetByteHash(gram, _byteCount));

                for (var i = 0; i < hash.Length; i++)
                {
                    if (hash[i])
                    {
                        result[i]++;
                    }
                    else
                    {
                        result[i]--;
                    }
                }
            }

            var bits = result
                       .Select(x => x > 0)
                       .ToArray();

            var bytes = new byte[_byteCount];

            new BitArray(bits).CopyTo(bytes, 0);

            return(bytes);
        }