私は既存のアプリケーションからスペルチェッカーアプリケーションを開発していますが、コードのこの部分を変更するのを手伝ってくれる人はいますか?stackalloc
同等のものが存在しないため、ポインターやJavaに移植できません。まったく同じ機能を持つJavaメソッド。
public static unsafe double GNULevenstein(string word1, string word2)
{
// this algorithm normally computes un-normalized distance between two string.
fixed (char* word1Ptr = word1)
fixed (char* word2Ptr = word2)
{
char* pointerToWord1 = word1Ptr;
char* pointerToWord2 = word2Ptr;
/* skip equal start sequence, if any */
if (word1.Length >= word2.Length)
{
while (*pointerToWord1 == *pointerToWord2)
{
/* if we already used up one string,
* then the result is the length of the other */
if (*pointerToWord1 == '\0') break;
pointerToWord1++;
pointerToWord2++;
}
}
else // wordl < word2
{
while (*pointerToWord1 == *pointerToWord2)
{
/* if we already used up one string,
* then the result is the length of the other */
if (*pointerToWord2 == '\0') break;
pointerToWord1++;
pointerToWord2++;
}
}
/* length count #1*/
int len1 = word1.Length - (int)(pointerToWord1 - word1Ptr);
int len2 = word2.Length - (int)(pointerToWord2 - word2Ptr);
/* if we already used up one string, then
the result is the length of the other */
if (*pointerToWord1 == '\0')
return ExportResult( len2 , word1.Length,word2.Length , false);
if (*pointerToWord2 == '\0')
return ExportResult(len1, word1.Length, word2.Length, false);
/* length count #2*/
pointerToWord1 += len1;
pointerToWord2 += len2;
/* cut of equal tail sequence, if any */
while (*--pointerToWord1 == *--pointerToWord2)
{
len1--;
len2--;
}
/* reset pointers, adjust length */
pointerToWord1 -= len1++;
pointerToWord2 -= len2++;
/* possible dist to great? */
//if ((len1 - len2 >= 0 ? len1 - len2 : -(len1 - len2)) >= char.MaxValue) return 1;
if (Math.Abs(len1 - len2) >= char.MaxValue)
return ExportResult(1, false); // no similarity
char* tmp;
/* swap if l2 longer than l1 */
if (len1 < len2)
{
tmp = pointerToWord1;
pointerToWord1 = pointerToWord2;
pointerToWord2 = tmp;
len1 ^= len2;
len2 ^= len1;
len1 ^= len2;
}
/* fill initial row */
int i, j, n;
n = (*pointerToWord1 != *pointerToWord2) ? 1 : 0;
char* r = stackalloc char[len1 * 2];
char* p1, p2;
for (i = 0, p1 = r; i < len1; i++, *p1++ = (char)n++, p1++)
{ /*empty*/}
/* calc. rowwise */
for (j = 1; j < len2; j++)
{
/* init pointers and col#0 */
p1 = r + ((j & 1) == 0 ? 1 : 0);
p2 = r + (j & 1);
n = *p1 + 1;
*p2++ = (char)n; p2++;
pointerToWord2++;
/* foreach column */
for (i = 1; i < len1; i++)
{
if (*p1 < n) n = *p1 + (*(pointerToWord1 + i) != *pointerToWord2 ? 1 : 0); /* replace cheaper than delete? */
p1++;
if (*++p1 < n) n = *p1 + 1; /* insert cheaper then insert ? */
*p2++ = (char)n++; /* update field and cost for next col's delete */
p2++;
}
}
/* return result */
return ExportResult( n - 1, word1.Length, word2.Length, false);
}
}