2

I am reaching a deadline at one of my projects and I am kind of stuck so I will need your help. The homework is all about making a spell checker in Prolog which will use an input of lists with letters and output the corrected lists. I will be given the correct words in a format like this:

word([t,h,e]).
word([h,e,l,l,o]).
word([w,o,r,l,d]).

The errors I should check about are

  1. One letter error. Example: [t,h,a] -> [t,h,e].
  2. One letter missing. Example: [h,e,l,l] -> [h,e,l,l,o].
  3. One letter extra. Example: [w,o,l,r,l,d] -> [w,o,r,l,d]

I already have the code for each kind of correction but I have to run every single rule for each word and get multiple results of which only 1 is the correct one. Is there any way to determine what the error is in the word given and run only the appropriate rule in order to correct it?

tshepang
  • 12,111
  • 21
  • 91
  • 136
Theocharis K.
  • 1,281
  • 1
  • 16
  • 43
  • I don't think that you can always determine the error; for example `hel` could be either `hell` (one letter missing) or `he` (one extra letter). You mentioned that you get multiple results and only 1 is correct; could you post an example? – Thanos Tintinidis Dec 17 '11 at 17:16
  • @thanosQR The words I will be given will have no such similarities so there is not a possibility to mistake one word with another. For example let's say this is the prompt: ?-checker([[t,h,e],[w,o,l,r,l,d]],X). X=[[t,h,e],[w,o,l,r,l,d]]; % Using the one letter error. X=[[t,h,e],[w,o,l,r,l,d]]; % Using the one letter missing. X=[[t,h,e],[w,o,r,l,d]]; % Using the one letter extra. – Theocharis K. Dec 17 '11 at 17:23

2 Answers2

1

It seems like you just need a disjunction :

my_correction(Word) :-
    (
        correction1(Word, Result)
    ;
        correction2(Word, Result)
    ;
        correction3(Word, Result)
    ),
    % do stuff with Result.

Or maybe I misunderstood your problem...

You may want to wrap this in a if structure for the case where a correction isn't needed :

my_correction(Word) :-
    (   \+ (correction1(Word, Result);correction2(Word, Result);correction3(Word, Result))
     -> Result = Word
     ;  true),
    % do stuff with Result.

(that says that if no correction has been needed, Result = Word, else, Result is already holding the correcting word, so just return true).

m09
  • 7,490
  • 3
  • 31
  • 58
  • That would do it Mog. Well not all of it but I noticed that if I use the code you provided in the beggining along with all my rules seperated by ; then the very first result I will take is the correct one. Although I don't know why is that happening is there any way just to keep the first result as long as it's correct? A way to stop the recursion I mean – Theocharis K. Dec 17 '11 at 21:28
  • 1
    yup, you can call once(my_correction) or use a cut after it (my_correction(X), !, % rest... – m09 Dec 18 '11 at 10:09
  • Thanks, a ! just before the recursion fixed the problem by showing only 1 result. I hope that will pass. Thank you very much. – Theocharis K. Dec 18 '11 at 14:16
1

Here is an implementation of a simple spellchecker in Prolog:

https://github.com/Attempto/APE/blob/master/lexicon/spellcheck.pl

Kaarel
  • 10,554
  • 4
  • 56
  • 78
  • That could help me but I don't want to copy something, I want to get the point really. Thanks for the link though. – Theocharis K. Dec 17 '11 at 18:41
  • The given link has changed to this : https://github.com/Attempto/APE/tree/master/prolog/lexicon/spellcheck.pl – peter.cyc Oct 18 '20 at 09:51