本文整理汇总了C#中Lucene.Net.Util.AttributeSource.AddAttribute方法的典型用法代码示例。如果您正苦于以下问题:C# AttributeSource.AddAttribute方法的具体用法?C# AttributeSource.AddAttribute怎么用?C# AttributeSource.AddAttribute使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类Lucene.Net.Util.AttributeSource
的用法示例。
在下文中一共展示了AttributeSource.AddAttribute方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: TestCaptureState
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
}
}
示例2: TestCaptureState
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
}
}
示例3: Accept
public override bool Accept(AttributeSource source)
{
if (typeAtt == null)
{
typeAtt = source.AddAttribute<ITypeAttribute>();
}
return typeToMatch.Equals(typeAtt.Type);
}
示例4: Accept
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));
}
示例5: Accept
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;
}
示例6: FuzzyTermsEnum
/// <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 >
/// <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);
}
示例7: Accept
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);
}
}
示例8: TestCloneAttributes
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");
}
示例9: TestInvalidArguments
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) { }
}
示例10: TestToStringAndMultiAttributeImplementations
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");
}
示例11: TestCloneAttributes
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");
}
示例12: TestLUCENE_3042
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());
}
示例13: TestInvalidArguments
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)
{
}*/
}
示例14: TestDefaultAttributeFactory
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");
}
示例15: GetNextToken
/// <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;
//.........这里部分代码省略.........