Chia sẻ

Hôm nay, csharpcanban.com sẽ giới thiệu với các bạn một số hàm spin nội dung tương đối chuẩn. Đây là những hàm tương đối đơn giản để có thể xây dựng ứng dụng spin content, cũng là những hàm spin nội dung mà csharpcanban.com đã sử dụng để xây dựng phần mềm SPINVIET miễn phí, tuy nhiên để có thể xây dựng những ứng dụng đa năng hơn thì các bạn cần phải chế biến thêm nhé.

Hàm thứ 1:

public string spintax_type_1(Random rnd, string str)
{
      // Lặp lại liên tục cho tới khi hết mẫu.
      string pattern = "{[^{}]*}";
      Match m = Regex.Match(str, pattern);
      while (m.Success)
      {
           // Lựa chọn các cụm từ ngẫu nhiên và thay thế.
           string seg = str.Substring(m.Index + 1, m.Length - 2);
           string[] choices = seg.Split('|');
           str = str.Substring(0, m.Index) + choices[rnd.Next(choices.Length)] + str.Substring(m.Index + m.Length);
           m = Regex.Match(str, pattern);
      }

      // Trả lại nội dung đã được spin.
      return str;
}

Cách sử dụng

static void Main(string[] args)
{
      Random rnd = new Random();
      string str = "{Hello|Hi} {World|People}! {C{#|++|}|Java} is an {awesome|amazing} language.";
      Console.WriteLine(spintax_type_1(rnd, str));
      Console.ReadLine();
}

Hàm thứ 2:

static int[] partIndices = new int[100];
static int[] depth = new int[100];
static char[] symbolsOfTextProcessed = new char[100000];
public static Random rand = new Random();
public static String SpinEvenMoreFaster(String text)
{
        int cur = SpinEvenMoreFasterInner(text, 0, text.Length, 0);
        return new String(symbolsOfTextProcessed, 0, cur);
}
public int SpinEvenMoreFasterInner(String text, int start, int end, int symbolIndex)
{
       int last = start;
       for (int i = start; i < end; i++)
       {
            if (text[i] == '{')
            {
                    int k = 1;
                    int j = i + 1;
                    int index = 0;
                    partIndices[0] = i;
                    depth[0] = 1;
                    for (; j < end && k > 0; j++)
                    {
                        if (text[j] == '{')
                            k++;
                        else if (text[j] == '}')
                            k--;
                        else if (text[j] == '|')
                        {
                            if (k == 1)
                            {
                                partIndices[++index] = j;
                                depth[index] = 1;
                            }
                            else
                                depth[index] = k;
                        }
                    }
                    if (k == 0)
                    {
                        partIndices[++index] = j - 1;
                        int part = rand.Next(index);
                        text.CopyTo(last, symbolsOfTextProcessed, symbolIndex, i - last);
                        symbolIndex += i - last;
                        if (depth[part] == 1)
                        {
                            text.CopyTo(partIndices[part] + 1,
                                         symbolsOfTextProcessed,
                                         symbolIndex,
                                         partIndices[part + 1] - partIndices[part] - 1);
                            symbolIndex += partIndices[part + 1] - partIndices[part] - 1;
                        }
                        else
                        {
                            symbolIndex = SpinEvenMoreFasterInner(text, partIndices[part] + 1,
                                          partIndices[part + 1], symbolIndex);
                    }
                    i = j - 1;
                    last = j;
               }
           }
      }
      text.CopyTo(last, symbolsOfTextProcessed, symbolIndex, end - last);
      return symbolIndex + end - last;
}

Cách sử dụng:

static void Main(string[] args)
{
      string str = "{Hello|Hi} {World|People}! {C{#|++|}|Java} is an {awesome|amazing} language.";
      Console.WriteLine(spintax_type_2(str));
      Console.ReadLine();
}

Hàm thứ 3:

public String spintax_type_3(String text)
{
    int i, j, e = -1;
    char[] curls = new char[] {'{', '}'};
    text += '~';

    do
    {
        i =  e;
        e = -1;
        while ((i = text.IndexOf('{', i+1)) != -1)
        {
            j = i;
            while ((j = text.IndexOfAny(curls, j+1)) != -1 && text[j] != '}')
            {
                if (e == -1) e = i;
                i = j;
            }
            if (j != -1)
            {
                parts = text.Substring(i+1, (j-1)-(i+1-1)).Split('|');
                text = text.Remove(i, j-(i-1)).Insert(i, parts[rand.Next(parts.Length)]);
            }
        }
    }
    while (e-- != -1);

    return text.Remove(text.Length-1);
}

Cách sử dụng:

static void Main(string[] args)
{
      string str = "Oh! {{I'm|You're} here!|How are you{ doing{|, {buddy|pal|guy}}|}?}";
      Console.WriteLine(spintax_type_3(str));
      Console.ReadLine();
}

Hãy vận dụng vào ứng dụng spintext của bạn để trải nghiệm nhé. Chúc các bạn thành công.


Chia sẻ

Trả lời

Email của bạn sẽ không được hiển thị công khai. Các trường bắt buộc được đánh dấu *