# Ruby to convert US to UK punctuation/spelling?

**URL:** <https://www.ruby-forum.com/t/ruby-to-convert-us-to-uk-punctuation-spelling/140544>\
**Category:** Ruby\
**Created:** [June 16, 2008, 2:46am UTC](https://www.ruby-forum.com/t/ruby-to-convert-us-to-uk-punctuation-spelling/140544 "2008-06-16T02:46:10Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![dreamwave](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/7feea3/32.png) [@dreamwave](https://www.ruby-forum.com/u/dreamwave)\
**Post date:** [June 16, 2008, 2:46am UTC](https://www.ruby-forum.com/t/ruby-to-convert-us-to-uk-punctuation-spelling/140544/1 "2008-06-16T02:46:10Z")

</div>

I have about a thousand multipage documents which I need to convert from  
US English and punctuation to UK English and punctuation. Before I start  
on a ruby script (I’m just learning ruby) wanted to see if anyone knows  
of existing tools to do this? I’ve also looked into a perl US-\>UK  
conversion tool but doesn’t seem to exist.

I’m starting with utf8 rtf documents which have printer’s quotes (i.e.,  
distinct left and right curly quotes) which were retained from an  
original conversion from MS Word docs. For my documents, converting from  
US to UK punctuation means double quotes become single quotes and some  
single quotes become double (apostrophes are retained and single quotes  
not inside double quotes would need to be retained); but in the  
conversion I would like to retain distinct left and right quotation  
marks.

I’m thinking that the end documents should have all print typography  
(em-dashes, en-dashes, printer quotes) should be converted to character  
entities.

If there is no existing script to do this (seems like a problem others  
must have faced before) any thoughts on the right approach/tools/code  
snippets?

Many thanks Rubyist…

---

<div class="post-metadata">

**Author:** ![dreamwave](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/7feea3/32.png) [@dreamwave](https://www.ruby-forum.com/u/dreamwave)\
**Post date:** [June 16, 2008, 12:48pm UTC](https://www.ruby-forum.com/t/ruby-to-convert-us-to-uk-punctuation-spelling/140544/2 "2008-06-16T12:48:06Z")

</div>

-------- Original-Nachricht --------

> Datum: Mon, 16 Jun 2008 09:45:45 +0900  
> Von: Michael L. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
> An: removed\_email\_address@domain.invalid  
> Betreff: Ruby to convert US to UK punctuation/spelling?

Dear Michael,

> I have about a thousand multipage documents which I need to convert from  
> US English and punctuation to UK English and punctuation. Before I start  
> on a ruby script (I’m just learning ruby) wanted to see if anyone knows  
> of existing tools to do this? I’ve also looked into a perl US-\>UK  
> conversion tool but doesn’t seem to exist.

for general spell-checking, there is aspell, which you can use with  
different language  
options, and there are Ruby bindings for it:

[http://blog.evanweaver.com/files/doc/fauna/raspell/files/README.html](http://blog.evanweaver.com/files/doc/fauna/raspell/files/README.html)

So you might use the language option Aspell.new(“en\_GB”) rather than  
Aspell.new(“en\_US”) for the spell checking of misspelled (in the British  
English sense) American English text.  
If you have so much text, it will find some other errors, that both  
language forms consider erroneous, too. 🙂

> I’ve also looked into a perl US-\>UK conversion tool but doesn’t seem to exist.

There certainly are Perl bindings to aspell- I’d bet a hundred quid/two  
hundred bucks 🙂

> For my documents, converting from  
> US to UK punctuation means double quotes become single quotes and some  
> single quotes become double (apostrophes are retained and single quotes  
> not inside double quotes would need to be retained); but in the  
> conversion I would like to retain distinct left and right quotation  
> marks.

That suggests some combination of String#scan, String#gsub and Regular  
expressions …  
Since apostrophes and quotation marks are the same sign, I’d suggest  
making a  
list of words with apostrophes, write them to a file, where you can  
correct them manually,  
and String#gsub - replace first the apostrophes by something like  
  
and then the quotes by and or the other way round.

There’s a Regular expressions tutorial here:

> **[Regular Expression Tutorial - Learn How to Use Regular Expressions](https://www.regular-expressions.info/tutorial.html)**
>
> This tutorial teaches you how to create your own regular expressions, starting with the most basic regex concepts and ending with the most advanced and specialized capabilities.

> I’m thinking that the end documents should have all print typography  
> (em-dashes, en-dashes, printer quotes) should be converted to character  
> entities.

You can do that with String.gsub(“–”,‘’), after having copied  
the em-dash  
into the double quotes… etc…

Best regards,

Axel
