Exercise 3 Build a grammar checker function gram_check that takes as an input se
ID: 3755071 • Letter: E
Question
Exercise 3 Build a grammar checker function gram_check that takes as an input sentences of the form [Pronoun] [present tense verb] [object] ex., He plays basketbal and ensures that their is subject-verb agreement; i.e. it distinguishes between He plays basketball and *You plays basketball. Important notes The subject can be any of I, you, he, she, it, they. The verb can be any regular verb The object can be any object. Your program must print "Grammatical" if the sentence is grammatical and "Ungrammatical" if the sentence is ungrammatical Some structure within gram_check has been provided below, but you may change it if you wish. However, do not change the name of the function.Explanation / Answer
import sys
verbs = {}#dictionary to store verbs
def loadVerbs():
f = open("verbs.txt","r")#contains verbs
x = f.readline()#loading verbs in a txt file
while(x):
x = x.strip().lower()#changing every verb to lower case for better checking
verbs[x]=1
x = f.readline()
f.close()
def processVerb(verb):#returns the verb and its pronoun person used.like all the verbs ending with es,s or 3rd person singular verbs
#all the other are other person verbs
if(verb in verbs):#if verb in verbs which are in present tense form
return 1,verb#then that is belongs to 1st person and 3rd person plural
elif(verb[:-1] in verbs):#loves[:-1] =>love which is a verb
if(verb[-1]=="s"):#loves[-1] = s therfore it is a 3rd person singular verb
return 3,verb[:-1]
else:
return 0,verb[:-1]# if not any extra char which may be a spelling mistake .
elif(verb[:-2] in verbs):
if(verb[-2:]=="es"):
return 3,verb[:-2]
else:
return 0,verb[:-1]
else:
return 0,verb
def getPronounIndex(pNoun):
pronoun = ["I","you","they","he","she","it"]
try:
pInd = pronoun.index(pNoun)
except ValueError:
pInd = -1
return pInd
def checkObject(obj):
if(obj in verbs):
return 1
else:
return 0
def gram_check(s):
s = s.strip().split(" ")
pInd = getPronounIndex(s[0])
person,verb = processVerb(s[1])
if(checkObject(s[2])):
print("Object looks like verb,Do you Sure it("+s[2]+") is an Object")
choice = int(input("Enter 0 to Close : "))
if(choice==0):
sys.exit(0)
else:
obj = s[2]
if((person==3 and pInd>2) or (person==1 and pInd<=2)):
print("Grammatical")
elif(person==0):
print("There may be a spelling mistake in the verb",s[1].upper(),"correction may looks like",verb.upper())
else:
print("Ungrammatical,Sentence should be of form <PRNOUN>[PRESENT TENSE VERB][OBJECT]")
loadVerbs()
inp = input("Enter Sentence: ").lower()
gram_check(inp)
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.