Example #1
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="reader">Delivers terms.
        /// </param>
        /// <param name="term">Pattern term.
        /// </param>
        /// <param name="minSimilarity">Minimum required similarity for terms from the reader. Default value is 0.5f.
        /// </param>
        /// <param name="prefixLength">Length of required common prefix. Default value is 0.
        /// </param>
        /// <throws>  IOException </throws>
        public FuzzyTermEnum(IndexReader reader, Term term, float minSimilarity, int prefixLength) : base()
        {
            if (minSimilarity >= 1.0f)
            {
                throw new System.ArgumentException("minimumSimilarity cannot be greater than or equal to 1");
            }
            else 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.minimumSimilarity = minSimilarity;
            this.scale_factor      = 1.0f / (1.0f - minimumSimilarity);
            this.searchTerm        = term;
            this.field             = searchTerm.Field();

            //The prefix could be longer than the word.
            //It's kind of silly though.  It means we must match the entire word.
            int fullSearchTermLength = searchTerm.Text().Length;
            int realPrefixLength     = prefixLength > fullSearchTermLength?fullSearchTermLength:prefixLength;

            this.text   = searchTerm.Text().Substring(realPrefixLength);
            this.prefix = searchTerm.Text().Substring(0, (realPrefixLength) - (0));

            InitializeMaxDistances();
            this.d = InitDistanceArray();

            SetEnum(reader.Terms(new Term(searchTerm.Field(), prefix)));
        }
Example #2
0
        public /*protected internal*/ override bool TermCompare(Term term)
        {
            if (collator == null)
            {
                // Use Unicode code point ordering
                bool checkLower = false;
                if (!includeLower)
                {
                    // make adjustments to set to exclusive
                    checkLower = true;
                }
                if (term != null && (System.Object)term.Field() == (System.Object)field)
                {
                    // interned comparison
                    if (!checkLower || null == lowerTermText || String.CompareOrdinal(term.Text(), lowerTermText) > 0)
                    {
                        checkLower = false;
                        if (upperTermText != null)
                        {
                            int compare = String.CompareOrdinal(upperTermText, term.Text());

                            /*
                             * if beyond the upper term, or is exclusive and this is equal to
                             * the upper term, break out
                             */
                            if ((compare < 0) || (!includeUpper && compare == 0))
                            {
                                endEnum = true;
                                return(false);
                            }
                        }
                        return(true);
                    }
                }
                else
                {
                    // break
                    endEnum = true;
                    return(false);
                }
                return(false);
            }
            else
            {
                if (term != null && (System.Object)term.Field() == (System.Object)field)
                {
                    // interned comparison
                    if ((lowerTermText == null || (includeLower?collator.Compare(term.Text().ToString(), lowerTermText.ToString()) >= 0:collator.Compare(term.Text().ToString(), lowerTermText.ToString()) > 0)) && (upperTermText == null || (includeUpper?collator.Compare(term.Text().ToString(), upperTermText.ToString()) <= 0:collator.Compare(term.Text().ToString(), upperTermText.ToString()) < 0)))
                    {
                        return(true);
                    }
                    return(false);
                }
                endEnum = true;
                return(false);
            }
        }
Example #3
0
 /// <summary>Prints a user-readable version of this query. </summary>
 public override System.String ToString(System.String field)
 {
     System.Text.StringBuilder buffer = new System.Text.StringBuilder();
     if (!term.Field().Equals(field))
     {
         buffer.Append(term.Field());
         buffer.Append(":");
     }
     buffer.Append(term.Text());
     buffer.Append(ToStringUtils.Boost(GetBoost()));
     return(buffer.ToString());
 }
Example #4
0
 public override System.String ToString(System.String field)
 {
     System.Text.StringBuilder buffer = new System.Text.StringBuilder();
     if (!term.Field().Equals(field))
     {
         buffer.Append(term.Field());
         buffer.Append(":");
     }
     buffer.Append(term.Text());
     buffer.Append('~');
     buffer.Append(SupportClass.Single.ToString(minimumSimilarity));
     buffer.Append(ToStringUtils.Boost(GetBoost()));
     return(buffer.ToString());
 }
Example #5
0
            protected internal override System.Object CreateValue(IndexReader reader, Entry entryKey)
            {
                Entry entry = (Entry)entryKey;

                System.String  field      = entry.field;
                SortComparator comparator = (SortComparator)entry.custom;

                System.IComparable[] retArray = new System.IComparable[reader.MaxDoc()];
                TermDocs             termDocs = reader.TermDocs();
                TermEnum             termEnum = reader.Terms(new Term(field));

                try
                {
                    do
                    {
                        Term term = termEnum.Term();
                        if (term == null || (System.Object)term.Field() != (System.Object)field)
                        {
                            break;
                        }
                        System.IComparable termval = comparator.GetComparable(term.Text());
                        termDocs.Seek(termEnum);
                        while (termDocs.Next())
                        {
                            retArray[termDocs.Doc()] = termval;
                        }
                    }while (termEnum.Next());
                }
                finally
                {
                    termDocs.Close();
                    termEnum.Close();
                }
                return(retArray);
            }
Example #6
0
            protected internal override System.Object CreateValue(IndexReader reader, Entry entryKey)
            {
                System.String   field    = StringHelper.Intern((System.String)entryKey.field);
                System.String[] retArray = new System.String[reader.MaxDoc()];
                TermDocs        termDocs = reader.TermDocs();
                TermEnum        termEnum = reader.Terms(new Term(field));

                try
                {
                    do
                    {
                        Term term = termEnum.Term();
                        if (term == null || (System.Object)term.Field() != (System.Object)field)
                        {
                            break;
                        }
                        System.String termval = term.Text();
                        termDocs.Seek(termEnum);
                        while (termDocs.Next())
                        {
                            retArray[termDocs.Doc()] = termval;
                        }
                    }while (termEnum.Next());
                }
                finally
                {
                    termDocs.Close();
                    termEnum.Close();
                }
                return(retArray);
            }
Example #7
0
            protected internal override System.Object CreateValue(IndexReader reader, Entry entryKey)
            {
                Entry entry = (Entry)entryKey;

                System.String field  = entry.field;
                FloatParser   parser = (FloatParser)entry.custom;

                if (parser == null)
                {
                    try
                    {
                        return(wrapper.GetFloats(reader, field, Mono.Lucene.Net.Search.FieldCache_Fields.DEFAULT_FLOAT_PARSER));
                    }
                    catch (System.FormatException ne)
                    {
                        return(wrapper.GetFloats(reader, field, Mono.Lucene.Net.Search.FieldCache_Fields.NUMERIC_UTILS_FLOAT_PARSER));
                    }
                }
                float[]  retArray = null;
                TermDocs termDocs = reader.TermDocs();
                TermEnum termEnum = reader.Terms(new Term(field));

                try
                {
                    do
                    {
                        Term term = termEnum.Term();
                        if (term == null || (System.Object)term.Field() != (System.Object)field)
                        {
                            break;
                        }
                        float termval = parser.ParseFloat(term.Text());
                        if (retArray == null)
                        {
                            // late init
                            retArray = new float[reader.MaxDoc()];
                        }
                        termDocs.Seek(termEnum);
                        while (termDocs.Next())
                        {
                            retArray[termDocs.Doc()] = termval;
                        }
                    }while (termEnum.Next());
                }
                catch (StopFillCacheException stop)
                {
                }
                finally
                {
                    termDocs.Close();
                    termEnum.Close();
                }
                if (retArray == null)
                {
                    // no values
                    retArray = new float[reader.MaxDoc()];
                }
                return(retArray);
            }
Example #8
0
        /// <summary> Adds a term to the end of the query phrase.
        /// The relative position of the term within the phrase is specified explicitly.
        /// This allows e.g. phrases with more than one term at the same position
        /// or phrases with gaps (e.g. in connection with stopwords).
        ///
        /// </summary>
        /// <param name="term">
        /// </param>
        /// <param name="position">
        /// </param>
        public virtual void  Add(Term term, int position)
        {
            if (terms.Count == 0)
            {
                field = term.Field();
            }
            else if ((System.Object)term.Field() != (System.Object)field)
            {
                throw new System.ArgumentException("All phrase terms must be in the same field: " + term);
            }

            terms.Add(term);
            positions.Add((System.Int32)position);
            if (position > maxPosition)
            {
                maxPosition = position;
            }
        }
Example #9
0
 public /*protected internal*/ override bool TermCompare(Term term)
 {
     if ((System.Object)term.Field() == (System.Object)prefix.Field() && term.Text().StartsWith(prefix.Text()))
     {
         return(true);
     }
     endEnum = true;
     return(false);
 }
Example #10
0
            protected internal override System.Object CreateValue(IndexReader reader, Entry entryKey)
            {
                System.String field      = StringHelper.Intern((System.String)entryKey.field);
                TermEnum      enumerator = reader.Terms(new Term(field));

                try
                {
                    Term term = enumerator.Term();
                    if (term == null)
                    {
                        throw new System.SystemException("no terms in field " + field + " - cannot determine type");
                    }
                    System.Object ret = null;
                    if ((System.Object)term.Field() == (System.Object)field)
                    {
                        System.String termtext = term.Text().Trim();

                        try
                        {
                            System.Int32.Parse(termtext);
                            ret = wrapper.GetInts(reader, field);
                        }
                        catch (System.FormatException nfe1)
                        {
                            try
                            {
                                System.Int64.Parse(termtext);
                                ret = wrapper.GetLongs(reader, field);
                            }
                            catch (System.FormatException nfe2)
                            {
                                try
                                {
                                    SupportClass.Single.Parse(termtext);
                                    ret = wrapper.GetFloats(reader, field);
                                }
                                catch (System.FormatException nfe3)
                                {
                                    ret = wrapper.GetStringIndex(reader, field);
                                }
                            }
                        }
                    }
                    else
                    {
                        throw new System.SystemException("field \"" + field + "\" does not appear to be indexed");
                    }
                    return(ret);
                }
                finally
                {
                    enumerator.Close();
                }
            }
Example #11
0
 /// <summary> The termCompare method in FuzzyTermEnum uses Levenshtein distance to
 /// calculate the distance between the given term and the comparing term.
 /// </summary>
 public /*protected internal*/ override bool TermCompare(Term term)
 {
     if ((System.Object)field == (System.Object)term.Field() && term.Text().StartsWith(prefix))
     {
         System.String target = term.Text().Substring(prefix.Length);
         this.similarity = Similarity(target);
         return(similarity > minimumSimilarity);
     }
     endEnum = true;
     return(false);
 }
Example #12
0
 public /*protected internal*/ override bool TermCompare(Term term)
 {
     if ((System.Object)field == (System.Object)term.Field())
     {
         System.String searchText = term.Text();
         if (searchText.StartsWith(pre))
         {
             return(WildcardEquals(text, 0, searchText, preLen));
         }
     }
     endEnum = true;
     return(false);
 }
Example #13
0
		public /*protected internal*/ override bool TermCompare(Term term)
		{
			if ((System.Object) field == (System.Object) term.Field())
			{
				System.String searchText = term.Text();
				if (searchText.StartsWith(pre))
				{
					return WildcardEquals(text, 0, searchText, preLen);
				}
			}
			endEnum = true;
			return false;
		}
Example #14
0
		/// <summary> Creates a new <code>WildcardTermEnum</code>.
		/// <p/>
		/// After calling the constructor the enumeration is already pointing to the first 
		/// valid term if such a term exists.
		/// </summary>
		public WildcardTermEnum(IndexReader reader, Term term):base()
		{
			searchTerm = term;
			field = searchTerm.Field();
			System.String searchTermText = searchTerm.Text();
			
			int sidx = searchTermText.IndexOf((System.Char) WILDCARD_STRING);
			int cidx = searchTermText.IndexOf((System.Char) WILDCARD_CHAR);
			int idx = sidx;
			if (idx == - 1)
			{
				idx = cidx;
			}
			else if (cidx >= 0)
			{
				idx = System.Math.Min(idx, cidx);
			}
			pre = idx != - 1?searchTerm.Text().Substring(0, (idx) - (0)):"";
			
			preLen = pre.Length;
			text = searchTermText.Substring(preLen);
			SetEnum(reader.Terms(new Term(searchTerm.Field(), pre)));
		}
Example #15
0
        /// <summary>Constructs a query selecting all terms greater than
        /// <code>lowerTerm</code> but less than <code>upperTerm</code>.
        /// There must be at least one term and either term may be null,
        /// in which case there is no bound on that side, but if there are
        /// two terms, both terms <b>must</b> be for the same field.
        /// <p/>
        /// If <code>collator</code> is not null, it will be used to decide whether
        /// index terms are within the given range, rather than using the Unicode code
        /// point order in which index terms are stored.
        /// <p/>
        /// <strong>WARNING:</strong> Using this constructor and supplying a non-null
        /// value in the <code>collator</code> parameter will cause every single
        /// index Term in the Field referenced by lowerTerm and/or upperTerm to be
        /// examined.  Depending on the number of index Terms in this Field, the
        /// operation could be very slow.
        ///
        /// </summary>
        /// <param name="lowerTerm">The Term at the lower end of the range
        /// </param>
        /// <param name="upperTerm">The Term at the upper end of the range
        /// </param>
        /// <param name="inclusive">If true, both <code>lowerTerm</code> and
        /// <code>upperTerm</code> will themselves be included in the range.
        /// </param>
        /// <param name="collator">The collator to use to collate index Terms, to determine
        /// their membership in the range bounded by <code>lowerTerm</code> and
        /// <code>upperTerm</code>.
        /// </param>
        public RangeQuery(Term lowerTerm, Term upperTerm, bool inclusive, System.Globalization.CompareInfo collator)
        {
            if (lowerTerm == null && upperTerm == null)
            {
                throw new System.ArgumentException("At least one term must be non-null");
            }
            if (lowerTerm != null && upperTerm != null && (System.Object)lowerTerm.Field() != (System.Object)upperTerm.Field())
            {
                throw new System.ArgumentException("Both terms must have the same field");
            }

            delegate_Renamed = new TermRangeQuery((lowerTerm == null)?upperTerm.Field():lowerTerm.Field(), (lowerTerm == null)?null:lowerTerm.Text(), (upperTerm == null)?null:upperTerm.Text(), inclusive, inclusive, collator);
            delegate_Renamed.SetRewriteMethod(TermRangeQuery.SCORING_BOOLEAN_QUERY_REWRITE);
        }
Example #16
0
        /// <summary> Creates a new <code>WildcardTermEnum</code>.
        /// <p/>
        /// After calling the constructor the enumeration is already pointing to the first
        /// valid term if such a term exists.
        /// </summary>
        public WildcardTermEnum(IndexReader reader, Term term) : base()
        {
            searchTerm = term;
            field      = searchTerm.Field();
            System.String searchTermText = searchTerm.Text();

            int sidx = searchTermText.IndexOf((System.Char)WILDCARD_STRING);
            int cidx = searchTermText.IndexOf((System.Char)WILDCARD_CHAR);
            int idx  = sidx;

            if (idx == -1)
            {
                idx = cidx;
            }
            else if (cidx >= 0)
            {
                idx = System.Math.Min(idx, cidx);
            }
            pre = idx != -1?searchTerm.Text().Substring(0, (idx) - (0)):"";

            preLen = pre.Length;
            text   = searchTermText.Substring(preLen);
            SetEnum(reader.Terms(new Term(searchTerm.Field(), pre)));
        }
Example #17
0
        internal static int DetectFieldType(IndexReader reader, System.String fieldKey)
        {
            System.String field      = StringHelper.Intern(fieldKey);
            TermEnum      enumerator = reader.Terms(new Term(field));

            try
            {
                Term term = enumerator.Term();
                if (term == null)
                {
                    throw new System.SystemException("no terms in field " + field + " - cannot determine sort type");
                }
                int ret = 0;
                if ((System.Object)term.Field() == (System.Object)field)
                {
                    System.String termtext = term.Text().Trim();

                    int tmpI32; long tmpI64; float tmpF;
                    if (System.Int32.TryParse(termtext, out tmpI32))
                    {
                        ret = SortField.INT;
                    }
                    else if (System.Int64.TryParse(termtext, out tmpI64))
                    {
                        ret = SortField.LONG;
                    }
                    else if (SupportClass.Single.TryParse(termtext, out tmpF))
                    {
                        ret = SortField.FLOAT;
                    }
                    else
                    {
                        ret = SortField.STRING;
                    }
                }
                else
                {
                    throw new System.SystemException("field \"" + field + "\" does not appear to be indexed");
                }
                return(ret);
            }
            finally
            {
                enumerator.Close();
            }
        }
Example #18
0
            protected internal override System.Object CreateValue(IndexReader reader, Entry entryKey)
            {
                Entry entry = (Entry)entryKey;

                System.String field  = entry.field;
                ShortParser   parser = (ShortParser)entry.custom;

                if (parser == null)
                {
                    return(wrapper.GetShorts(reader, field, Mono.Lucene.Net.Search.FieldCache_Fields.DEFAULT_SHORT_PARSER));
                }
                short[]  retArray = new short[reader.MaxDoc()];
                TermDocs termDocs = reader.TermDocs();
                TermEnum termEnum = reader.Terms(new Term(field));

                try
                {
                    do
                    {
                        Term term = termEnum.Term();
                        if (term == null || (System.Object)term.Field() != (System.Object)field)
                        {
                            break;
                        }
                        short termval = parser.ParseShort(term.Text());
                        termDocs.Seek(termEnum);
                        while (termDocs.Next())
                        {
                            retArray[termDocs.Doc()] = termval;
                        }
                    }while (termEnum.Next());
                }
                catch (StopFillCacheException stop)
                {
                }
                finally
                {
                    termDocs.Close();
                    termEnum.Close();
                }
                return(retArray);
            }
Example #19
0
 /// <summary> Compares if current upper bound is reached,
 /// this also updates the term count for statistics.
 /// In contrast to {@link FilteredTermEnum}, a return value
 /// of <code>false</code> ends iterating the current enum
 /// and forwards to the next sub-range.
 /// </summary>
 //@Override
 public /*protected internal*/ override bool TermCompare(Term term)
 {
     return((System.Object)term.Field() == (System.Object)Enclosing_Instance.field && String.CompareOrdinal(term.Text(), currentUpperBound) <= 0);
 }
Example #20
0
 public override System.String GetField()
 {
     return(term.Field());
 }
Example #21
0
            protected internal override System.Object CreateValue(IndexReader reader, Entry entryKey)
            {
                System.String   field    = StringHelper.Intern((System.String)entryKey.field);
                int[]           retArray = new int[reader.MaxDoc()];
                System.String[] mterms   = new System.String[reader.MaxDoc() + 1];
                TermDocs        termDocs = reader.TermDocs();
                TermEnum        termEnum = reader.Terms(new Term(field));
                int             t        = 0; // current term number

                // an entry for documents that have no terms in this field
                // should a document with no terms be at top or bottom?
                // this puts them at the top - if it is changed, FieldDocSortedHitQueue
                // needs to change as well.
                mterms[t++] = null;

                try
                {
                    do
                    {
                        Term term = termEnum.Term();
                        if (term == null || term.Field() != field || t >= mterms.Length)
                        {
                            break;
                        }

                        // store term text
                        mterms[t] = term.Text();

                        termDocs.Seek(termEnum);
                        while (termDocs.Next())
                        {
                            retArray[termDocs.Doc()] = t;
                        }

                        t++;
                    }while (termEnum.Next());
                }
                finally
                {
                    termDocs.Close();
                    termEnum.Close();
                }

                if (t == 0)
                {
                    // if there are no terms, make the term array
                    // have a single null entry
                    mterms = new System.String[1];
                }
                else if (t < mterms.Length)
                {
                    // if there are less terms than documents,
                    // trim off the dead array space
                    System.String[] terms = new System.String[t];
                    Array.Copy(mterms, 0, terms, 0, t);
                    mterms = terms;
                }

                StringIndex value_Renamed = new StringIndex(retArray, mterms);

                return(value_Renamed);
            }
Example #22
0
		/// <summary>Constructs a query selecting all terms greater than
		/// <code>lowerTerm</code> but less than <code>upperTerm</code>.
		/// There must be at least one term and either term may be null,
		/// in which case there is no bound on that side, but if there are
		/// two terms, both terms <b>must</b> be for the same field.
		/// <p/>
		/// If <code>collator</code> is not null, it will be used to decide whether
		/// index terms are within the given range, rather than using the Unicode code
		/// point order in which index terms are stored.
		/// <p/>
		/// <strong>WARNING:</strong> Using this constructor and supplying a non-null
		/// value in the <code>collator</code> parameter will cause every single 
		/// index Term in the Field referenced by lowerTerm and/or upperTerm to be
		/// examined.  Depending on the number of index Terms in this Field, the 
		/// operation could be very slow.
		/// 
		/// </summary>
		/// <param name="lowerTerm">The Term at the lower end of the range
		/// </param>
		/// <param name="upperTerm">The Term at the upper end of the range
		/// </param>
		/// <param name="inclusive">If true, both <code>lowerTerm</code> and
		/// <code>upperTerm</code> will themselves be included in the range.
		/// </param>
		/// <param name="collator">The collator to use to collate index Terms, to determine
		/// their membership in the range bounded by <code>lowerTerm</code> and
		/// <code>upperTerm</code>.
		/// </param>
		public RangeQuery(Term lowerTerm, Term upperTerm, bool inclusive, System.Globalization.CompareInfo collator)
		{
			if (lowerTerm == null && upperTerm == null)
				throw new System.ArgumentException("At least one term must be non-null");
			if (lowerTerm != null && upperTerm != null && (System.Object) lowerTerm.Field() != (System.Object) upperTerm.Field())
				throw new System.ArgumentException("Both terms must have the same field");
			
			delegate_Renamed = new TermRangeQuery((lowerTerm == null)?upperTerm.Field():lowerTerm.Field(), (lowerTerm == null)?null:lowerTerm.Text(), (upperTerm == null)?null:upperTerm.Text(), inclusive, inclusive, collator);
			delegate_Renamed.SetRewriteMethod(TermRangeQuery.SCORING_BOOLEAN_QUERY_REWRITE);
		}
Example #23
0
		/// <summary> Adds a term to the end of the query phrase.
		/// The relative position of the term within the phrase is specified explicitly.
		/// This allows e.g. phrases with more than one term at the same position
		/// or phrases with gaps (e.g. in connection with stopwords).
		/// 
		/// </summary>
		/// <param name="term">
		/// </param>
		/// <param name="position">
		/// </param>
		public virtual void  Add(Term term, int position)
		{
			if (terms.Count == 0)
				field = term.Field();
			else if ((System.Object) term.Field() != (System.Object) field)
			{
				throw new System.ArgumentException("All phrase terms must be in the same field: " + term);
			}
			
			terms.Add(term);
			positions.Add((System.Int32) position);
			if (position > maxPosition)
				maxPosition = position;
		}
Example #24
0
        public PrefixTermEnum(IndexReader reader, Term prefix)
        {
            this.prefix = prefix;

            SetEnum(reader.Terms(new Term(prefix.Field(), prefix.Text())));
        }
Example #25
0
		public /*protected internal*/ override bool TermCompare(Term term)
		{
			if (collator == null)
			{
				// Use Unicode code point ordering
				bool checkLower = false;
				if (!includeLower)
				// make adjustments to set to exclusive
					checkLower = true;
				if (term != null && (System.Object) term.Field() == (System.Object) field)
				{
					// interned comparison
					if (!checkLower || null == lowerTermText || String.CompareOrdinal(term.Text(), lowerTermText) > 0)
					{
						checkLower = false;
						if (upperTermText != null)
						{
							int compare = String.CompareOrdinal(upperTermText, term.Text());
							/*
							* if beyond the upper term, or is exclusive and this is equal to
							* the upper term, break out
							*/
							if ((compare < 0) || (!includeUpper && compare == 0))
							{
								endEnum = true;
								return false;
							}
						}
						return true;
					}
				}
				else
				{
					// break
					endEnum = true;
					return false;
				}
				return false;
			}
			else
			{
				if (term != null && (System.Object) term.Field() == (System.Object) field)
				{
					// interned comparison
					if ((lowerTermText == null || (includeLower?collator.Compare(term.Text().ToString(), lowerTermText.ToString()) >= 0:collator.Compare(term.Text().ToString(), lowerTermText.ToString()) > 0)) && (upperTermText == null || (includeUpper?collator.Compare(term.Text().ToString(), upperTermText.ToString()) <= 0:collator.Compare(term.Text().ToString(), upperTermText.ToString()) < 0)))
					{
						return true;
					}
					return false;
				}
				endEnum = true;
				return false;
			}
		}
Example #26
0
		/// <summary> The termCompare method in FuzzyTermEnum uses Levenshtein distance to 
		/// calculate the distance between the given term and the comparing term. 
		/// </summary>
		public /*protected internal*/ override bool TermCompare(Term term)
		{
			if ((System.Object) field == (System.Object) term.Field() && term.Text().StartsWith(prefix))
			{
				System.String target = term.Text().Substring(prefix.Length);
				this.similarity = Similarity(target);
				return (similarity > minimumSimilarity);
			}
			endEnum = true;
			return false;
		}
Example #27
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="reader">Delivers terms.
		/// </param>
		/// <param name="term">Pattern term.
		/// </param>
		/// <param name="minSimilarity">Minimum required similarity for terms from the reader. Default value is 0.5f.
		/// </param>
		/// <param name="prefixLength">Length of required common prefix. Default value is 0.
		/// </param>
		/// <throws>  IOException </throws>
		public FuzzyTermEnum(IndexReader reader, Term term, float minSimilarity, int prefixLength):base()
		{
			
			if (minSimilarity >= 1.0f)
				throw new System.ArgumentException("minimumSimilarity cannot be greater than or equal to 1");
			else 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.minimumSimilarity = minSimilarity;
			this.scale_factor = 1.0f / (1.0f - minimumSimilarity);
			this.searchTerm = term;
			this.field = searchTerm.Field();
			
			//The prefix could be longer than the word.
			//It's kind of silly though.  It means we must match the entire word.
			int fullSearchTermLength = searchTerm.Text().Length;
			int realPrefixLength = prefixLength > fullSearchTermLength?fullSearchTermLength:prefixLength;
			
			this.text = searchTerm.Text().Substring(realPrefixLength);
			this.prefix = searchTerm.Text().Substring(0, (realPrefixLength) - (0));
			
			InitializeMaxDistances();
			this.d = InitDistanceArray();
			
			SetEnum(reader.Terms(new Term(searchTerm.Field(), prefix)));
		}