Exemple #1
0
        public void Test()
        {
            String test = "The quick red fox jumped over the lazy brown dogs";

            NumericPayloadTokenFilter nptf = new NumericPayloadTokenFilter(new WordTokenFilter(new WhitespaceTokenizer(new StringReader(test))), 3, "D");
            bool              seenDogs     = false;
            ITermAttribute    termAtt      = nptf.GetAttribute <ITermAttribute>();
            ITypeAttribute    typeAtt      = nptf.GetAttribute <ITypeAttribute>();
            IPayloadAttribute payloadAtt   = nptf.GetAttribute <IPayloadAttribute>();

            while (nptf.IncrementToken())
            {
                if (termAtt.Term.Equals("dogs"))
                {
                    seenDogs = true;
                    Assert.True(typeAtt.Type.Equals("D") == true, typeAtt.Type + " is not equal to " + "D");
                    Assert.True(payloadAtt.Payload != null, "payloadAtt.GetPayload() is null and it shouldn't be");
                    byte[] bytes = payloadAtt.Payload.GetData();//safe here to just use the bytes, otherwise we should use offset, length
                    Assert.True(bytes.Length == payloadAtt.Payload.Length, bytes.Length + " does not equal: " + payloadAtt.Payload.Length);
                    Assert.True(payloadAtt.Payload.Offset == 0, payloadAtt.Payload.Offset + " does not equal: " + 0);
                    float pay = PayloadHelper.DecodeFloat(bytes);
                    Assert.True(pay == 3, pay + " does not equal: " + 3);
                }
                else
                {
                    Assert.True(typeAtt.Type.Equals("word"), typeAtt.Type + " is not null and it should be");
                }
            }
            Assert.True(seenDogs == true, seenDogs + " does not equal: " + true);
        }
        /// <summary>
        /// Returns a 32 bit float from the payload, or 1f it null.
        /// </summary>
        /// <param name="token"></param>
        /// <returns></returns>
        public override float GetWeight(Token token)
        {
            if (token.Payload == null || token.Payload.GetData() == null)
            {
                return(1f);
            }

            return(PayloadHelper.DecodeFloat(token.Payload.GetData()));
        }
        private static void AssertNext(TokenStream ts, String text, int positionIncrement, float boost, int startOffset,
                                       int endOffset)
        {
            var termAtt    = ts.AddAttribute <ITermAttribute>();
            var posIncrAtt = ts.AddAttribute <IPositionIncrementAttribute>();
            var payloadAtt = ts.AddAttribute <IPayloadAttribute>();
            var offsetAtt  = ts.AddAttribute <IOffsetAttribute>();

            Assert.IsTrue(ts.IncrementToken());
            Assert.AreEqual(text, termAtt.Term);
            Assert.AreEqual(positionIncrement, posIncrAtt.PositionIncrement);
            Assert.AreEqual(boost,
                            payloadAtt.Payload == null
                                ? 1f
                                : PayloadHelper.DecodeFloat(payloadAtt.Payload.GetData()), 0);
            Assert.AreEqual(startOffset, offsetAtt.StartOffset);
            Assert.AreEqual(endOffset, offsetAtt.EndOffset);
        }