Example project for replacing a list of words, part I

Dec 30, 2013 at 10:55 PM
Edited Dec 30, 2013 at 10:59 PM
I thought I would share this util I wrote using docx. It replaces a list of British-spelled words with the equivalent in the American dialect ("colour" becomes "color", "centre" becomes "center", etc.)

All you need is to create a new C# project, add a button and an openfiledialog, keeping the default names, add a reference to DocX, and this should compile:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using Novacode;

namespace AmericanizeBritSpeak
{
public partial class Form1 : Form
{
    List<string> wordPairs;
    public Form1()
    {
        InitializeComponent();
        Popul8WordPairs();
    }

    private void ExpandWordPairs(string britSpelling, string amiSpelling)
    {
        wordPairs.Add(SpacesForeAndAft(britSpelling, amiSpelling));
        wordPairs.Add(CapitalizedAndSpaceAft(britSpelling, amiSpelling));
        wordPairs.Add(SpaceForePeriodAft(britSpelling, amiSpelling));
        wordPairs.Add(SpaceForeCommaAft(britSpelling, amiSpelling));
        wordPairs.Add(SpaceForeColonAft(britSpelling, amiSpelling));
        wordPairs.Add(SpaceForeDashAft(britSpelling, amiSpelling));
    }

    private string SpaceForeDashAft(string britSpelling, string amiSpelling)
    {
        return string.Format(" {0}-# {1}-", britSpelling, amiSpelling);
    }

    private string SpaceForeSemicolonAft(string britSpelling, string amiSpelling)
    {
       return string.Format(" {0};# {1};", britSpelling, amiSpelling);
    }

    private string SpaceForeColonAft(string britSpelling, string amiSpelling)
    {
        return string.Format(" {0}:# {1}:", britSpelling, amiSpelling);
    }

    private string SpaceForeCommaAft(string britSpelling, string amiSpelling)
    {
        return string.Format(" {0},# {1},", britSpelling, amiSpelling);
    }

    private string SpaceForePeriodAft(string britSpelling, string amiSpelling)
    {
        return string.Format(" {0}.# {1}.", britSpelling, amiSpelling);
    }

    private string CapitalizedAndSpaceAft(string britSpelling, string amiSpelling)
    {
        string capitalizedBrit = char.ToUpper(britSpelling[0]) + britSpelling.Substring(1);
        string capitalizedAmi = char.ToUpper(amiSpelling[0]) + amiSpelling.Substring(1);
        return string.Format("{0} #{1} ", capitalizedBrit, capitalizedAmi);
    }

    private string SpacesForeAndAft(string britSpelling, string amiSpelling)
    {
        return string.Format(" {0} # {1} ", britSpelling, amiSpelling);
    }

    private void Popul8WordPairs()
    {
        wordPairs = new List<string>();
        ExpandWordPairs("æroplane", "airplane");
        ExpandWordPairs("æsthetic", "esthetic");
        ExpandWordPairs("ageing", "aging");
        ExpandWordPairs("æsthetic", "esthetic");
        ExpandWordPairs("ageing", "aging");
        ExpandWordPairs("aluminium", "aluminum");
        ExpandWordPairs("amœba", "ameba");
        ExpandWordPairs("anæmia", "anemia");
        ExpandWordPairs("anæsthesia", "anesthesia");
        ExpandWordPairs("analyse", "analyze");
        ExpandWordPairs("analogue", "analog");
        ExpandWordPairs("annexe", "annex");
        ExpandWordPairs("apologise", "apologize");
        ExpandWordPairs("apologising", "apologizing");
        ExpandWordPairs("archæology", "archeology");
        ExpandWordPairs("ardour", "ardor");
        ExpandWordPairs("armour", "armor");
        ExpandWordPairs("arse", "ass");
        ExpandWordPairs("artefact", "artifact");
        ExpandWordPairs("authorise", "authorize");
        ExpandWordPairs("authorising", "authorizing");
        ExpandWordPairs("axe", "ax");
        ExpandWordPairs("behaviour", "behavior");
        ExpandWordPairs("broncho", "bronco");
        ExpandWordPairs("caesium", "cesium");
        ExpandWordPairs("cæsium", "cesium");
        ExpandWordPairs("chamomile", "camomile");
        ExpandWordPairs("cancelled", "canceled");
        ExpandWordPairs("cancelling", "canceling");
        ExpandWordPairs("carburettor", "carburetor");
        ExpandWordPairs("catalogue", "catalog");
        ExpandWordPairs("centre", "center");
        ExpandWordPairs("cheque", "check");
        ExpandWordPairs("chequer", "checker");
        ExpandWordPairs("cypher", "cipher");
        ExpandWordPairs("cyphering", "ciphering");
        ExpandWordPairs("civilise", "civilize");
        ExpandWordPairs("civilising", "civilizing");
        ExpandWordPairs("civilisation", "civilization");
        ExpandWordPairs("colonise", "colonize");
        ExpandWordPairs("colonising", "colonizing");
        ExpandWordPairs("colonisation", "colonization");
        ExpandWordPairs("colour", "color");
        ExpandWordPairs("colouring", "coloring");
        ExpandWordPairs("cosy", "cozy");
        ExpandWordPairs("counsellor", "counselor");
        ExpandWordPairs("counselling", "counseling");
        ExpandWordPairs("defence", "defense    ");
        ExpandWordPairs("dialler", "dialer");
        ExpandWordPairs("dialling", "dialing");
        ExpandWordPairs("dialogue", "dialog");
        ExpandWordPairs("diarrhœa", "diarrhea");
        ExpandWordPairs("disc", "disk");
        ExpandWordPairs("distention", "distension");
        ExpandWordPairs("distil", "distill");
        ExpandWordPairs("doughnut", "donut");
        ExpandWordPairs("draught", "draft");
        ExpandWordPairs("dreamt", "dreamed");
        ExpandWordPairs("emphasise", "emphasize");
        ExpandWordPairs("emphasising", "emphasizing");
        ExpandWordPairs("encyclopaedia", "encyclopedia");
        ExpandWordPairs("œnology", "enology");
        ExpandWordPairs("enrolment", "enrollment");
        ExpandWordPairs("enroling", "enrolling");
        ExpandWordPairs("equalling", "equaling");
        ExpandWordPairs("equalising", "equalizing");
        ExpandWordPairs("œsophagus", "esophagus");
        ExpandWordPairs("œstrogen", "estrogen");
        ExpandWordPairs("oedema", "edema");
        ExpandWordPairs("endeavour", "endeavor");
        ExpandWordPairs("endeavouring", "endeavoring");
        ExpandWordPairs("favourite", "favorite");
        ExpandWordPairs("fæces", "feces");
        ExpandWordPairs("fibre", "fiber");
        ExpandWordPairs("fœtid", "fetid");
        ExpandWordPairs("fœtus", "fetus");
        ExpandWordPairs("flautist", "flutist");
        ExpandWordPairs("flavour", "flavor");
        ExpandWordPairs("flavoured", "flavored");
        ExpandWordPairs("fulfil", "fulfill");
        ExpandWordPairs("furore", "furor");
        ExpandWordPairs("fuelling", "fueling");
        ExpandWordPairs("gaol", "jail");
        ExpandWordPairs("generalise", "generalize");
        ExpandWordPairs("generalising", "generalizing");
        ExpandWordPairs("glycerine", "glycerin");
        ExpandWordPairs("grey", "gray");
        ExpandWordPairs("gynæcology", "gynecology");
        ExpandWordPairs("hæmophilia", "hemophilia");
        ExpandWordPairs("haematology", "hematology");
        ExpandWordPairs("haem", "heme");
        ExpandWordPairs("harbour", "harbor");
        ExpandWordPairs("harbouring", "harboring");
        ExpandWordPairs("harmonise", "harmonize");
        ExpandWordPairs("harmonising", "harmonizing");
        ExpandWordPairs("harmonisation", "harmonization");
        ExpandWordPairs("homologue", "homolog");
        ExpandWordPairs("honour", "honor");
        ExpandWordPairs("honourable", "honorable");
        ExpandWordPairs("honouring", "honoring");
        ExpandWordPairs("humour", "humor");
        ExpandWordPairs("humouring", "humoring");
        ExpandWordPairs("instalment", "installment");
        ExpandWordPairs("italicise", "italicize");
        ExpandWordPairs("italicising", "italicizing");
        ExpandWordPairs("italicisation", "italicization");
        ExpandWordPairs("jewellery", "jewelry");
  • continued in part II of the post
Developer
Jan 1, 2014 at 7:04 PM
Please put this into one single post not into 2 separate threads. Doesn't make much sense otherwise.
Jan 2, 2014 at 3:19 PM
MadBoy wrote:
Please put this into one single post not into 2 separate threads. Doesn't make much sense otherwise.
I tried, but I hit the length-limitation wall.

I now have it at http://www.codeproject.com/Tips/704411/How-to-Replace-a-List-of-Words-in-a-DOCX-File-usin
Developer
Jan 2, 2014 at 4:09 PM
That's nice.