/**************************************************************************************
This script relates to the following SQL Server Central forum thread:
Simple CLR request ( http://www.sqlservercentral.com/Forums/Topic1795321-3412-1.aspx )
Written by: Solomon Rutzky
On: 2016-06-26
This script is located at: http://pastebin.com/Rd4G7T3B
***************************************************************************************/
using System; // Array, Object
using System.Collections; // IEnumerable
using System.Data.SqlTypes; // SqlChars, SqlInt32
using Microsoft.SqlServer.Server; // SqlFacet
public class NGrams
{
[Microsoft.SqlServer.Server.SqlFunction(FillRowMethodName = "NGramSolomon_FillRow",
TableDefinition = "Position INT, Token NVARCHAR(4000)")]
public static IEnumerable NGramSolomon([SqlFacet(MaxSize = 4000)] SqlChars InputString, SqlInt32 TokenSize)
{
if (InputString.IsNull || TokenSize.IsNull)
{
yield break;
}
int _Take = TokenSize.Value;
int _Limit = ((int)InputString.Length - _Take);
if (_Take < 1 || _Limit < 0)
{
yield break;
}
ResultRow _Result = new ResultRow();
char[] _Chars = InputString.Value;
char[] _Temp = new char[_Take];
for (int _Index = 0; _Index <= _Limit; _Index++)
{
Array.Copy(_Chars, _Index, _Temp, 0, _Take);
_Result.Position = (_Index + 1);
_Result.Token = new SqlChars(_Temp);
yield return _Result;
}
}
public class ResultRow
{
public int Position;
public SqlChars Token;
}
public static void NGramSolomon_FillRow(Object obj, out SqlInt32 position, out SqlChars token)
{
ResultRow _Item = (ResultRow)obj;
position = new SqlInt32(_Item.Position);
token = _Item.Token;
}
}
Comments