a scanner for python3.6

Ok so this took a while, and I did find a really neat tool to strip punctuation from stackOverflow.
https://stackoverflow.com/questions/5843518/remove-all-special-characters-punctuation-and-spaces-from-string


So far, I think I got everything covered.  It will take any dictionary, and any sentence and return a list of tuples of the words and numbers and things not found in either of those will be listed as 'error' in the returned tuple.

# a scan method:
from lexicon import game_lexicon
import re


# scan a dictionary and return tuples from it in a list
def scan(some_dict, some_sentence):
# empty tuple list
tuple_list = []
# try it, so if it fails, the scan function doesn't throw error
try:
#user_input = input(" give me a sentence...>")
# make the sentence Lower Case
sentance = some_sentence.lower()
# strip out the punctuations with re module and sub
cleansentance = re.sub(r'[?|$|.|!]',r'',sentance)
# split up the clean sentance
some_list = cleansentance.split()

print(some_list)
except:
print("input not testable.")

# take each word from the user_words list and do something:
for word in some_list:
# I can't use word as a key unless I say it's the key
key = word
#print(key) <--test print
# set success to False so words are only added when success is not True
success =False
try:
#first try and see if it is a number that the user entered
int(word) == word
#print(f"the integer {word} is accepted.") <--- test print
# add the number to the tuple_list if it is a number
tuple_list.append(('number', word))
# set success to True so number is not added to error tuples
success = True
# if it is not a number, check the dictionary
except ValueError:
# for the key or word in the dictionary put it in a tuple list with the value
for key in some_dict:
# define value
val = some_dict.get(key)
# if word is in the dictionary key, add to tuple_list with value
if word in val:
#print(f"word {word} FOUND!")
#print(f"{word} was found in {key} list")
tuple_list.append((key, word))
# set success to True so it is not added to error tuple
success = True
else:
# I'm not sure what to put in this else clause... so CARROTS!
carrots = 'carrots'
#print(f"word not found in {key}")
# i need a way to create tuples of errors that are not found in the lexicon
finally:
# if success is not true for the word that it's testing, add it as an error tuple in list
if not success:
tuple_list.append(('error', word))
else:
# I don't know what to put here, so CARROTS!
carrots = 'carrots'
#print("This word added to tuple list.")
# print it off, later version will be 'return' tuple-list
print(tuple_list)

sentance = input("Give me a test string:")

scan(game_lexicon, sentance)


Comments

Popular posts from this blog

JavaScript Ascii animation with while loops and console.log

playing with trigonometry sin in pygame

JavaScript and a Matrix