Exemplo n.º 1
0
        public virtual void TestCaptureState()
        {
            // init a first instance
            AttributeSource    src     = new AttributeSource();
            ICharTermAttribute termAtt = src.AddAttribute <ICharTermAttribute>();
            ITypeAttribute     typeAtt = src.AddAttribute <ITypeAttribute>();

            termAtt.Append("TestTerm");
            typeAtt.Type = "TestType";
            int hashCode = src.GetHashCode();

            AttributeSource.State state = src.CaptureState();

            // modify the attributes
            termAtt.SetEmpty().Append("AnotherTestTerm");
            typeAtt.Type = "AnotherTestType";
            Assert.IsTrue(hashCode != src.GetHashCode(), "Hash code should be different");

            src.RestoreState(state);
            Assert.AreEqual(termAtt.ToString(), "TestTerm");
            Assert.AreEqual(typeAtt.Type, "TestType");
            Assert.AreEqual(hashCode, src.GetHashCode(), "Hash code should be equal after restore");

            // restore into an exact configured copy
            AttributeSource copy = new AttributeSource();

            copy.AddAttribute <ICharTermAttribute>();
            copy.AddAttribute <ITypeAttribute>();
            copy.RestoreState(state);
            Assert.AreEqual(src.GetHashCode(), copy.GetHashCode(), "Both AttributeSources should have same hashCode after restore");
            Assert.AreEqual(src, copy, "Both AttributeSources should be equal after restore");

            // init a second instance (with attributes in different order and one additional attribute)
            AttributeSource src2 = new AttributeSource();

            typeAtt = src2.AddAttribute <ITypeAttribute>();
            IFlagsAttribute flagsAtt = src2.AddAttribute <IFlagsAttribute>();

            termAtt        = src2.AddAttribute <ICharTermAttribute>();
            flagsAtt.Flags = 12345;

            src2.RestoreState(state);
            Assert.AreEqual(termAtt.ToString(), "TestTerm");
            Assert.AreEqual(typeAtt.Type, "TestType");
            Assert.AreEqual(12345, flagsAtt.Flags, "FlagsAttribute should not be touched");

            // init a third instance missing one Attribute
            AttributeSource src3 = new AttributeSource();

            termAtt = src3.AddAttribute <ICharTermAttribute>();
            try
            {
                src3.RestoreState(state);
                Assert.Fail("The third instance is missing the TypeAttribute, so restoreState() should throw IllegalArgumentException");
            }
            catch (Exception iae) when(iae.IsIllegalArgumentException())
            {
                // pass
            }
        }
Exemplo n.º 2
0
        public virtual void TestToStringAndMultiAttributeImplementations()
        {
            AttributeSource src     = new AttributeSource();
            TermAttribute   termAtt = (TermAttribute)src.AddAttribute(typeof(TermAttribute));
            TypeAttribute   typeAtt = (TypeAttribute)src.AddAttribute(typeof(TypeAttribute));

            termAtt.SetTermBuffer("TestTerm");
            typeAtt.SetType("TestType");
            Assert.AreEqual("(" + termAtt.ToString() + "," + typeAtt.ToString() + ")", src.ToString(), "Attributes should appear in original order");
            System.Collections.Generic.IEnumerator <AttributeImpl> it = src.GetAttributeImplsIterator().GetEnumerator();
            Assert.IsTrue(it.MoveNext(), "Iterator should have 2 attributes left");
            Assert.AreSame(termAtt, it.Current, "First AttributeImpl from iterator should be termAtt");
            Assert.IsTrue(it.MoveNext(), "Iterator should have 1 attributes left");
            Assert.AreSame(typeAtt, it.Current, "Second AttributeImpl from iterator should be typeAtt");
            Assert.IsFalse(it.MoveNext(), "Iterator should have 0 attributes left");

            src = new AttributeSource();
            src.AddAttributeImpl(new Token());
            // this should not add a new attribute as Token implements TermAttribute, too
            termAtt = (TermAttribute)src.AddAttribute(typeof(TermAttribute));
            Assert.IsTrue(termAtt is Token, "TermAttribute should be implemented by Token");
            // get the Token attribute and check, that it is the only one
            it = src.GetAttributeImplsIterator().GetEnumerator();
            Assert.IsTrue(it.MoveNext());
            Token tok = (Token)it.Current;

            Assert.IsFalse(it.MoveNext(), "There should be only one attribute implementation instance");

            termAtt.SetTermBuffer("TestTerm");
            Assert.AreEqual("(" + tok.ToString() + ")", src.ToString(), "Token should only printed once");
        }
Exemplo n.º 3
0
        public virtual void TestCloneAttributes()
        {
            AttributeSource src     = new AttributeSource();
            TermAttribute   termAtt = (TermAttribute)src.AddAttribute(typeof(TermAttribute));
            TypeAttribute   typeAtt = (TypeAttribute)src.AddAttribute(typeof(TypeAttribute));

            termAtt.SetTermBuffer("TestTerm");
            typeAtt.SetType("TestType");

            AttributeSource clone = src.CloneAttributes();

            System.Collections.IEnumerator it = clone.GetAttributeClassesIterator().GetEnumerator();
            Assert.IsTrue(it.MoveNext());
            Assert.AreEqual(typeof(TermAttribute), it.Current, "TermAttribute must be the first attribute");
            Assert.IsTrue(it.MoveNext());
            Assert.AreEqual(typeof(TypeAttribute), it.Current, "TypeAttribute must be the second attribute");
            Assert.IsFalse(it.MoveNext(), "No more attributes");

            TermAttribute termAtt2 = (TermAttribute)clone.GetAttribute(typeof(TermAttribute));
            TypeAttribute typeAtt2 = (TypeAttribute)clone.GetAttribute(typeof(TypeAttribute));

            Assert.IsFalse(ReferenceEquals(termAtt2, termAtt), "TermAttribute of original and clone must be different instances");
            Assert.IsFalse(ReferenceEquals(typeAtt2, typeAtt), "TypeAttribute of original and clone must be different instances");
            Assert.AreEqual(termAtt2, termAtt, "TermAttribute of original and clone must be equal");
            Assert.AreEqual(typeAtt2, typeAtt, "TypeAttribute of original and clone must be equal");
        }
Exemplo n.º 4
0
        public virtual void TestCloneAttributes()
        {
            AttributeSource src     = new AttributeSource();
            ITermAttribute  termAtt = src.AddAttribute <ITermAttribute>();
            ITypeAttribute  typeAtt = src.AddAttribute <ITypeAttribute>();

            termAtt.SetTermBuffer("TestTerm");
            typeAtt.Type = "TestType";

            var                types = new List <Type>();
            AttributeSource    clone = src.CloneAttributes();
            IEnumerator <Type> it    = clone.GetAttributeTypesIterator().GetEnumerator();

            Assert.IsTrue(it.MoveNext());
            types.Add(it.Current);
            Assert.IsTrue(it.MoveNext());
            types.Add(it.Current);
            Assert.IsFalse(it.MoveNext(), "No more attributes");

            Assert.Contains(typeof(ITypeAttribute), types, "TypeAttribute must be present in attributes");
            Assert.Contains(typeof(ITermAttribute), types, "TermAttribute must be present in attributes");

            ITermAttribute termAtt2 = clone.GetAttribute <ITermAttribute>();
            ITypeAttribute typeAtt2 = clone.GetAttribute <ITypeAttribute>();

            Assert.IsFalse(ReferenceEquals(termAtt2, termAtt), "TermAttribute of original and clone must be different instances");
            Assert.IsFalse(ReferenceEquals(typeAtt2, typeAtt), "TypeAttribute of original and clone must be different instances");
            Assert.AreEqual(termAtt2, termAtt, "TermAttribute of original and clone must be equal");
            Assert.AreEqual(typeAtt2, typeAtt, "TypeAttribute of original and clone must be equal");
        }
Exemplo n.º 5
0
        public virtual void TestCaptureState()
        {
            // init a first instance
            AttributeSource src     = new AttributeSource();
            TermAttribute   termAtt = (TermAttribute)src.AddAttribute(typeof(TermAttribute));
            TypeAttribute   typeAtt = (TypeAttribute)src.AddAttribute(typeof(TypeAttribute));

            termAtt.SetTermBuffer("TestTerm");
            typeAtt.SetType("TestType");
            int hashCode = src.GetHashCode();

            AttributeSource.State state = src.CaptureState();

            // modify the attributes
            termAtt.SetTermBuffer("AnotherTestTerm");
            typeAtt.SetType("AnotherTestType");
            Assert.IsTrue(hashCode != src.GetHashCode(), "Hash code should be different");

            src.RestoreState(state);
            Assert.AreEqual("TestTerm", termAtt.Term());
            Assert.AreEqual("TestType", typeAtt.Type());
            Assert.AreEqual(hashCode, src.GetHashCode(), "Hash code should be equal after restore");

            // restore into an exact configured copy
            AttributeSource copy = new AttributeSource();

            copy.AddAttribute(typeof(TermAttribute));
            copy.AddAttribute(typeof(TypeAttribute));
            copy.RestoreState(state);
            Assert.AreEqual(src.GetHashCode(), copy.GetHashCode(), "Both AttributeSources should have same hashCode after restore");
            Assert.AreEqual(src, copy, "Both AttributeSources should be equal after restore");

            // init a second instance (with attributes in different order and one additional attribute)
            AttributeSource src2 = new AttributeSource();

            typeAtt = (TypeAttribute)src2.AddAttribute(typeof(TypeAttribute));
            Lucene.Net.Analysis.Tokenattributes.FlagsAttribute flagsAtt = (Lucene.Net.Analysis.Tokenattributes.FlagsAttribute)src2.AddAttribute(typeof(Lucene.Net.Analysis.Tokenattributes.FlagsAttribute));
            termAtt = (TermAttribute)src2.AddAttribute(typeof(TermAttribute));
            flagsAtt.SetFlags(12345);

            src2.RestoreState(state);
            Assert.AreEqual("TestTerm", termAtt.Term());
            Assert.AreEqual("TestType", typeAtt.Type());
            Assert.AreEqual(12345, flagsAtt.GetFlags(), "FlagsAttribute should not be touched");

            // init a third instance missing one Attribute
            AttributeSource src3 = new AttributeSource();

            termAtt = (TermAttribute)src3.AddAttribute(typeof(TermAttribute));
            try
            {
                src3.RestoreState(state);
                Assert.Fail("The third instance is missing the TypeAttribute, so restoreState() should throw IllegalArgumentException");
            }
            catch (System.ArgumentException iae)
            {
                // pass
            }
        }
Exemplo n.º 6
0
        public virtual void TestCaptureState()
        {
            // init a first instance
            AttributeSource src = new AttributeSource();
            TermAttribute termAtt = (TermAttribute)src.AddAttribute(typeof(TermAttribute));
            TypeAttribute typeAtt = (TypeAttribute)src.AddAttribute(typeof(TypeAttribute));
            termAtt.SetTermBuffer("TestTerm");
            typeAtt.SetType("TestType");
            int hashCode = src.GetHashCode();

            AttributeSource.State state = src.CaptureState();

            // modify the attributes
            termAtt.SetTermBuffer("AnotherTestTerm");
            typeAtt.SetType("AnotherTestType");
            Assert.IsTrue(hashCode != src.GetHashCode(), "Hash code should be different");

            src.RestoreState(state);
            Assert.AreEqual("TestTerm", termAtt.Term());
            Assert.AreEqual("TestType", typeAtt.Type());
            Assert.AreEqual(hashCode, src.GetHashCode(), "Hash code should be equal after restore");

            // restore into an exact configured copy
            AttributeSource copy = new AttributeSource();
            copy.AddAttribute(typeof(TermAttribute));
            copy.AddAttribute(typeof(TypeAttribute));
            copy.RestoreState(state);
            Assert.AreEqual(src.GetHashCode(), copy.GetHashCode(), "Both AttributeSources should have same hashCode after restore");
            Assert.AreEqual(src, copy, "Both AttributeSources should be equal after restore");

            // init a second instance (with attributes in different order and one additional attribute)
            AttributeSource src2 = new AttributeSource();
            typeAtt = (TypeAttribute)src2.AddAttribute(typeof(TypeAttribute));
            Lucene.Net.Analysis.Tokenattributes.FlagsAttribute flagsAtt = (Lucene.Net.Analysis.Tokenattributes.FlagsAttribute)src2.AddAttribute(typeof(Lucene.Net.Analysis.Tokenattributes.FlagsAttribute));
            termAtt = (TermAttribute)src2.AddAttribute(typeof(TermAttribute));
            flagsAtt.SetFlags(12345);

            src2.RestoreState(state);
            Assert.AreEqual("TestTerm", termAtt.Term());
            Assert.AreEqual("TestType", typeAtt.Type());
            Assert.AreEqual(12345, flagsAtt.GetFlags(), "FlagsAttribute should not be touched");

            // init a third instance missing one Attribute
            AttributeSource src3 = new AttributeSource();
            termAtt = (TermAttribute)src3.AddAttribute(typeof(TermAttribute));
            try
            {
                src3.RestoreState(state);
                Assert.Fail("The third instance is missing the TypeAttribute, so restoreState() should throw IllegalArgumentException");
            }
            catch (System.ArgumentException iae)
            {
                // pass
            }
        }
Exemplo n.º 7
0
        public virtual void TestCaptureState()
        {
            // init a first instance
            AttributeSource src = new AttributeSource();
            ICharTermAttribute termAtt = src.AddAttribute<ICharTermAttribute>();
            ITypeAttribute typeAtt = src.AddAttribute<ITypeAttribute>();
            termAtt.Append("TestTerm");
            typeAtt.Type = "TestType";
            int hashCode = src.GetHashCode();

            AttributeSource.State state = src.CaptureState();

            // modify the attributes
            termAtt.SetEmpty().Append("AnotherTestTerm");
            typeAtt.Type = "AnotherTestType";
            Assert.IsTrue(hashCode != src.GetHashCode(), "Hash code should be different");

            src.RestoreState(state);
            Assert.AreEqual(termAtt.ToString(), "TestTerm");
            Assert.AreEqual(typeAtt.Type, "TestType");
            Assert.AreEqual(hashCode, src.GetHashCode(), "Hash code should be equal after restore");

            // restore into an exact configured copy
            AttributeSource copy = new AttributeSource();
            copy.AddAttribute<ICharTermAttribute>();
            copy.AddAttribute<ITypeAttribute>();
            copy.RestoreState(state);
            Assert.AreEqual(src.GetHashCode(), copy.GetHashCode(), "Both AttributeSources should have same hashCode after restore");
            Assert.AreEqual(src, copy, "Both AttributeSources should be equal after restore");

            // init a second instance (with attributes in different order and one additional attribute)
            AttributeSource src2 = new AttributeSource();
            typeAtt = src2.AddAttribute<ITypeAttribute>();
            IFlagsAttribute flagsAtt = src2.AddAttribute<IFlagsAttribute>();
            termAtt = src2.AddAttribute<ICharTermAttribute>();
            flagsAtt.Flags = 12345;

            src2.RestoreState(state);
            Assert.AreEqual(termAtt.ToString(), "TestTerm");
            Assert.AreEqual(typeAtt.Type, "TestType");
            Assert.AreEqual(12345, flagsAtt.Flags, "FlagsAttribute should not be touched");

            // init a third instance missing one Attribute
            AttributeSource src3 = new AttributeSource();
            termAtt = src3.AddAttribute<ICharTermAttribute>();
            try
            {
                src3.RestoreState(state);
                Assert.Fail("The third instance is missing the TypeAttribute, so restoreState() should throw IllegalArgumentException");
            }
            catch (System.ArgumentException iae)
            {
                // pass
            }
        }
Exemplo n.º 8
0
        public virtual void TestInvalidArguments()
        {
            try
            {
                AttributeSource src = new AttributeSource();
                src.AddAttribute <Token>();
                Assert.Fail("Should throw IllegalArgumentException");
            }
            catch (System.ArgumentException iae)
            {
            }

            try
            {
                AttributeSource src = new AttributeSource(Token.TOKEN_ATTRIBUTE_FACTORY);
                src.AddAttribute <Token>();
                Assert.Fail("Should throw IllegalArgumentException");
            }
            catch (System.ArgumentException iae)
            {
            }

            /*try
             * {
             * AttributeSource src = new AttributeSource();
             * // break this by unsafe cast
             * src.AddAttribute<typeof((Type)IEnumerator)>();
             * Assert.Fail("Should throw IllegalArgumentException");
             * }
             * catch (System.ArgumentException iae)
             * {
             * }*/
        }
Exemplo n.º 9
0
        public override bool Accept(AttributeSource source)
        {
            if (typeAtt == null)
            {
                typeAtt = source.AddAttribute<ITypeAttribute>();
            }

            return typeToMatch.Equals(typeAtt.Type);
        }
Exemplo n.º 10
0
        public override bool Accept(AttributeSource source)
        {
            if (typeAtt == null)
            {
                typeAtt = source.AddAttribute<ITypeAttribute>();
            }

            //check to see if this is a Category
            return (typeToMatch.Equals(typeAtt.Type));
        }
Exemplo n.º 11
0
        public virtual void TestLUCENE_3042()
        {
            AttributeSource src1 = new AttributeSource();

            src1.AddAttribute <ICharTermAttribute>().Append("foo");
            int             hash1 = src1.GetHashCode(); // this triggers a cached state
            AttributeSource src2  = new AttributeSource(src1);

            src2.AddAttribute <ITypeAttribute>().Type = "bar";
            Assert.IsTrue(hash1 != src1.GetHashCode(), "The hashCode is identical, so the captured state was preserved.");
            Assert.AreEqual(src2.GetHashCode(), src1.GetHashCode());
        }
Exemplo n.º 12
0
        public virtual void TestCloneAttributes()
        {
            AttributeSource src      = new AttributeSource();
            IFlagsAttribute flagsAtt = src.AddAttribute <IFlagsAttribute>();
            ITypeAttribute  typeAtt  = src.AddAttribute <ITypeAttribute>();

            flagsAtt.Flags = 1234;
            typeAtt.Type   = "TestType";

            AttributeSource    clone = src.CloneAttributes();
            IEnumerator <Type> it    = clone.GetAttributeClassesEnumerator();

            it.MoveNext();
            Assert.AreEqual(typeof(IFlagsAttribute), it.Current, "FlagsAttribute must be the first attribute");
            it.MoveNext();
            Assert.AreEqual(typeof(ITypeAttribute), it.Current, "TypeAttribute must be the second attribute");
            Assert.IsFalse(it.MoveNext(), "No more attributes");

            IFlagsAttribute flagsAtt2 = clone.GetAttribute <IFlagsAttribute>();
            ITypeAttribute  typeAtt2  = clone.GetAttribute <ITypeAttribute>();

            Assert.AreNotSame(flagsAtt2, flagsAtt, "FlagsAttribute of original and clone must be different instances");
            Assert.AreNotSame(typeAtt2, typeAtt, "TypeAttribute of original and clone must be different instances");
            Assert.AreEqual(flagsAtt2, flagsAtt, "FlagsAttribute of original and clone must be equal");
            Assert.AreEqual(typeAtt2, typeAtt, "TypeAttribute of original and clone must be equal");

            // test copy back
            flagsAtt2.Flags = 4711;
            typeAtt2.Type   = "OtherType";
            clone.CopyTo(src);
            Assert.AreEqual(4711, flagsAtt.Flags, "FlagsAttribute of original must now contain updated term");
            Assert.AreEqual(typeAtt.Type, "OtherType", "TypeAttribute of original must now contain updated type");
            // verify again:
            Assert.AreNotSame(flagsAtt2, flagsAtt, "FlagsAttribute of original and clone must be different instances");
            Assert.AreNotSame(typeAtt2, typeAtt, "TypeAttribute of original and clone must be different instances");
            Assert.AreEqual(flagsAtt2, flagsAtt, "FlagsAttribute of original and clone must be equal");
            Assert.AreEqual(typeAtt2, typeAtt, "TypeAttribute of original and clone must be equal");
        }
Exemplo n.º 13
0
        public virtual void TestDefaultAttributeFactory()
        {
            AttributeSource src = new AttributeSource();

            Assert.IsTrue(src.AddAttribute <ICharTermAttribute>() is CharTermAttribute, "CharTermAttribute is not implemented by CharTermAttributeImpl");
            Assert.IsTrue(src.AddAttribute <IOffsetAttribute>() is OffsetAttribute, "OffsetAttribute is not implemented by OffsetAttributeImpl");
            Assert.IsTrue(src.AddAttribute <IFlagsAttribute>() is FlagsAttribute, "FlagsAttribute is not implemented by FlagsAttributeImpl");
            Assert.IsTrue(src.AddAttribute <IPayloadAttribute>() is PayloadAttribute, "PayloadAttribute is not implemented by PayloadAttributeImpl");
            Assert.IsTrue(src.AddAttribute <IPositionIncrementAttribute>() is PositionIncrementAttribute, "PositionIncrementAttribute is not implemented by PositionIncrementAttributeImpl");
            Assert.IsTrue(src.AddAttribute <ITypeAttribute>() is TypeAttribute, "TypeAttribute is not implemented by TypeAttributeImpl");
        }
Exemplo n.º 14
0
        public void TestInvalidArguments()
        {
            var src = new AttributeSource();

            Assert.Throws <ArgumentException>(() => src.AddAttribute <Token>(), "Should throw ArgumentException");

            src = new AttributeSource();
            Assert.Throws <ArgumentException>(() => src.AddAttribute <Token>(), "Should throw ArgumentException");

            //try
            //{
            //    AttributeSource src = new AttributeSource();
            //    src.AddAttribute<System.Collections.IEnumerator>(); //Doesn't compile.
            //    Assert.Fail("Should throw IllegalArgumentException");
            //}
            //catch (ArgumentException iae) { }
        }
Exemplo n.º 15
0
        public void TestInvalidArguments()
        {
            try
            {
                AttributeSource src = new AttributeSource();
                src.AddAttribute(typeof(Token));
                Assert.Fail("Should throw IllegalArgumentException");
            }
            catch (ArgumentException iae) { }

            try
            {
                AttributeSource src = new AttributeSource();
                src.AddAttribute(typeof(System.Collections.IEnumerator));
                Assert.Fail("Should throw IllegalArgumentException");
            }
            catch (ArgumentException iae) { }
        }
Exemplo n.º 16
0
        public virtual void TestInvalidArguments()
        {
            try
            {
                AttributeSource src = new AttributeSource();
                src.AddAttribute <Token>();
                Assert.Fail("Should throw IllegalArgumentException");
            }
#pragma warning disable 168
            catch (System.ArgumentException iae)
#pragma warning restore 168
            {
            }

            try
            {
                AttributeSource src = new AttributeSource(Token.TOKEN_ATTRIBUTE_FACTORY);
                src.AddAttribute <Token>();
                Assert.Fail("Should throw IllegalArgumentException");
            }
#pragma warning disable 168
            catch (System.ArgumentException iae)
#pragma warning restore 168
            {
            }


            // LUCENENET NOTE: Invalid type won't compile because
            // of the generic constraint, so this test is not necessary in .NET.

            /*try
             * {
             * AttributeSource src = new AttributeSource();
             * // break this by unsafe cast
             * src.AddAttribute<typeof((Type)IEnumerator)>();
             * Assert.Fail("Should throw IllegalArgumentException");
             * }
             * catch (System.ArgumentException iae)
             * {
             * }*/
        }
        public override bool Accept(AttributeSource source)
        {
            if (termAtt == null)
            {
                termAtt = source.AddAttribute<ITermAttribute>();
            }
            try
            {
                DateTime date = DateTime.Parse(termAtt.Term, dateFormat);//We don't care about the date, just that we can parse it as a date
                if (date != null)
                {
                    return true;
                }
            }
            catch (FormatException)
            {

            }

            return false;
        }
Exemplo n.º 18
0
        /// <summary>
        /// Constructor for enumeration of all terms from specified <code>reader</code> which share a prefix of
        /// length <code>prefixLength</code> with <code>term</code> and which have a fuzzy similarity &gt;
        /// <code>minSimilarity</code>.
        /// <p>
        /// After calling the constructor the enumeration is already pointing to the first
        /// valid term if such a term exists.
        /// </summary>
        /// <param name="terms"> Delivers terms. </param>
        /// <param name="atts"> <seealso cref="AttributeSource"/> created by the rewrite method of <seealso cref="MultiTermQuery"/>
        /// thats contains information about competitive boosts during rewrite. It is also used
        /// to cache DFAs between segment transitions. </param>
        /// <param name="term"> Pattern term. </param>
        /// <param name="minSimilarity"> Minimum required similarity for terms from the reader. Pass an integer value
        ///        representing edit distance. Passing a fraction is deprecated. </param>
        /// <param name="prefixLength"> Length of required common prefix. Default value is 0. </param>
        /// <exception cref="IOException"> if there is a low-level IO error </exception>
        public FuzzyTermsEnum(Terms terms, AttributeSource atts, Term term, float minSimilarity, int prefixLength, bool transpositions)
        {
            if (!InstanceFieldsInitialized)
            {
                InitializeInstanceFields();
                InstanceFieldsInitialized = true;
            }
            if (minSimilarity >= 1.0f && minSimilarity != (int)minSimilarity)
            {
                throw new System.ArgumentException("fractional edit distances are not allowed");
            }
            if (minSimilarity < 0.0f)
            {
                throw new System.ArgumentException("minimumSimilarity cannot be less than 0");
            }
            if (prefixLength < 0)
            {
                throw new System.ArgumentException("prefixLength cannot be less than 0");
            }
            this.Terms = terms;
            this.Term_Renamed = term;

            // convert the string into a utf32 int[] representation for fast comparisons
            string utf16 = term.Text();
            //LUCENE TO-DO
            //this.TermText = new int[utf16.codePointCount(0, utf16.Length)];
            this.TermText = new int[utf16.Length];
            for (int cp, i = 0, j = 0; i < utf16.Length; i += Character.CharCount(cp))
            {
                TermText[j++] = cp = Character.CodePointAt(utf16, i);
            }
            this.TermLength = TermText.Length;
            this.DfaAtt = atts.AddAttribute<ILevenshteinAutomataAttribute>();

            //The prefix could be longer than the word.
            //It's kind of silly though.  It means we must match the entire word.
            this.RealPrefixLength = prefixLength > TermLength ? TermLength : prefixLength;
            // if minSimilarity >= 1, we treat it as number of edits
            if (minSimilarity >= 1f)
            {
                this.MinSimilarity_Renamed = 0; // just driven by number of edits
                MaxEdits = (int)minSimilarity;
                Raw = true;
            }
            else
            {
                this.MinSimilarity_Renamed = minSimilarity;
                // calculate the maximum k edits for this similarity
                MaxEdits = InitialMaxDistance(this.MinSimilarity_Renamed, TermLength);
                Raw = false;
            }
            if (transpositions && MaxEdits > LevenshteinAutomata.MAXIMUM_SUPPORTED_DISTANCE)
            {
                throw new System.NotSupportedException("with transpositions enabled, distances > " + LevenshteinAutomata.MAXIMUM_SUPPORTED_DISTANCE + " are not supported ");
            }
            this.Transpositions = transpositions;
            this.Scale_factor = 1.0f / (1.0f - this.MinSimilarity_Renamed);

            this.MaxBoostAtt = atts.AddAttribute<IMaxNonCompetitiveBoostAttribute>();
            Bottom = MaxBoostAtt.MaxNonCompetitiveBoost;
            BottomTerm = MaxBoostAtt.CompetitiveTerm;
            BottomChanged(null, true);
        }
Exemplo n.º 19
0
 /// <summary>
 /// <para>Get the next token from the input stream.
 /// </para>
 /// <para>If the next token has <code>positionIncrement > 1</code>,
 /// <code>positionIncrement - 1</code> <seealso cref="#fillerToken"/>s are
 /// inserted first.
 /// </para>
 /// </summary>
 /// <param name="target"> Where to put the new token; if null, a new instance is created. </param>
 /// <returns> On success, the populated token; null otherwise </returns>
 /// <exception cref="IOException"> if the input stream has a problem </exception>
 private InputWindowToken GetNextToken(InputWindowToken target)
 {
     InputWindowToken newTarget = target;
     if (numFillerTokensToInsert > 0)
     {
         if (null == target)
         {
             newTarget = new InputWindowToken(this, nextInputStreamToken.CloneAttributes());
         }
         else
         {
             nextInputStreamToken.CopyTo(target.attSource);
         }
         // A filler token occupies no space
         newTarget.offsetAtt.SetOffset(newTarget.offsetAtt.StartOffset(), newTarget.offsetAtt.StartOffset());
         newTarget.termAtt.CopyBuffer(fillerToken, 0, fillerToken.Length);
         newTarget.isFiller = true;
         --numFillerTokensToInsert;
     }
     else if (isNextInputStreamToken)
     {
         if (null == target)
         {
             newTarget = new InputWindowToken(this, nextInputStreamToken.CloneAttributes());
         }
         else
         {
             nextInputStreamToken.CopyTo(target.attSource);
         }
         isNextInputStreamToken = false;
         newTarget.isFiller = false;
     }
     else if (!exhausted)
     {
         if (input.IncrementToken())
         {
             if (null == target)
             {
                 newTarget = new InputWindowToken(this, CloneAttributes());
             }
             else
             {
                 this.CopyTo(target.attSource);
             }
             if (posIncrAtt.PositionIncrement > 1)
             {
                 // Each output shingle must contain at least one input token, 
                 // so no more than (maxShingleSize - 1) filler tokens will be inserted.
                 numFillerTokensToInsert = Math.Min(posIncrAtt.PositionIncrement - 1, maxShingleSize - 1);
                 // Save the current token as the next input stream token
                 if (null == nextInputStreamToken)
                 {
                     nextInputStreamToken = CloneAttributes();
                 }
                 else
                 {
                     this.CopyTo(nextInputStreamToken);
                 }
                 isNextInputStreamToken = true;
                 // A filler token occupies no space
                 newTarget.offsetAtt.SetOffset(offsetAtt.StartOffset(), offsetAtt.StartOffset());
                 newTarget.termAtt.CopyBuffer(fillerToken, 0, fillerToken.Length);
                 newTarget.isFiller = true;
                 --numFillerTokensToInsert;
             }
             else
             {
                 newTarget.isFiller = false;
             }
         }
         else
         {
             exhausted = true;
             input.End();
             endState = CaptureState();
             numFillerTokensToInsert = Math.Min(posIncrAtt.PositionIncrement, maxShingleSize - 1);
             if (numFillerTokensToInsert > 0)
             {
                 // LUCENENET TODO: Property attributeFactory should begin with uppre case character
                 nextInputStreamToken = new AttributeSource(this.attributeFactory);
                 nextInputStreamToken.AddAttribute<ICharTermAttribute>();
                 IOffsetAttribute newOffsetAtt = nextInputStreamToken.AddAttribute<IOffsetAttribute>();
                 newOffsetAtt.SetOffset(offsetAtt.EndOffset(), offsetAtt.EndOffset());
                 // Recurse/loop just once:
                 return GetNextToken(target);
             }
             else
             {
                 newTarget = null;
             }
         }
     }
     else
     {
         newTarget = null;
     }
     return newTarget;
 }
Exemplo n.º 20
0
        public virtual void TestDefaultAttributeFactory()
        {
            AttributeSource src = new AttributeSource();

            Assert.IsTrue(src.AddAttribute<ICharTermAttribute>() is CharTermAttribute, "CharTermAttribute is not implemented by CharTermAttributeImpl");
            Assert.IsTrue(src.AddAttribute<IOffsetAttribute>() is OffsetAttribute, "OffsetAttribute is not implemented by OffsetAttributeImpl");
            Assert.IsTrue(src.AddAttribute<IFlagsAttribute>() is FlagsAttribute, "FlagsAttribute is not implemented by FlagsAttributeImpl");
            Assert.IsTrue(src.AddAttribute<IPayloadAttribute>() is PayloadAttribute, "PayloadAttribute is not implemented by PayloadAttributeImpl");
            Assert.IsTrue(src.AddAttribute<IPositionIncrementAttribute>() is PositionIncrementAttribute, "PositionIncrementAttribute is not implemented by PositionIncrementAttributeImpl");
            Assert.IsTrue(src.AddAttribute<ITypeAttribute>() is TypeAttribute, "TypeAttribute is not implemented by TypeAttributeImpl");
        }
Exemplo n.º 21
0
        public virtual void TestInvalidArguments()
        {
            try
            {
                AttributeSource src = new AttributeSource();
                src.AddAttribute<Token>();
                Assert.Fail("Should throw IllegalArgumentException");
            }
            catch (System.ArgumentException iae)
            {
            }

            try
            {
                AttributeSource src = new AttributeSource(Token.TOKEN_ATTRIBUTE_FACTORY);
                src.AddAttribute<Token>();
                Assert.Fail("Should throw IllegalArgumentException");
            }
            catch (System.ArgumentException iae)
            {
            }

            /*try
            {
              AttributeSource src = new AttributeSource();
              // break this by unsafe cast
              src.AddAttribute<typeof((Type)IEnumerator)>();
              Assert.Fail("Should throw IllegalArgumentException");
            }
            catch (System.ArgumentException iae)
            {
            }*/
        }
Exemplo n.º 22
0
 public virtual void TestLUCENE_3042()
 {
     AttributeSource src1 = new AttributeSource();
     src1.AddAttribute<ICharTermAttribute>().Append("foo");
     int hash1 = src1.GetHashCode(); // this triggers a cached state
     AttributeSource src2 = new AttributeSource(src1);
     src2.AddAttribute<ITypeAttribute>().Type = "bar";
     Assert.IsTrue(hash1 != src1.GetHashCode(), "The hashCode is identical, so the captured state was preserved.");
     Assert.AreEqual(src2.GetHashCode(), src1.GetHashCode());
 }
Exemplo n.º 23
0
        public virtual void TestCloneAttributes()
        {
            AttributeSource src = new AttributeSource();
            IFlagsAttribute flagsAtt = src.AddAttribute<IFlagsAttribute>();
            ITypeAttribute typeAtt = src.AddAttribute<ITypeAttribute>();
            flagsAtt.Flags = 1234;
            typeAtt.Type = "TestType";

            AttributeSource clone = src.CloneAttributes();
            IEnumerator<Type> it = clone.AttributeClassesIterator;
            it.MoveNext();
            Assert.AreEqual(typeof(IFlagsAttribute), it.Current, "FlagsAttribute must be the first attribute");
            it.MoveNext();
            Assert.AreEqual(typeof(ITypeAttribute), it.Current, "TypeAttribute must be the second attribute");
            Assert.IsFalse(it.MoveNext(), "No more attributes");

            IFlagsAttribute flagsAtt2 = clone.GetAttribute<IFlagsAttribute>();
            ITypeAttribute typeAtt2 = clone.GetAttribute<ITypeAttribute>();
            Assert.AreNotSame(flagsAtt2, flagsAtt, "FlagsAttribute of original and clone must be different instances");
            Assert.AreNotSame(typeAtt2, typeAtt, "TypeAttribute of original and clone must be different instances");
            Assert.AreEqual(flagsAtt2, flagsAtt, "FlagsAttribute of original and clone must be equal");
            Assert.AreEqual(typeAtt2, typeAtt, "TypeAttribute of original and clone must be equal");

            // test copy back
            flagsAtt2.Flags = 4711;
            typeAtt2.Type = "OtherType";
            clone.CopyTo(src);
            Assert.AreEqual(4711, flagsAtt.Flags, "FlagsAttribute of original must now contain updated term");
            Assert.AreEqual(typeAtt.Type, "OtherType", "TypeAttribute of original must now contain updated type");
            // verify again:
            Assert.AreNotSame(flagsAtt2, flagsAtt, "FlagsAttribute of original and clone must be different instances");
            Assert.AreNotSame(typeAtt2, typeAtt, "TypeAttribute of original and clone must be different instances");
            Assert.AreEqual(flagsAtt2, flagsAtt, "FlagsAttribute of original and clone must be equal");
            Assert.AreEqual(typeAtt2, typeAtt, "TypeAttribute of original and clone must be equal");
        }
Exemplo n.º 24
0
        public virtual void TestToStringAndMultiAttributeImplementations()
        {
            AttributeSource src = new AttributeSource();
            TermAttribute termAtt = (TermAttribute)src.AddAttribute(typeof(TermAttribute));
            TypeAttribute typeAtt = (TypeAttribute)src.AddAttribute(typeof(TypeAttribute));
            termAtt.SetTermBuffer("TestTerm");
            typeAtt.SetType("TestType");
            Assert.AreEqual("(" + termAtt.ToString() + "," + typeAtt.ToString() + ")", src.ToString(), "Attributes should appear in original order");
            System.Collections.Generic.IEnumerator<AttributeImpl> it = src.GetAttributeImplsIterator().GetEnumerator();
            Assert.IsTrue(it.MoveNext(), "Iterator should have 2 attributes left");
            Assert.AreSame(termAtt, it.Current, "First AttributeImpl from iterator should be termAtt");
            Assert.IsTrue(it.MoveNext(), "Iterator should have 1 attributes left");
            Assert.AreSame(typeAtt, it.Current, "Second AttributeImpl from iterator should be typeAtt");
            Assert.IsFalse(it.MoveNext(), "Iterator should have 0 attributes left");

            src = new AttributeSource();
            src.AddAttributeImpl(new Token());
            // this should not add a new attribute as Token implements TermAttribute, too
            termAtt = (TermAttribute)src.AddAttribute(typeof(TermAttribute));
            Assert.IsTrue(termAtt is Token, "TermAttribute should be implemented by Token");
            // get the Token attribute and check, that it is the only one
            it = src.GetAttributeImplsIterator().GetEnumerator();
            Assert.IsTrue(it.MoveNext());
            Token tok = (Token)it.Current;
            Assert.IsFalse(it.MoveNext(), "There should be only one attribute implementation instance");

            termAtt.SetTermBuffer("TestTerm");
            Assert.AreEqual("(" + tok.ToString() + ")", src.ToString(), "Token should only printed once");
        }
Exemplo n.º 25
0
        public void TestInvalidArguments()
        {
            try
            {
                AttributeSource src = new AttributeSource();
                src.AddAttribute(typeof(Token));
                Assert.Fail("Should throw IllegalArgumentException");
            }
            catch (ArgumentException iae) { }

            try
            {
                AttributeSource src = new AttributeSource();
                src.AddAttribute(typeof(System.Collections.IEnumerator));
                Assert.Fail("Should throw IllegalArgumentException");
            }
            catch (ArgumentException iae) { }
        }
        public override bool Accept(AttributeSource source)
        {
            if (termAtt == null)
            {
                termAtt = source.AddAttribute<ICharTermAttribute>();
            }

            DateTime date; //We don't care about the date, just that we can parse it as a date
            if (formats == null)
            {
                return DateTime.TryParse(termAtt.ToString(), culture, style, out date);
            }
            else
            {
                return DateTime.TryParseExact(termAtt.ToString(), formats, culture, style, out date);
            }
        }
Exemplo n.º 27
0
        public virtual void TestCloneAttributes()
        {
            AttributeSource src = new AttributeSource();
            TermAttribute termAtt = (TermAttribute)src.AddAttribute(typeof(TermAttribute));
            TypeAttribute typeAtt = (TypeAttribute)src.AddAttribute(typeof(TypeAttribute));
            termAtt.SetTermBuffer("TestTerm");
            typeAtt.SetType("TestType");

            AttributeSource clone = src.CloneAttributes();
            System.Collections.IEnumerator it = clone.GetAttributeClassesIterator().GetEnumerator();
            Assert.IsTrue(it.MoveNext());
            Assert.AreEqual(typeof(TermAttribute), it.Current, "TermAttribute must be the first attribute");
            Assert.IsTrue(it.MoveNext());
            Assert.AreEqual(typeof(TypeAttribute), it.Current, "TypeAttribute must be the second attribute");
            Assert.IsFalse(it.MoveNext(), "No more attributes");

            TermAttribute termAtt2 = (TermAttribute)clone.GetAttribute(typeof(TermAttribute));
            TypeAttribute typeAtt2 = (TypeAttribute)clone.GetAttribute(typeof(TypeAttribute));
            Assert.IsFalse(ReferenceEquals(termAtt2, termAtt), "TermAttribute of original and clone must be different instances");
            Assert.IsFalse(ReferenceEquals(typeAtt2, typeAtt), "TypeAttribute of original and clone must be different instances");
            Assert.AreEqual(termAtt2, termAtt, "TermAttribute of original and clone must be equal");
            Assert.AreEqual(typeAtt2, typeAtt, "TypeAttribute of original and clone must be equal");
        }
Exemplo n.º 28
0
        public void TestInvalidArguments()
        {
            var src = new AttributeSource();
            Assert.Throws<ArgumentException>(() => src.AddAttribute<Token>(), "Should throw ArgumentException");

            src = new AttributeSource();
            Assert.Throws<ArgumentException>(() => src.AddAttribute<Token>(), "Should throw ArgumentException");

            //try
            //{
            //    AttributeSource src = new AttributeSource();
            //    src.AddAttribute<System.Collections.IEnumerator>(); //Doesn't compile.
            //    Assert.Fail("Should throw IllegalArgumentException");
            //}
            //catch (ArgumentException iae) { }
        }