Skip to content

Instantly share code, notes, and snippets.

@antdking
Last active December 23, 2015 08:39
Show Gist options
  • Save antdking/6609240 to your computer and use it in GitHub Desktop.
Save antdking/6609240 to your computer and use it in GitHub Desktop.
#!/usr/bin/env python
'''
Front end for displaying the definitions on screen, and to forward
the words to the spellChecker function to return a value of 0, 1 or 2
This is just an example to use the spellChecker, it will not be used
in the final product
'''
# create and parse a temporary database
def parseDb():
# we will use sqlite3 for testing because it needs no configuring on the system
from sqlite3 import connect
# dump the database to ram for testing
db = connect(":memory:")
cur = db.cursor()
# table only needs to be made up of the word and definition for now
cur.execute('''CREATE TABLE wordList
(word text, definition text)''')
cur.execute("INSERT INTO wordList VALUES ('hello', 'a greeting')")
cur.execute("INSERT INTO wordList VALUES ('night', 'the time of day that you sleep')")
cur.execute("INSERT INTO wordList VALUES ('activity', 'being busy')")
# save the database
db.commit()
# select the word and definition columns from the table
cur.execute("select word, definition from wordList")
# and dump them as a list
buff = cur.fetchall()
# close the database
db.close()
# and return the list
return buff
def main():
# we only need to import the spellChecker function
from check_word import spellChecker
# local store for the scores
scores = []
# for totalling up the scores at the end
totalScore = 0
print "for each definition, type in the spelling of the word"
# for each word and definition in the list that is returned from the parseDB function
for word, definition in parseDb():
# output the definition and capture the user's input in a variable
input = raw_input(definition + ": ")
# calculate the score using the spellChecker function, and add it to the scores list
scores.append(spellChecker(word, input))
# for each value in the scores list, add it to the total score
for score in scores:
totalScore += score
# finally, tell the user what their total score was
print "your total score for this test was %d" % totalScore
# only run if called directly
if __name__ == '__main__':
main()
#!/usr/bin/env python
'''
function to check (minimally) the spelling of the word
This will then check against a percentage (which will be dynamically defined at a later point)
This will then return either 2 (for a correct word), 1 (for an almost correct word), or 0
(for an incorrect word)
'''
def calculatePercent(errorCount, wordLength):
# convert the integers to real numbers
# this is to allow the final value to contain decimal points
# else the value will always be 0
errorCount = float(errorCount)
wordLength = float(wordLength)
# calculate the percent. (change/original * 100)
percent = errorCount/wordLength*100
# use a hard coded value for now for saying if you get 1 or 0 points
passLimit = 20 # %
#if the pecent is lower than or equal to passLimit
if percent <= passLimit:
# return 1 back to the spellChecker function
return 1
else:
# return 0 back to the spellChecker function
return 0
def spellChecker(real, input):
# convert to lower case. we don't care if they type like a serial killer
real = real.lower()
input = input.lower()
# catch if both words are the same at the start
if input == real:
return 2
# define the counters needed for the loops and error checking
placeCount = 0
errorCount = 0
# split the words into arrays
realList = list(real)
inputList = list(input)
# if both of the words are the same length, we can skip checking the next letter on
if len(input) == len(real):
# for each letter in the realList arrays
for letter in realList:
# if the letter is incorrect
if not letter == inputList[placeCount]:
# assign an error point
errorCount += 1
# move onto the next letter
placeCount += 1
else:
# now we need to check the letter against the next letter on
# but only if the letter against letter returns false
# for each letter in the realList arrays
for letter in realList:
# if the letter is incorrect
try:
inputList[placeCount]
except IndexError:
# end of the word
# continue the for loop to build up the error count
placeCount -= 1
if not letter == inputList[placeCount]:
# assign an error point
errorCount += 1
'''
check if it was a letter missings
if it is, go back a letter on the input word
this will cause the input word letter to be repeated
'''
# use a try catch in the event of an index
# this would be caused if the last letter of the input word is incorrect
try:
# check against the next letter of the input word
if inputList[placeCount + 1] == letter:
print inputList[placeCount + 1]
# go back a letter
# this will cause the letter to be repeated
placeCount -= 1
except IndexError:
# out of letters. go back a letter
placeCount -= 1
# go onto the next letter
placeCount += 1
# return the return value of calculatePercent back to the main function
return calculatePercent(errorCount, len(real))
# ---------------------------------------------------
# this is a temporary measure for testinf purposes
# ---------------------------------------------------
if __name__ == '__main__': # only run these commands if the file is called directly
from sys import argv # to get the args from stdin
# call the function and print the return values (basic implementation)
print spellChecker(argv[1], argv[2])
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment