[FREE] Quick release that I wrote last night - article spinner [FREE]

Wolfpack

Junior Member
Joined
Jul 13, 2011
Messages
166
Reaction score
351
Been a while since I posted anything helpful/anything at all. Here's a small little program I wrote last night in C#, it spins articles using the spintax format, and supports multilevel spinning, i.e.

Code:
{this|is|{multi|level|spinning}|as|there's|more{than|one}|layer {of|curly}|brackets}
It's not exactly the most extensive program, but it's a quick put-together which I think a lot of people will want. Have fun, and report bugs here :)

PS: Don't know if it needs a virus check because it's only 7KB and archived, but if anybody wants to do a virus check and post the stats that'd be really helpful, no idea how to do it or what to use, as I've never uploaded a .exe

Aussi, here's a screenshot of it in action:
spinner.png


On request, here's the source code :)

Code:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Text.RegularExpressions;
using System.IO;

namespace WindowsFormsApplication36
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
            lblError.Visible = false;
        }

        private static readonly Random random = new Random();
        private static readonly object syncLock = new object();

        private void button1_Click(object sender, EventArgs e)
        {
            if (textBox1.Text.Split('{').Length == textBox1.Text.Split('}').Length)
            {
                string text = textBox1.Text;
                string carrier;
                MatchCollection md = Regex.Matches(text, "\\{[\\s\\S]*\\}*");
                foreach (Match d in md)
                {
                    string dstr = d.Value;
                    carrier = LongSpinner(dstr);
                    text = textBox2.Text = Regex.Replace(text, Regex.Escape(d.Value), carrier);
                }
                lblCharCount.Text = textBox2.Text.Length.ToString();
                string unique = Uniqueness(textBox1.Text);
                if (unique != "")
                {
                    lblUnique.Text = unique + "%";
                }
                else
                {
                    lblUnique.Text = "0.00%";
                }
                if (Combinations(textBox1.Text) > 1000000 | Combinations(textBox1.Text) < 0)
                {
                    lblCombination.Text = "1,000,000+";
                }
                else
                {
                    lblCombination.Text = Combinations(textBox1.Text).ToString();
                }
            }
            else
            {
                lblError.Visible = true;
            }
        }

        private void button2_Click(object sender, EventArgs e)
        {
            if (textBox1.Text.Split('{').Length == textBox1.Text.Split('}').Length)
            {
                string text = textBox1.Text;
                string carrier;
                MatchCollection md = Regex.Matches(text, "\\{[\\s\\S]*\\}*");
                Random rnd = new Random();
                foreach (Match d in md)
                {
                    string dstr = d.Value;
                    carrier = Spinner(dstr, rnd);
                    text = textBox2.Text = Regex.Replace(text, Regex.Escape(d.Value), carrier);
                }
                lblCharCount.Text = textBox2.Text.Length.ToString();
                string unique = Uniqueness(textBox1.Text);
                if (unique != "")
                {
                    lblUnique.Text = unique + "%";
                }
                else
                {
                    lblUnique.Text = "0.00%";
                }
                if (Combinations(textBox1.Text) > 1000000)
                {
                    lblCombination.Text = "1,000,000+";
                }
                else
                {
                    lblCombination.Text = Combinations(textBox1.Text).ToString();
                }
            }
            else
            {
                lblError.Visible = true;
            }
        }

        private void textBox1_TextChanged(object sender, EventArgs e)
        {
            lblError.Visible = false;
        }

        private void button7_Click(object sender, EventArgs e)
        {
            if (textBox1.Text.Split('{').Length == textBox1.Text.Split('}').Length)
            {
                string text = textBox1.Text;
                string carrier;
                MatchCollection md = Regex.Matches(text, "\\{[\\s\\S]*\\}*");
                Random rnd = new Random();
                foreach (Match d in md)
                {
                    string dstr = d.Value;
                    carrier = ShortSpinner(dstr);
                    text = textBox2.Text = Regex.Replace(text, Regex.Escape(d.Value), carrier);
                }
                lblCharCount.Text = textBox2.Text.Length.ToString();
                string unique = Uniqueness(textBox1.Text);
                if (unique != "")
                {
                    lblUnique.Text = unique + "%";
                }
                else
                {
                    lblUnique.Text = "0.00%";
                }
                if (Combinations(textBox1.Text) > 1000000)
                {
                    lblCombination.Text = "1,000,000+";
                }
                else
                {
                    lblCombination.Text = Combinations(textBox1.Text).ToString();
                }
            }
        }

        private string Spinner(string mstr, Random rdm)
        {
            string returnword;
            string[] arrSplit;
            int cnt = 0;
            while (Regex.IsMatch(mstr, "\\{"))
            {
                string innerspin = mstr.Split('{')[(mstr.Split('{').Length) - 1];
                string tospin = Regex.Match(innerspin, "[\\s\\S]*?(?=\\})").Value;

                arrSplit = tospin.Split('|');
                returnword = arrSplit[rdm.Next(0, arrSplit.Length)];
                cnt++;
                Console.WriteLine(cnt);

                mstr = Regex.Replace(mstr, "\\{" + Regex.Escape(tospin) + "\\}", returnword);
            }
            return mstr;
        }

        private string LongSpinner(string mstr)
        {
            string returnword;
            string[] arrSplit;
            int cnt = 0;
            while (Regex.IsMatch(mstr, "\\{"))
            {
                string innerspin = mstr.Split('{')[(mstr.Split('{').Length) - 1];
                string tospin = Regex.Match(innerspin, "[\\s\\S]*?(?=\\})").Value;
                int idx = 0;
                int x = 0;
                int longcount = 0;

                arrSplit = tospin.Split('|');
                foreach (string str in arrSplit)
                {
                    int length = str.Length;
                    if (length > longcount)
                    {
                        longcount = length;
                        idx = x;
                    }
                    x++;
                }
                returnword = arrSplit[idx];

                cnt++;
                Console.WriteLine(cnt);

                mstr = Regex.Replace(mstr, "\\{" + Regex.Escape(tospin) + "\\}", returnword);
            }
            return mstr;
        }

        private int Combinations(string text)
        {
            MatchCollection mc = Regex.Matches(text, "\\{[\\s\\S]*?\\}");

            int combno = 1;

            foreach (Match m in mc)
            {
                string[] arrMatch = m.Value.Split('|');
                combno *= arrMatch.Length;
                if (combno > 1000000)
                {
                    break;
                }
            }
            return combno;
        }


        private string Uniqueness(string text)
        {
            int spincount = 0;
            int allcount = 0;

            MatchCollection notspun = Regex.Matches(text, "\\{[\\s\\S]*?\\}");
            foreach (Match m in notspun)
            {
                spincount += m.Value.Length;
            }
            MatchCollection all = Regex.Matches(text, "[\\S]*");
            foreach (Match m in all)
            {
                allcount += m.Value.Length;
            }
            int nonspun = allcount - spincount;

            MatchCollection mc = Regex.Matches(text, "(?<=\\{)[\\s\\S]*?(?=\\})");
            decimal[] arrDoubles = new decimal[mc.Count];
            int x = 0;
            foreach (Match m in mc)
            {
                int averagelengthcnt = 0;
                string[] arrMatch = m.Value.Split('|');
                foreach (string s in arrMatch)
                {
                    averagelengthcnt += s.Length;
                }
                arrDoubles[x] = Decimal.Divide(averagelengthcnt, arrMatch.Length);
                x++;
            }
            return text = (Decimal.Divide(arrDoubles.Sum(), (arrDoubles.Sum() + nonspun)) * 100).ToString("#.##");
        }

        public static int RandomNumber(int min, int max)
        {
            lock (syncLock)
            {
                return random.Next(min, max);
            }
        }

        private string ShortSpinner(string mstr)
        {
            string returnword;
            string[] arrSplit;
            int cnt = 0;
            while (Regex.IsMatch(mstr, "\\{"))
            {
                string innerspin = mstr.Split('{')[(mstr.Split('{').Length) - 1];
                string tospin = Regex.Match(innerspin, "[\\s\\S]*?(?=\\})").Value;
                int idx = 0;
                int x = 0;
                int longcount = 0;
                int firstcnt = 0;

                arrSplit = tospin.Split('|');
                foreach (string str in arrSplit)
                {
                    int length = str.Length;
                    if (length < longcount | (longcount == 0 && firstcnt == 0))
                    {
                        longcount = length;
                        idx = x;
                        firstcnt++;
                    }
                    x++;
                }
                returnword = arrSplit[idx];

                cnt++;
                Console.WriteLine(cnt);

                mstr = Regex.Replace(mstr, "\\{" + Regex.Escape(tospin) + "\\}", returnword);
            }
            return mstr;
        }

        private void button5_Click(object sender, EventArgs e)
        {
            dlgSave.Filter = "Text Files|*.txt";
            DialogResult drUsr;
            drUsr = dlgSave.ShowDialog();
            if (drUsr == DialogResult.OK)
            {
                FileStream fs = new FileStream(dlgSave.FileName, FileMode.OpenOrCreate, FileAccess.Write, FileShare.ReadWrite);
                StreamWriter sw = new StreamWriter(fs, Encoding.ASCII);
                sw.Write(textBox1.Text);
                sw.Flush();
                sw.Close(); 
                fs.Close();
            }
        }

        private void button6_Click(object sender, EventArgs e)
        {
            textBox2.Text = "";
        }

        static System.Timers.Timer Time = new System.Timers.Timer(8000);
        static bool signaler = false;

        static void _timer_Elapsed(object sender, ElapsedEventArgs e)
        {
            signaler = true;
            Time.Enabled = false;
        }

        private void button3_Click(object sender, EventArgs e)
        {
            dlgOpen.Filter = "Text Files|*.txt";
            DialogResult drUsr;
            drUsr = dlgOpen.ShowDialog();
            if (drUsr == DialogResult.OK)
            {
                StreamReader sr = new StreamReader(dlgOpen.FileName);
                textBox1.Text = sr.ReadToEnd();
            }
        }

        private void button4_Click(object sender, EventArgs e)
        {
            textBox1.Text = "";
        }
    }
}
 

Attachments

Last edited:
doesn work for me... spinning doesn work,.... running on windows 7 32 bit
 
Fair play, if that's what people want :) Edited the original post, it's a quickly made program so don't expect anything magical in my code.
 
Thanks.
Btw: I'd catch those nasty DivideByZero-Exceptions when I don't check the divisor.
 
I shared a way to spin in the VB.net section of the forum which is much more reliable (you can convert it easily to C using a code converter)
 
Thanks.
Btw: I'd catch those nasty DivideByZero-Exceptions when I don't check the divisor.

Stupid of me to forget that, haven't had to deal with much math-related coding in forever now, lol. All about iterations when it comes to software lately.

I shared a way to spin in the VB.net section of the forum which is much more reliable (you can convert it easily to C using a code converter)

Please post a link so I can compare? This was a quick post, I know it's not a fantastic program but it works for the most part, it's only the birth of about 2 hour's thought
 
very nice.. includes source code as well.. I built my own text spinner in excel macros lol much less lines but wasn`t multi level
 
Actually this is vb.net function that does nested spinning. It is very very short. I have extracted it from the Wolfpacks script and modified the code a bit, so it is now perfect for integrating into a software, that still haven't got built in a nested spinner.

Private Function Spinner(ByVal mstr As String) As String
Dim returnword As String
Dim arrSplit As String()
Dim cnt As Integer = 0
Dim rdm As New Random()
While Regex.IsMatch(mstr, "\{")
Dim innerspin As String = mstr.Split("{"c)((mstr.Split("{"c).Length) - 1)
Dim tospin As String = Regex.Match(innerspin, "[\s\S]*?(?=\})").Value

arrSplit = tospin.Split("|"c)
returnword = arrSplit(rdm.[Next](0, arrSplit.Length))
cnt += 1
Console.WriteLine(cnt)

mstr = Regex.Replace(mstr, "\{" & Regex.Escape(tospin) & "\}", returnword)
End While
Return mstr
End Function
 
Last edited:
You mean in visual studio on windows 7?

"mstr = Regex.Replace(mstr, "\{" & Regex.Escape(tospin) & "\}", returnword)" should be changed to mstr = Regex.Replace(mstr, "\{" & Regex.Escape(tospin) & "\}", returnword,1) so just the first match would be replaced.

Otherwise if some phrase under under curly quotes (level 1 spin) appears in the text more than one time, then all appearances of this phrase are going to be replaced with the same word. This is bad. It is much better to replace each of these appearances of the phrase, with a random word defined by spintax.
 
Actually this is vb.net function that does nested spinning. It is very very short. I have extracted it from the Wolfpacks script and modified the code a bit, so it is now perfect for integrating into a software, that still haven't got built in a nested spinner.

One critical flaw here, is that you have this inside the function:

Code:
Dim rdm As New Random()

If you call this function multiple times very quickly, say, inside a loop, the Random() class will instantiate an RNG with the same seed, as the seed is based on the clock. You will hence get the same spin each time (when called very quickly).

It's better to use a globally instance of Random, or create one and pass it through if you are calling in a loop.

If anyone is interested, here is Wolfpack's random spin code, reformatted a bit for easy use. All credits go to Wolfpack:

Code:
        /// <summary>
        /// Returns a random spin of the supplied text.
        /// </summary>
        /// <param name="spintax">The text, spintax formatted.</param>
        /// <param name="rng">An instance of Random.</param>
        public static string RandomSpin(string spintax, Random rng)
        {
            string returnword;
            string[] arrSplit;
            while (Regex.IsMatch(spintax, "\\{"))
            {
                string innerspin = spintax.Split('{')[(spintax.Split('{').Length) - 1];
                string tospin = Regex.Match(innerspin, "[\\s\\S]*?(?=\\})").Value;


                arrSplit = tospin.Split('|');
                returnword = arrSplit[rng.Next(0, arrSplit.Length)];


                spintax = Regex.Replace(spintax, "\\{" + Regex.Escape(tospin) + "\\}", returnword);
            }
            return spintax;
        }


        /// <summary>
        /// Returns a random spin of the supplied text.
        /// CAUTION: If using this in a fast loop, use the other overload with a manually created instance of Random().
        /// </summary>
        /// <param name="spintax">The text, spintax formatted.</param>
        public static string RandomSpin(string spintax)
        {
            return RandomSpin(spintax, new Random());
        }
 
Good Job mate ! Just add a Virus total for this.

Thanks
 
Code:
        /// <summary>
        /// Returns a random spin of the supplied text.
        /// </summary>
        /// <param name="spintax">The text, spintax formatted.</param>
        /// <param name="rng">An instance of Random.</param>
        public static string RandomSpin(string spintax, Random rng)
        {
            string returnword;
            string[] arrSplit;
            while (Regex.IsMatch(spintax, "\\{"))
            {
                string innerspin = spintax.Split('{')[(spintax.Split('{').Length) - 1];
                string tospin = Regex.Match(innerspin, "[\\s\\S]*?(?=\\})").Value;


                arrSplit = tospin.Split('|');
                returnword = arrSplit[rng.Next(0, arrSplit.Length)];


                spintax = Regex.Replace(spintax, "\\{" + Regex.Escape(tospin) + "\\}", returnword);
            }
            return spintax;
        }


        /// <summary>
        /// Returns a random spin of the supplied text.
        /// CAUTION: If using this in a fast loop, use the other overload with a manually created instance of Random().
        /// </summary>
        /// <param name="spintax">The text, spintax formatted.</param>
        public static string RandomSpin(string spintax)
        {
            return RandomSpin(spintax, new Random());
        }

Best post on this thread :) Thank you so much for the sweet little function, and extra props for the XML comments, +rep!
 
Back
Top