0

I have created a simple py quiz and now want to develop my program. Being dyslexic I get rather frustrated when I have come really close to matching a word but it is just not quite spelt right.

Therefore I have written the function below to check for approximate spelling of a word based on the following rules: the first two letters must match, the last letter must match and the users answer is within 1 character, plus or minus, of the correct answer.

The code works but I am sure it can be simplified using a regular expr . I have had a look at a few tutorials but now stuck.

word= paris
my attempt to match [p,a] {3:5} [s]

def closeMatch():
    word=input('input your word here')
    wordLen=len(word)
    lowWord=wordLen-1
    highWord=wordLen+1
    frontSplit=(word[0:2])
    backSplit=(word[-1])
    myWord=input('input the word you want to test')
    print('this is the word you entered ' , myWord)
    myWordLen=len(myWord)
    myWordFSplit=(myWord[0:2])
    myWordBSplit=(myWord[-1])

    if myWord==word:
        print('Correct')
    elif myWordFSplit==frontSplit and myWordBSplit==backSplit and myWordLen>=lowWord and myWordLen<=highWord:
         print('Nearly correct ' , myWord , ' has at least the first 2 and last letters the same')
         print('Also the length of ' , myWord , ' is within  one character, plus or minus, of the the word ' , word )
    else:
        print('Incorrect')
closeMatch()
Lesmana
  • 25,663
  • 9
  • 82
  • 87
user3234931
  • 83
  • 1
  • 3

3 Answers3

1

Therefore I have written the function below to check for approximate spelling of a word based on the following rules: the first two letters must match, the last letter must match and the users answer is within 1 character, plus or minus, of the correct answer. The code works but I am sure it can be simplified using a regular expr . I have had a look at a few tutorials but now stuck.

I don't think your approach is the good one. Given what you say, you need to find the closest string from your dictionary, so I think you should calculate the hamming distance between the word you input and the words in the dictionary. I think that I've read dyslexic people usually get right the first and last letters of words, so I'd look for words that share the same first and last letter for which the hamming distance is the closest.

So the improvement I'd do on your algorithm would be:

  • match every word from dict that share same (two?) first and last letter with the input ;
  • for each word of that set, calculate the hamming distance for the word and the input ;
  • sort them by closest distance, and display the matches

HTH

zmo
  • 24,463
  • 4
  • 54
  • 90
  • Thanks for the advice - hamming distance offers a much better approach. I have had a quick play around with some snippets of code found on this site 'Finding how similar two strings are'and now confident I have a way forward with this - cheers – user3234931 Feb 01 '14 at 11:40
0

I've rewritten your code. It takes a raw_input string,word, and matches it towards another string, match_word, that is supposed to be correctly spelled. The function closeMatch () checks three conditions. First it attempts to match the whole word, word == match word, then the first_two letters, first = match_word[0:2] and lastly it tries to match the ending, [-1]. I wrote "car" as a test for a match_word. I've used two functions, startswith and endswith, which match the begining and the end letters of a string respectivly.

import re

# takes raw_input word, word

word = raw_input('Word:')

# takes correctly spelled word


match_word = "car"

def closeMatch(word, match_word):



    first = match_word[0:2] # first two letters
    back = match_word[-1] # end letter


    for i in range(len(word)):


        # checks if the word starts with (startswith function) the front letter


        if word == match_word:
            return "Correct, you spelled", word, "correctly!"





        elif word.startswith(first):

            return "Nearly correct, you've spelled", word, "begining with",first





        elif word[-1].endswith(back):
            return "Almost correct, you've spelled", word, "ending with",back

    else:
        return "Incorrect"


print closeMatch(word,match_word)

Output: if correct, ('Correct, you spelled', 'car', 'correctly!') if nearly, ("Nearly correct, you've spelled", 'cab', 'begining with', 'ca') if end letter, ("Almost correct, you've spelled", 'vcr', 'ending with', 'r') else, Incorrect

user1749431
  • 559
  • 6
  • 21
  • Thanks for the help - this is where I am up to: – user3234931 Feb 01 '14 at 13:37
  • Having had a look around and play I am going down the route of importing [difflib](http://stackoverflow.com/questions/6690739/fuzzy-string-comparison-in-python-confused-with-which-library-to-use) as well as stipulating first 2 and last letters. – user3234931 Feb 01 '14 at 13:45
  • I hope it's useful. There are probably other functions than start - and endswith but the conditional statements will do most of the job. I noticed in your code that you were using input(), note that input() is for digits, use raw_input() for strings. – user1749431 Feb 01 '14 at 14:31
0

This how I have modified the code in the light of comments - thanks for the help

import difflib

word = 'maneuver' #nasty word to spell (for us dyslexics anyway)
match_word =input('The word is maneuver >> test it here')

def closeMatch(word, match_word):

    first = match_word[0:2] # first two letters
    back = match_word[-1] # end letter
    #keeping with the rule that first two and last letters must be the same as the word

    for i in range(len(word)):
        # checks if the word starts with (startswith function) the front letter
        if word == match_word:
            return 'Correct, you spelled', word, 'correctly!'

        elif word.startswith(first) and word[-1].endswith(back):
           ratioScore=difflib.SequenceMatcher(None, word, match_word).ratio()
        #with the use of the difflib library I now have value to logic test also
           return 'Nearly correct - the ratio score is ' , ratioScore , ' Now logic test the ratio score'
    else:
        return 'Incorrect'

print (closeMatch(word, match_word))
user3234931
  • 83
  • 1
  • 3