Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use J2N methods instead of StringBuilder.Remove, #664 #1132

Draft
wants to merge 1 commit into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
41 changes: 21 additions & 20 deletions src/Lucene.Net.Analysis.Common/Analysis/De/GermanStemmer.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
// Lucene version compatibility level 4.8.1

using J2N.Text;
using System;
using System.Globalization;
using System.Text;
Expand All @@ -25,7 +27,7 @@ namespace Lucene.Net.Analysis.De
*/

/// <summary>
/// A stemmer for German words.
/// A stemmer for German words.
/// <para>
/// The algorithm is based on the report
/// "A Fast and Simple Stemming Algorithm for German Words" by Jörg
Expand Down Expand Up @@ -60,8 +62,7 @@ protected internal virtual string Stem(string term)
return term;
}
// Reset the StringBuilder.
sb.Remove(0, sb.Length);
sb.Insert(0, term);
sb.Replace(0, sb.Length, term);
// Stemming starts here...
Substitute(sb);
Strip(sb);
Expand Down Expand Up @@ -102,32 +103,32 @@ private void Strip(StringBuilder buffer)
{
if ((buffer.Length + substCount > 5) && buffer.ToString(buffer.Length - 2, buffer.Length - (buffer.Length - 2)).Equals("nd", StringComparison.Ordinal))
{
buffer.Remove(buffer.Length - 2, buffer.Length - (buffer.Length - 2));
buffer.Delete(buffer.Length - 2, buffer.Length - (buffer.Length - 2));
}
else if ((buffer.Length + substCount > 4) && buffer.ToString(buffer.Length - 2, buffer.Length - (buffer.Length - 2)).Equals("em", StringComparison.Ordinal))
{
buffer.Remove(buffer.Length - 2, buffer.Length - (buffer.Length - 2));
buffer.Delete(buffer.Length - 2, buffer.Length - (buffer.Length - 2));
}
else if ((buffer.Length + substCount > 4) && buffer.ToString(buffer.Length - 2, buffer.Length - (buffer.Length - 2)).Equals("er", StringComparison.Ordinal))
{
buffer.Remove(buffer.Length - 2, buffer.Length - (buffer.Length - 2));
buffer.Delete(buffer.Length - 2, buffer.Length - (buffer.Length - 2));
}
else if (buffer[buffer.Length - 1] == 'e')
{
buffer.Remove(buffer.Length - 1, 1);
buffer.Delete(buffer.Length - 1, 1);
}
else if (buffer[buffer.Length - 1] == 's')
{
buffer.Remove(buffer.Length - 1, 1);
buffer.Delete(buffer.Length - 1, 1);
}
else if (buffer[buffer.Length - 1] == 'n')
{
buffer.Remove(buffer.Length - 1, 1);
buffer.Delete(buffer.Length - 1, 1);
}
// "t" occurs only as suffix of verbs.
else if (buffer[buffer.Length - 1] == 't')
{
buffer.Remove(buffer.Length - 1, 1);
buffer.Delete(buffer.Length - 1, 1);
}
else
{
Expand All @@ -145,7 +146,7 @@ private void Optimize(StringBuilder buffer)
// Additional step for female plurals of professions and inhabitants.
if (buffer.Length > 5 && buffer.ToString(buffer.Length - 5, buffer.Length - (buffer.Length - 5)).Equals("erin*", StringComparison.Ordinal))
{
buffer.Remove(buffer.Length - 1, 1);
buffer.Delete(buffer.Length - 1, 1);
Strip(buffer);
}
// Additional step for irregular plural nouns like "Matrizen -> Matrix".
Expand All @@ -167,7 +168,7 @@ private static void RemoveParticleDenotion(StringBuilder buffer) // LUCENENET: C
{
if (buffer.ToString(c, 4).Equals("gege", StringComparison.Ordinal))
{
buffer.Remove(c, (c + 2) - c);
buffer.Delete(c, (c + 2) - c);
return;
}
}
Expand All @@ -176,7 +177,7 @@ private static void RemoveParticleDenotion(StringBuilder buffer) // LUCENENET: C

/// <summary>
/// Do some substitutions for the term to reduce overstemming:
///
///
/// <list type="bullet">
/// <item><description>Substitute Umlauts with their corresponding vowel: äöü -> aou,
/// "ß" is substituted by "ss"</description></item>
Expand Down Expand Up @@ -223,37 +224,37 @@ private void Substitute(StringBuilder buffer)
if ((c < buffer.Length - 2) && buffer[c] == 's' && buffer[c + 1] == 'c' && buffer[c + 2] == 'h')
{
buffer[c] = '$';
buffer.Remove(c + 1, (c + 3) - (c + 1));
buffer.Delete(c + 1, (c + 3) - (c + 1));
substCount = +2;
}
else if (buffer[c] == 'c' && buffer[c + 1] == 'h')
{
buffer[c] = '§';
buffer.Remove(c + 1, 1);
buffer.Delete(c + 1, 1);
substCount++;
}
else if (buffer[c] == 'e' && buffer[c + 1] == 'i')
{
buffer[c] = '%';
buffer.Remove(c + 1, 1);
buffer.Delete(c + 1, 1);
substCount++;
}
else if (buffer[c] == 'i' && buffer[c + 1] == 'e')
{
buffer[c] = '&';
buffer.Remove(c + 1, 1);
buffer.Delete(c + 1, 1);
substCount++;
}
else if (buffer[c] == 'i' && buffer[c + 1] == 'g')
{
buffer[c] = '#';
buffer.Remove(c + 1, 1);
buffer.Delete(c + 1, 1);
substCount++;
}
else if (buffer[c] == 's' && buffer[c + 1] == 't')
{
buffer[c] = '!';
buffer.Remove(c + 1, 1);
buffer.Delete(c + 1, 1);
substCount++;
}
}
Expand Down Expand Up @@ -307,4 +308,4 @@ private static void Resubstitute(StringBuilder buffer) // LUCENENET: CA1822: Mar
}
}
}
}
}
36 changes: 18 additions & 18 deletions src/Lucene.Net.Analysis.Common/Analysis/Fr/FrenchStemmer.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
// Lucene version compatibility level 4.8.1

using J2N.Text;
using System;
using System.Globalization;
using System.Text;
Expand All @@ -23,14 +25,14 @@ namespace Lucene.Net.Analysis.Fr
*/

/// <summary>
/// A stemmer for French words.
/// A stemmer for French words.
/// <para/>
/// The algorithm is based on the work of
/// Dr Martin Porter on his snowball project<para/>
/// refer to http://snowball.sourceforge.net/french/stemmer.html
/// (French stemming algorithm) for details
/// </summary>
/// @deprecated Use <see cref="Tartarus.Snowball.Ext.FrenchStemmer"/> instead,
/// @deprecated Use <see cref="Tartarus.Snowball.Ext.FrenchStemmer"/> instead,
/// which has the same functionality. This filter will be removed in Lucene 4.0
[Obsolete("Use FrenchStemmer instead, which has the same functionality.")]
public class FrenchStemmer
Expand Down Expand Up @@ -102,8 +104,7 @@ protected internal virtual string Stem(string term)
term = locale.TextInfo.ToLower(term);

// Reset the StringBuilder.
sb.Remove(0, sb.Length);
sb.Insert(0, term);
sb.Replace(0, sb.Length, term);

// reset the booleans
modified = false;
Expand Down Expand Up @@ -155,8 +156,7 @@ private void SetStrings()
R1 = RetrieveR(sb);
if (R1 != null)
{
tb.Remove(0, tb.Length);
tb.Insert(0, R1);
tb.Replace(0, tb.Length, R1);
R2 = RetrieveR(tb);
}
else
Expand Down Expand Up @@ -291,7 +291,7 @@ private void Step4()
char b = sb[sb.Length - 2];
if (b != 'a' && b != 'i' && b != 'o' && b != 'u' && b != 'è' && b != 's')
{
sb.Remove(sb.Length - 1, sb.Length - (sb.Length - 1));
sb.Delete(sb.Length - 1, sb.Length - (sb.Length - 1));
SetStrings();
}
}
Expand All @@ -317,7 +317,7 @@ private void Step5()
{
if (R0.EndsWith("enn", StringComparison.Ordinal) || R0.EndsWith("onn", StringComparison.Ordinal) || R0.EndsWith("ett", StringComparison.Ordinal) || R0.EndsWith("ell", StringComparison.Ordinal) || R0.EndsWith("eill", StringComparison.Ordinal))
{
sb.Remove(sb.Length - 1, sb.Length - (sb.Length - 1));
sb.Delete(sb.Length - 1, sb.Length - (sb.Length - 1));
SetStrings();
}
}
Expand Down Expand Up @@ -387,7 +387,7 @@ private bool DeleteFromIfPrecededIn(string source, string[] search, string from,
{
if (from != null && from.EndsWith(prefix + search[i], StringComparison.Ordinal))
{
sb.Remove(sb.Length - search[i].Length, sb.Length - (sb.Length - search[i].Length));
sb.Delete(sb.Length - search[i].Length, sb.Length - (sb.Length - search[i].Length));
found = true;
SetStrings();
break;
Expand Down Expand Up @@ -420,7 +420,7 @@ private bool DeleteFromIfTestVowelBeforeIn(string source, string[] search, bool
bool test = IsVowel(sb[sb.Length - (search[i].Length + 1)]);
if (test == vowel)
{
sb.Remove(sb.Length - search[i].Length, sb.Length - (sb.Length - search[i].Length));
sb.Delete(sb.Length - search[i].Length, sb.Length - (sb.Length - search[i].Length));
modified = true;
found = true;
SetStrings();
Expand Down Expand Up @@ -448,14 +448,14 @@ private void DeleteButSuffixFrom(string source, string[] search, string prefix,
{
if (source.EndsWith(prefix + search[i], StringComparison.Ordinal))
{
sb.Remove(sb.Length - (prefix.Length + search[i].Length), sb.Length - (sb.Length - (prefix.Length + search[i].Length)));
sb.Delete(sb.Length - (prefix.Length + search[i].Length), sb.Length - (sb.Length - (prefix.Length + search[i].Length)));
modified = true;
SetStrings();
break;
}
else if (without && source.EndsWith(search[i], StringComparison.Ordinal))
{
sb.Remove(sb.Length - search[i].Length, sb.Length - (sb.Length - search[i].Length));
sb.Delete(sb.Length - search[i].Length, sb.Length - (sb.Length - search[i].Length));
modified = true;
SetStrings();
break;
Expand Down Expand Up @@ -483,21 +483,21 @@ private void DeleteButSuffixFromElseReplace(string source, string[] search, stri
{
if (source.EndsWith(prefix + search[i], StringComparison.Ordinal))
{
sb.Remove(sb.Length - (prefix.Length + search[i].Length), sb.Length - (sb.Length - (prefix.Length + search[i].Length)));
sb.Delete(sb.Length - (prefix.Length + search[i].Length), sb.Length - (sb.Length - (prefix.Length + search[i].Length)));
modified = true;
SetStrings();
break;
}
else if (from != null && from.EndsWith(prefix + search[i], StringComparison.Ordinal))
{
sb.Remove(sb.Length - (prefix.Length + search[i].Length), sb.Length - (sb.Length - (prefix.Length + search[i].Length))).Insert(sb.Length - (prefix.Length + search[i].Length), replace);
sb.Replace(sb.Length - (prefix.Length + search[i].Length), sb.Length - (sb.Length - (prefix.Length + search[i].Length)), replace);
modified = true;
SetStrings();
break;
}
else if (without && source.EndsWith(search[i], StringComparison.Ordinal))
{
sb.Remove(sb.Length - search[i].Length, sb.Length - (sb.Length - search[i].Length));
sb.Delete(sb.Length - search[i].Length, sb.Length - (sb.Length - search[i].Length));
modified = true;
SetStrings();
break;
Expand All @@ -521,7 +521,7 @@ private bool ReplaceFrom(string source, string[] search, string replace)
{
if (source.EndsWith(search[i], StringComparison.Ordinal))
{
sb.Remove(sb.Length - search[i].Length, sb.Length - (sb.Length - search[i].Length)).Insert(sb.Length - search[i].Length, replace);
sb.Replace(sb.Length - search[i].Length, sb.Length - (sb.Length - search[i].Length), replace);
modified = true;
found = true;
SetStrings();
Expand All @@ -545,7 +545,7 @@ private void DeleteFrom(string source, string[] suffix)
{
if (source.EndsWith(suffix[i], StringComparison.Ordinal))
{
sb.Remove(sb.Length - suffix[i].Length, sb.Length - (sb.Length - suffix[i].Length));
sb.Delete(sb.Length - suffix[i].Length, sb.Length - (sb.Length - suffix[i].Length));
modified = true;
SetStrings();
break;
Expand Down Expand Up @@ -784,4 +784,4 @@ private bool IsStemmable(string term)
return true;
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -1450,8 +1450,7 @@ internal static void ApplyMappings(FST<CharsRef> fst, StringBuilder sb)

if (longestMatch >= 0)
{
sb.Remove(i, longestMatch + 1 - i);
sb.Insert(i, longestOutput);
sb.Replace(i, longestMatch + 1 - i, longestOutput!.ToString()); // LUCENENET: [!]: longestOutput is set if longestMatch >= 0
i += (longestOutput.Length - 1);
}
}
Expand Down
Loading
Loading