Alex Rivera | Logout

PHP Repairing Bad Text

Asked 2011-12-15T10:14:42.137
12

This is something I'm working on and I'd like input from the intelligent people here on StackOverflow.

What I'm attempting is a function to repair text based on combining various bad versions of the same text page. Basically this can be used to combine different OCR results into one with greater accuracy than any of them individually.

I start with a dictionary of 600,000 English words, that's pretty much everything including legal and medical terms and common names. I have this already.

Then I have 4 versions of the text sample.

Something like this:

$text[0] = 'Fir5t text sample is thisline';
$text[1] = 'Fir5t text Smplee is this line.';
$text[2] = 'First te*t sample i this l1ne.';
$text[3] = 'F i r st text s ample is this line.';

I attempting to combine the above to get an output which looks like:

$text = 'First text sample is this line.';

Don't tell me it's impossible, because it is certainly not, just very difficult.

I would very much appreciate any ideas anyone has towards this.

Thank you!

My current thoughts:

Just checking the words against the dictionary will not work, since some of the spaces are in the wrong place and occasionally the word will not be in the dictionary.

The major concern is repairing broken spacings, once this is fixed then then the most commonly occurring dictionary word can be chosen if exists, or else the most commonly occurring non-dictionary word.

Edit
Report

2 Answers

1

Tackling complex algorithms like this by yourself will probably take longer and be more error prone than using a third party tool - unless you really need to program this yourself, you can check the Yahoo Spelling Suggestion API. They allow 5.000 requests per IP per day, I believe.

Others may offer something similar (I think there's a bing API, too).

UPDATE: Sorry, I just read that they've stopped this service in April 2011. They claim to offer a similar service called "Spelling Suggestion YQL table" now.

answered 2011-12-15T10:31:44.967
1

This is indeed a rather complicated problem.

When I do wonder how to spell a word, the direct way is to open a dictionary. But what if it is a small complex sentence that I'm trying to spell correctly ? One of my personal trick, which works most of the time, is to call Google. I place my sentence between quotes on Google and count the results. Here is an example : entering "your very smart" on Google gives 13'600k page. Entering "you're very smart" gives 20'000k pages. Then, likely, the correct spelling is "you're very smart". And... indeed it is ;)

Based on this concept, I guess you have samples which, for the most parts, are correctly misspelled (well, maybe not if your develop for a teens gaming site...). Can you try to divide the samples into sub pieces, not going up to the words, and matching these by frequency ? The most frequent piece is the most likely correctly spelled. Prior to this, you can already make a dictionary spellcheck with your 600'000 terms to increase the chance that small spelling mistakes will alredy be corrected. This should increase the frequency of correct sub pieces.

Dividing the sentences in pieces and finding the right "piece-size" is also tricky.

What concerns me a little too : how do you extract the samples and match them together to know the correctly spelled sentence is the same (or very close?). Your question seems to assume you have this, which also seems something very complex for me.

Well, what precedes is just a general tip based on my personal and human experience. Donno if this can help. This is obviously not a real answer and is not meant to be one.

answered 2011-12-15T10:39:36.657

Your Answer