TheDeveloperBlog.com

Home | Contact Us

C-Sharp | Java | Python | Swift | GO | WPF | Ruby | Scala | F# | JavaScript | SQL | PHP | Angular | HTML

C# Spelling Errors, Generator

This C# program generates spelling errors from a string. It can be used as part of a spell-checker.

Spelling errors. A list of errors has many uses.

With it, we can scan for occurrences of those errors. A list of spelling errors can be used to search for known spelling mistakes. These errors can be generated with a simple program.

Based on:

.NET 4.5

Example. This program creates spelling errors. We pass the Apply() method a string. The method then writes many possible spelling mistakes for that string. It applies ten transformations. These use four custom methods.

SwapChars: This exchanges two adjacent characters in the string. It is the same as if you typed two correct chars, but in the wrong order.

RepeatChars: This repeats one character. It is the same as if a key became stuck on the keyboard.

DeleteChar: A char was omitted. This could be a genuine spelling mistake or an editing error.

ChangeCharIfValue: This exchanges certain letter values—the lowercase "l" can be exchanged for the letter "i" for example.

In the Apply method, we loop through each char in the string. Then we loop through ten numbers (0 through 9). We apply a different transformation for each number. If the result is non-null, we display the generated error.

C# program that generates spelling errors

using System;

class Program
{
    static void Main()
    {
	Apply("hello");
    }

    /// <summary>
    /// Apply.
    /// </summary>
    static void Apply(string value)
    {
	const int max = 9;
	for (int i = 0; i < value.Length; i++)
	{
	    for (int pass = 0; pass <= max; pass++)
	    {
		string result = null;
		switch (pass)
		{
		    case 0:
			result = SwapChars(value, i);
			break;
		    case 1:
			result = RepeatChars(value, i);
			break;
		    case 2:
			result = DeleteChar(value, i);
			break;
		    case 3:
			result = ChangeCharIfValue(value, i, 'i', 'l');
			break;
		    case 4:
			result = ChangeCharIfValue(value, i, 'l', 'i');
			break;
		    case 5:
			result = ChangeCharIfValue(value, i, 'e', 'a');
			break;
		    case 6:
			result = ChangeCharIfValue(value, i, 't', 'l');
			break;
		    case 7:
			result = ChangeCharIfValue(value, i, 'm', 'n');
			break;
		    case 8:
			result = ChangeCharIfValue(value, i, 'n', 'm');
			break;
		    case 9:
			result = ChangeCharIfValue(value, i, 'a', 'e');
			break;
		}
		if (result != null)
		{
		    Console.WriteLine(result);
		}
	    }
	}
    }

    /// <summary>
    /// SwapChars.
    /// [Flip 2 chars]
    /// </summary>
    static string SwapChars(string value, int index)
    {
	if (index == value.Length - 1)
	{
	    return null;
	}
	char[] array = value.ToCharArray();
	char temp1 = array[index];
	char temp2 = array[index + 1];
	if (temp1 == temp2)
	{
	    return null;
	}
	array[index] = temp2;
	array[index + 1] = temp1;
	return new string(array);
    }

    /// <summary>
    /// RepeatChars.
    /// [Repeat 1 char]
    /// </summary>
    static string RepeatChars(string value, int index)
    {
	return value.Substring(0, index) + value.Substring(index, 1) +
	    value.Substring(index);
    }

    /// <summary>
    /// DeleteChar.
    /// </summary>
    static string DeleteChar(string value, int index)
    {
	return value.Substring(0, index) + value.Substring(index + 1);
    }

    /// <summary>
    /// ChangeCharIfValue.
    /// </summary>
    static string ChangeCharIfValue(string value, int index, char before, char after)
    {
	if (value[index] == before)
	{
	    char[] array = value.ToCharArray();
	    array[index] = after;
	    return new string(array);
	}
	return null;
    }
}

Output

ehllo
hhello
ello
hlelo
heello
hllo
hallo
helllo
helo
heilo
helol
helllo
helo
helio
helloo
hell

In the output, we find lots of errors (typos) for the string "hello". Some values, such as "hell" may not actually be spelling mistakes. Other values, such as "ehllo" are most likely typos in a text.

Discussion. One way to test a system is to look for possible errors. Even unlikely errors can be tested. With a good algorithm, many thousands of errors could be quickly checked. This could improve quality.

For a spelling-checker, a directed acylic word graph is helpful. We can place errors, as from this generator algorithm, into the tree. A program then can scan massive amounts of text in minimal time.

TreeSpell-Checker

Summary. A text could be proven correct. This is difficult—computer programs do not yet have a way to understand writing and prove its correctness. This is a task for artificial intelligence.

However: We can simply look for known errors, in spelling, grammar and even content, to detect many problems.


Related Links

Adjectives Ado Ai Android Angular Antonyms Apache Articles Asp Autocad Automata Aws Azure Basic Binary Bitcoin Blockchain C Cassandra Change Coa Computer Control Cpp Create Creating C-Sharp Cyber Daa Data Dbms Deletion Devops Difference Discrete Es6 Ethical Examples Features Firebase Flutter Fs Git Go Hbase History Hive Hiveql How Html Idioms Insertion Installing Ios Java Joomla Js Kafka Kali Laravel Logical Machine Matlab Matrix Mongodb Mysql One Opencv Oracle Ordering Os Pandas Php Pig Pl Postgresql Powershell Prepositions Program Python React Ruby Scala Selecting Selenium Sentence Seo Sharepoint Software Spellings Spotting Spring Sql Sqlite Sqoop Svn Swift Synonyms Talend Testng Types Uml Unity Vbnet Verbal Webdriver What Wpf